mimetype

Crates.iomimetype
lib.rsmimetype
version0.1.6
sourcesrc
created_at2024-08-05 09:44:07.646498
updated_at2024-08-11 03:09:48.142042
descriptiondetech mime type from file bytes
homepage
repositoryhttps://github.com/hoslo/mimetype
max_upload_size
id1325760
size108,397
(hoslo)

documentation

README

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" }
Commit count: 0

cargo fmt