| Crates.io | mimetype-detector |
| lib.rs | mimetype-detector |
| version | 0.3.4 |
| created_at | 2025-07-23 14:03:24.031033+00 |
| updated_at | 2025-12-15 14:31:07.533822+00 |
| description | Fast, accurate, and thread-safe MIME type detection for ~450 file formats with zero dependencies |
| homepage | https://github.com/Asuan/mimetype-detector |
| repository | https://github.com/Asuan/mimetype-detector |
| max_upload_size | |
| id | 1764814 |
| size | 408,640 |
Fast MIME type detection for ~450 file formats with zero dependencies.
🌐 Online Detector - Test file detection directly in your browser
[dependencies]
mimetype-detector = "0.3.4"
use mimetype_detector::{detect, detect_file, constants::*};
// From bytes - Basic detection
let data = b"\x89PNG\r\n\x1a\n";
let mime = detect(data);
assert_eq!(mime.mime(), IMAGE_PNG);
assert_eq!(mime.extension(), ".png");
assert_eq!(mime.name(), "Portable Network Graphics");
// From file
let mime = detect_file("document.pdf")?;
if mime.is(APPLICATION_PDF) {
println!("Detected: {}", mime.name()); // "Portable Document Format"
}
// Pattern matching
match mime.mime() {
IMAGE_PNG | IMAGE_JPEG => println!("Image format"),
APPLICATION_PDF => println!("PDF document"),
_ => println!("Other: {} ({})", mime.name(), mime.mime()),
}
// Navigate type hierarchy
let docx = detect_file("document.docx")?;
println!("Type: {}", docx.name()); // "Word 2007+"
println!("MIME: {}", docx.mime()); // application/vnd.openxmlformats-...
if let Some(parent) = docx.parent() {
println!("Container: {}", parent.name()); // "ZIP Archive"
}
// Check type categories using MimeKind
let png_data = b"\x89PNG\r\n\x1a\n";
let mime = detect(png_data);
if mime.kind().is_image() {
println!("It's an image: {}", mime.name());
}
// Multiple kinds display with pipe separator
let jar = detect(b"PK\x03\x04...META-INF/MANIFEST.MF");
println!("Kind: {}", jar.kind()); // "ARCHIVE | APPLICATION"
println!("Name: {}", jar.name()); // "JAR"
let pdf = detect_file("document.pdf")?;
println!("MIME type: {}", pdf.mime()); // "application/pdf"
println!("MIME aliases: {:?}", pdf.aliases()); // &["application/x-pdf"]
println!("Extension aliases: {:?}", pdf.extension_aliases()); // &[".ai"]
See SUPPORTED_FORMATS.md for the complete list with MIME types and detection details.
// Core detection
detect(data: &[u8]) -> &'static MimeType
detect_file<P: AsRef<Path>>(path: P) -> io::Result<&'static MimeType>
detect_reader<R: Read>(reader: R) -> io::Result<&'static MimeType>
// MimeType methods
mime() -> &'static str // Get MIME type
name() -> &'static str // Get verbose human-readable name
extension() -> &'static str // Get primary extension
aliases() -> &'static [&'static str] // Get MIME type aliases (zero-cost)
extension_aliases() -> &'static [&'static str] // Get alternative file extensions (zero-cost)
is(expected: &str) -> bool // Check type
parent() -> Option<&'static MimeType> // Get parent type
kind() -> MimeKind // Get type category bitmask
// MimeKind methods (call on mime.kind())
is_image/video/audio/archive/document/...() // Category checks
contains(kind: MimeKind) -> bool // Check if contains kind
// Utilities
match_mime(data: &[u8], mime: &str) -> bool
equals_any(mime: &str, types: &[&str]) -> bool
is_supported(mime: &str) -> bool
Licensed under either of:
at your option.
Based on Go mimetype library.