multi-mime-guess 0.1.0

A Rust library for determining MIME types based on file extensions.
Documentation

multi-mime-guess

A Rust library for determining MIME types based on file extensions.

Overview

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:

Features

  • 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

Installation

Add this to your Cargo.toml:

[dependencies]
multi-mime-guess = "0.1.0"

Usage

Basic lookup

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
}

In your application

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(())
}

Building the database

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.py

The build process automatically generates the perfect hash database using build.rs.

Version bumping

This project uses automated version bumping when MIME databases are updated. The GitHub Actions workflow will:

  1. Fetch updated MIME databases from all sources
  2. Detect if any changes were made
  3. If changes detected, bump the patch version in Cargo.toml
  4. Create a git commit and push a new tag

See the GitHub Actions workflow for details.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

MIME type definitions sourced from: