A Rust library for determining MIME types based on file extensions.
This library provides a fast and efficient way to look up MIME types for common file extensions. It uses a pre-generated database built from multiple authoritative sources including:
- jshttp/mime-db npm package
- Apache mime.types
- NGINX mime.types
- Fast lookup - uses a pre-generated perfect hash for O(1) lookup time
- Comprehensive coverage - supports hundreds of file extensions
- Static data - no runtime dependencies, zero allocations
- Cross-platform - works on any platform that supports Rust
Add this to your Cargo.toml:
[dependencies]
multi-mime-guess = "0.1.0"use multi_mime_guess::lookup;
fn main() {
// Lookup MIME type for a file extension
let mime_type = lookup("html");
println!("{:?}", mime_type); // Some("text/html")
// Lookup with case-insensitive extension
let mime_type = lookup("HTML");
println!("{:?}", mime_type); // Some("text/html")
// Lookup with dot prefix
let mime_type = lookup(".html");
println!("{:?}", mime_type); // Some("text/html")
// Handle unknown extensions
let mime_type = lookup("unknown");
println!("{:?}", mime_type); // None
}fn get_content_type(file_extension: &str) -> &'static str {
lookup(file_extension)
.unwrap_or("application/octet-stream")
}
fn serve_file(extension: &str) -> Result<(), Box<dyn std::error::Error>> {
let mime_type = get_content_type(extension);
// Use mime_type for Content-Type header
// Ok(())
}The MIME type database is generated from authoritative sources and stored in the dbs/ directory. You can regenerate the database by running:
# Fetch and generate databases
python fetch/fetch_apache.py
python fetch/fetch_mime_db.py
python fetch/fetch_nginx.pyThe build process automatically generates the perfect hash database using build.rs.
This project uses automated version bumping when MIME databases are updated. The GitHub Actions workflow will:
- Fetch updated MIME databases from all sources
- Detect if any changes were made
- If changes detected, bump the patch version in
Cargo.toml - Create a git commit and push a new tag
See the GitHub Actions workflow for details.
This project is licensed under the MIT License - see the LICENSE file for details.
MIME type definitions sourced from: