Crate mimetype_detector

Crate mimetype_detector 

Source
Expand description

§mimetype-detector

A comprehensive Rust library for detecting MIME types and file extensions based on magic numbers. This library provides fast, accurate, and thread-safe MIME type detection for ~450 file formats across all major categories.

§Features

  • Fast and precise MIME type detection using magic number analysis
  • 527 supported formats including images, audio, video, documents, archives, and more
  • Thread-safe operations with lazy initialization
  • Zero unsafe code - built with RwLock and LazyLock for safety
  • Memory efficient - reads only first 3KB of files
  • Zero dependencies - pure Rust implementation

§Quick Start

use mimetype_detector::{detect, detect_file};

// Detect from byte data
let data = b"\x89PNG\r\n\x1a\n";
let mime_type = detect(data);
println!("MIME type: {}", mime_type); // image/png
println!("Name: {}", mime_type.name()); // Portable Network Graphics
println!("Extension: {}", mime_type.extension()); // .png
assert!(!mime_type.name().is_empty());
println!("Kind: {}", mime_type.kind()); // IMAGE

// Detect from file
let mime_type = detect_file("test.png")?;
println!("File type: {}", mime_type);

§Architecture

The library uses a hierarchical tree structure for MIME type detection:

  • Tree-based detection: Organizes formats by priority and relationships
  • Private matchers: Detection algorithms are encapsulated and not exposed
  • Lazy initialization: MIME type tree is built on first use
  • Static lifetime: All MIME type data lives for the entire program duration

§Supported Formats

  • Images: PNG, JPEG, GIF, WebP, TIFF, BMP, SVG, DDS, PCX, KTX, ASTC, and 20+ others
  • Audio: MP3, FLAC, WAV, OGG, AAC, WavPack, TTA, DSF, DFF, and 15+ others
  • Video: MP4, WebM, AVI, MKV, MPEG, and 8+ others
  • Documents: PDF, HTML, XML, Office formats, and more
  • Archives: ZIP, TAR, 7Z, RAR, GZIP, LZ4, and 15+ others
  • Executables: ELF, PE/EXE, Mach-O, Java CLASS, WASM
  • Fonts: TTF, OTF, WOFF, WOFF2, EOT, TTC
  • 3D/CAD: Blender (.blend), PLY, GLB, GLTF, DWG, DXF
  • Network/Debug: PCAP, PCAPNG for packet capture analysis
  • And many more: See documentation for complete list

§Custom Matchers

You can register custom detection functions for additional MIME types:

use mimetype_detector::{register_mime, match_mime};

// Register a custom matcher
register_mime("application/x-custom", |data| {
    data.starts_with(b"CUSTOM")
});

// Test the custom matcher
let data = b"CUSTOM file content";
assert!(match_mime(data, "application/x-custom"));

Re-exports§

pub use mime_type::MimeType;
pub use kind::MimeKind;
pub use constants::*;

Modules§

constants
Public constants for all supported MIME types.
kind
MIME type categorization using bitflags
mime_type

Functions§

detect
Detects the MIME type of the given byte data.
detect_file
Detects the MIME type of a file at the given path.
detect_file_with_limit
Detects the MIME type of a file at the given path with a custom read limit.
detect_reader
Detects the MIME type by reading from a Read implementor.
detect_reader_with_limit
Detects the MIME type by reading from a Read implementor with a custom read limit.
detect_with_limit
Detects the MIME type of the given byte data with a custom read limit.
equals_any
Checks if a MIME type equals any of the provided types.
is_supported
Checks if a MIME type is supported by the library.
is_supported_extension
Checks if a file extension is supported by the library.
match_extension
Checks if the given data matches a specific file extension.
match_file
Checks if a file matches a specific MIME type.
match_file_extension
Checks if a file matches a specific file extension.
match_mime
Checks if the given data matches a specific MIME type.
match_reader
Checks if data from a reader matches a specific MIME type.
match_reader_extension
Checks if data from a reader matches a specific file extension.
register_extension
Registers a custom matcher function for a specific file extension.
register_mime
Registers a custom matcher function for a specific MIME type.