Module std::net::mime

MIME type detection.

Determine the MIME content type of a file by its path or extension. Useful for HTTP servers that need to set Content-Type headers.

Examples

import std.net.mime;

fn main() {
    let ct = mime.from_path("index.html");
    println(ct);  // "text/html"

    if mime.is_text(ct) {
        println("text file");
    }
}

Contents

Functions

Function from_path

pub fn from_path(path: string) -> string

Detect the MIME type from a file path.

Uses the file extension to determine the type. Returns "application/octet-stream" for unknown extensions.

Examples

import std.net.mime;

fn main() {
    println(mime.from_path("photo.jpg"));   // "image/jpeg"
    println(mime.from_path("style.css"));   // "text/css"
    println(mime.from_path("data.json"));   // "application/json"
}

Function from_ext

pub fn from_ext(ext: string) -> string

Detect the MIME type from a file extension (without the dot).

Examples

import std.net.mime;

fn main() {
    println(mime.from_ext("html"));   // "text/html"
    println(mime.from_ext("png"));   // "image/png"
}

Function is_text

pub fn is_text(mime_type: string) -> bool

Test whether a MIME type is a text type (e.g. text/*).

Function is_image

pub fn is_image(mime_type: string) -> bool

Test whether a MIME type is an image type (e.g. image/*).

Function is_audio

pub fn is_audio(mime_type: string) -> bool

Test whether a MIME type is an audio type (e.g. audio/*).

Function is_video

pub fn is_video(mime_type: string) -> bool

Test whether a MIME type is a video type (e.g. video/*).