7 releases

0.1.6 Aug 11, 2024
0.1.5 Aug 11, 2024

#1175 in Parser implementations

Apache-2.0

98KB
2.5K SLoC

rdcache

Crates.io MIT/Apache-2 licensed

Rust version of mimetype

Features

  • Detects MIME type based on the "magic bytes" of a file.

Example

Sync version

Add this to your Cargo.toml:

[dependencies]
mimetype = "0.1.6"

Then you can use it like this:

fn main() {
    let file = std::fs::read("test.jpg").unwrap();

    let mime = mimetype::detect(&file);

    println!("{:?}", mime);
}

or use reader

fn main() {
    let file = std::fs::File::open("Cargo.toml").unwrap();

    let mime = mimetype::detect(&file);

    println!("{:?}", mime);
}

Async version

Add this to your Cargo.toml:

[dependencies]
mimetype = { version = "0.1.6", features = ["async"] }

Then you can use it like this:

#[tokio::main]
async fn main() {
    let file = tokio::fs::read("test.jpg").await.unwrap();

    let mime = mimetype::detect_async(&file).await;

    println!("{:?}", mime);
}

or use reader

#[tokio::main]
async fn main() {
    let file = tokio::fs::File::open("test.jpg").await.unwrap();

    let mime = mimetype::detect_async(&file).await;

    println!("{:?}", mime);
}

The output will be like:

Mime { mime: "image/jpeg", aliases: [], extension: ".jpg" }

Dependencies

~2.2–9MB
~83K SLoC