minimime 1.0.0

A minimal MIME type detection library for Rust, ported from the Ruby minimime gem
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//! # minimime
//!
//! A minimal MIME type detection library for Rust, ported from the Ruby [minimime](https://github.com/discourse/mini_mime) gem.
//!
//! This library provides fast MIME type detection based on file extensions and content types,
//! using embedded database files for efficient lookups without external dependencies.
//!
//! ## Features
//!
//! - **Fast lookups**: Uses embedded hash maps for average O(1) MIME type detection
//! - **No external dependencies**: Database files are embedded at compile time
//! - **Case insensitive**: Handles file extensions in any case
//! - **Binary detection**: Identifies binary vs text file types
//! - **Thread safe**: Safe for concurrent use across multiple threads
//!
//! ## Quick Start
//!
//! ```rust
//! use minimime::{lookup_by_filename, lookup_by_extension, lookup_by_content_type};
//!
//! // Look up by filename
//! if let Some(info) = lookup_by_filename("document.pdf") {
//!     println!("MIME type: {}", info.content_type); // "application/pdf"
//!     println!("Is binary: {}", info.is_binary());  // true
//! }
//!
//! // Look up by extension
//! if let Some(info) = lookup_by_extension("json") {
//!     println!("MIME type: {}", info.content_type); // "application/json"
//! }
//!
//! // Look up by content type
//! if let Some(info) = lookup_by_content_type("text/css") {
//!     println!("Extension: {}", info.extension); // "css"
//! }
//! ```
//!
//! ## Supported File Types
//!
//! This library supports hundreds of file extensions and MIME types, including:
//! - Web formats (HTML, CSS, JS, JSON, XML)
//! - Images (PNG, JPEG, GIF, SVG, WebP)
//! - Documents (PDF, DOC, XLS, PPT)
//! - Archives (ZIP, TAR, GZ)
//! - Media files (MP3, MP4, AVI, MOV)
//! - And many more...

use std::{
    collections::HashMap,
    path::Path,
    sync::{Mutex, OnceLock},
};

/// MIME type information including extension, content type, and encoding.
///
/// This struct contains all the information about a specific MIME type,
/// including whether it's a binary or text format.
#[derive(Debug, Clone, PartialEq)]
pub struct Info {
    /// File extension (without the dot)
    pub extension: String,
    /// MIME content type (e.g., "text/plain", "image/png")
    pub content_type: String,
    /// Encoding type (e.g., "8bit", "base64")
    pub encoding: String,
}

impl Info {
    /// Encodings that indicate binary file types
    const BINARY_ENCODINGS: &'static [&'static str] = &["base64", "8bit"];

    /// Creates a new `Info` instance from a database line.
    ///
    /// The line format is: `extension content_type encoding`
    ///
    /// # Arguments
    ///
    /// * `line` - A whitespace-separated string containing extension, content type, and encoding
    ///
    /// # Returns
    ///
    /// * `Some(Info)` if the line is valid
    /// * `None` if the line doesn't have at least 3 parts
    ///
    /// # Examples
    ///
    /// ```
    /// use minimime::Info;
    ///
    /// let info = Info::new("pdf application/pdf base64").unwrap();
    /// assert_eq!(info.extension, "pdf");
    /// assert_eq!(info.content_type, "application/pdf");
    /// assert_eq!(info.encoding, "base64");
    /// ```
    pub fn new(line: &str) -> Option<Self> {
        let parts: Vec<&str> = line.split_whitespace().collect();
        if parts.len() >= 3 {
            Some(Info {
                extension: parts[0].to_string(),
                content_type: parts[1].to_string(),
                encoding: parts[2].to_string(),
            })
        } else {
            None
        }
    }

    /// Determines if this MIME type represents a binary file format.
    ///
    /// Binary files are those that use "base64" or "8bit" encoding.
    ///
    /// # Returns
    ///
    /// * `true` if the file type is binary
    /// * `false` if the file type is text-based
    ///
    /// # Examples
    ///
    /// ```
    /// use minimime::Info;
    ///
    /// let pdf = Info::new("pdf application/pdf base64").unwrap();
    /// assert!(pdf.is_binary());
    ///
    /// let txt = Info::new("txt text/plain 7bit").unwrap();
    /// assert!(!txt.is_binary());
    /// ```
    pub fn is_binary(&self) -> bool {
        Self::BINARY_ENCODINGS.contains(&self.encoding.as_str())
    }
}

/// Internal database for MIME type lookups.
///
/// This struct manages the hash maps used for fast MIME type lookups
/// by file extension and content type.
pub struct Db {
    ext_db: HashMap<String, Info>,
    content_type_db: HashMap<String, Info>,
}

impl Db {
    /// Creates a new database instance and loads the embedded data files.
    pub fn new() -> Result<Self, Box<dyn std::error::Error>> {
        let mut db = Db {
            ext_db: HashMap::new(),
            content_type_db: HashMap::new(),
        };

        // Load extension database
        db.load_ext_db()?;
        // Load content type database
        db.load_content_type_db()?;

        Ok(db)
    }

    /// Loads the file extension to MIME type database.
    ///
    /// This method reads the embedded `ext_mime.db` file and populates
    /// the extension lookup hash map.
    fn load_ext_db(&mut self) -> Result<(), Box<dyn std::error::Error>> {
        let db_content = include_str!("db/ext_mime.db");
        for line in db_content.lines() {
            if let Some(info) = Info::new(line) {
                self.ext_db.insert(info.extension.clone(), info);
            }
        }
        Ok(())
    }

    /// Loads the content type to MIME type database.
    ///
    /// This method reads the embedded `content_type_mime.db` file and populates
    /// the content type lookup hash map.
    fn load_content_type_db(&mut self) -> Result<(), Box<dyn std::error::Error>> {
        let db_content = include_str!("db/content_type_mime.db");
        for line in db_content.lines() {
            if let Some(info) = Info::new(line) {
                self.content_type_db.insert(info.content_type.clone(), info);
            }
        }
        Ok(())
    }

    /// Looks up MIME information by file extension.
    ///
    /// The lookup is case-insensitive, trying the exact extension first,
    /// then falling back to lowercase.
    ///
    /// # Arguments
    ///
    /// * `extension` - File extension (with or without leading dot)
    ///
    /// # Returns
    ///
    /// * `Some(&Info)` if the extension is found
    /// * `None` if the extension is not recognized
    pub fn lookup_by_extension(&self, extension: &str) -> Option<&Info> {
        self.ext_db
            .get(extension)
            .or_else(|| self.ext_db.get(&extension.to_lowercase()))
    }

    /// Looks up MIME information by content type.
    ///
    /// # Arguments
    ///
    /// * `content_type` - MIME content type (e.g., "text/plain")
    ///
    /// # Returns
    ///
    /// * `Some(&Info)` if the content type is found
    /// * `None` if the content type is not recognized
    pub fn lookup_by_content_type(&self, content_type: &str) -> Option<&Info> {
        self.content_type_db.get(content_type)
    }

    /// Looks up MIME information by filename.
    ///
    /// Extracts the file extension from the filename and performs a lookup.
    /// The lookup is case-insensitive.
    ///
    /// # Arguments
    ///
    /// * `filename` - Full filename or path
    ///
    /// # Returns
    ///
    /// * `Some(&Info)` if the file extension is recognized
    /// * `None` if the file has no extension or the extension is not recognized
    pub fn lookup_by_filename(&self, filename: &str) -> Option<&Info> {
        let path = Path::new(filename);
        if let Some(ext) = path.extension() {
            if let Some(ext_str) = ext.to_str() {
                return self.lookup_by_extension(ext_str);
            }
        }
        None
    }
}

// Global database instance
static DB: OnceLock<Mutex<Db>> = OnceLock::new();

/// Gets the global database instance.
///
/// This function initializes the database on first access and returns
/// a reference to the global singleton instance.
///
/// # Returns
///
/// A reference to the global `Mutex<Db>` instance
///
/// # Panics
///
/// Panics if the database fails to initialize
fn get_db() -> &'static Mutex<Db> {
    DB.get_or_init(|| Mutex::new(Db::new().expect("Failed to initialize MIME database")))
}

/// Looks up MIME information by filename.
///
/// This is a convenience function that uses the global database instance
/// to perform the lookup. The lookup is case-insensitive.
///
/// # Arguments
///
/// * `filename` - Full filename or path
///
/// # Returns
///
/// * `Some(Info)` if the file extension is recognized
/// * `None` if the file has no extension or the extension is not recognized
///
/// # Examples
///
/// ```
/// use minimime::lookup_by_filename;
///
/// if let Some(info) = lookup_by_filename("document.pdf") {
///     println!("MIME type: {}", info.content_type);
///     println!("Is binary: {}", info.is_binary());
/// }
/// ```
pub fn lookup_by_filename(filename: &str) -> Option<Info> {
    let db = get_db().lock().unwrap();
    db.lookup_by_filename(filename).cloned()
}

/// Looks up MIME information by file extension.
///
/// This is a convenience function that uses the global database instance
/// to perform the lookup. The lookup is case-insensitive.
///
/// # Arguments
///
/// * `extension` - File extension (with or without leading dot)
///
/// # Returns
///
/// * `Some(Info)` if the extension is found
/// * `None` if the extension is not recognized
///
/// # Examples
///
/// ```
/// use minimime::lookup_by_extension;
///
/// if let Some(info) = lookup_by_extension("pdf") {
///     println!("MIME type: {}", info.content_type);
///     println!("Encoding: {}", info.encoding);
/// }
/// ```
pub fn lookup_by_extension(extension: &str) -> Option<Info> {
    let db = get_db().lock().unwrap();
    db.lookup_by_extension(extension).cloned()
}

/// Looks up MIME information by content type.
///
/// This is a convenience function that uses the global database instance
/// to perform the lookup.
///
/// # Arguments
///
/// * `content_type` - MIME content type (e.g., "text/plain")
///
/// # Returns
///
/// * `Some(Info)` if the content type is found
/// * `None` if the content type is not recognized
///
/// # Examples
///
/// ```
/// use minimime::lookup_by_content_type;
///
/// if let Some(info) = lookup_by_content_type("application/pdf") {
///     println!("Extension: {}", info.extension);
///     println!("Is binary: {}", info.is_binary());
/// }
/// ```
pub fn lookup_by_content_type(content_type: &str) -> Option<Info> {
    let db = get_db().lock().unwrap();
    db.lookup_by_content_type(content_type).cloned()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_info_creation() {
        let info = Info::new("pdf application/pdf base64").unwrap();
        assert_eq!(info.extension, "pdf");
        assert_eq!(info.content_type, "application/pdf");
        assert_eq!(info.encoding, "base64");
        assert!(info.is_binary());
    }

    #[test]
    fn test_extension() {
        if let Some(info) = lookup_by_extension("zip") {
            assert_eq!(info.content_type, "application/zip");
        }
    }

    #[test]
    fn test_mixed_case() {
        // Test case insensitive lookups
        if let Some(info) = lookup_by_filename("a.GTM") {
            assert_eq!(info.content_type, "application/vnd.groove-tool-message");
        }
        if let Some(info) = lookup_by_extension("ZiP") {
            assert_eq!(info.content_type, "application/zip");
        }
    }

    #[test]
    fn test_content_type_lookups() {
        // Test various file extensions
        if let Some(info) = lookup_by_filename("a.123") {
            assert_eq!(info.content_type, "application/vnd.lotus-1-2-3");
        }
        if let Some(info) = lookup_by_filename("a.Z") {
            assert_eq!(info.content_type, "application/x-compressed");
        }
        if let Some(info) = lookup_by_filename("a.gtm") {
            assert_eq!(info.content_type, "application/vnd.groove-tool-message");
        }
        if let Some(info) = lookup_by_filename("a.zmm") {
            assert_eq!(
                info.content_type,
                "application/vnd.HandHeld-Entertainment+xml"
            );
        }
        if let Some(info) = lookup_by_filename("x.csv") {
            assert_eq!(info.content_type, "text/csv");
        }
        if let Some(info) = lookup_by_filename("x.mda") {
            assert_eq!(info.content_type, "application/x-msaccess");
        }

        // Test unknown extension
        assert!(lookup_by_filename("a.frog").is_none());
    }

    #[test]
    fn test_binary() {
        // Test binary detection
        if let Some(info) = lookup_by_filename("a.z") {
            assert!(info.is_binary());
        }
        if let Some(info) = lookup_by_filename("a.Z") {
            assert!(info.is_binary());
        }
        if let Some(info) = lookup_by_filename("a.txt") {
            assert!(!info.is_binary());
        }
        assert!(lookup_by_filename("a.frog").is_none());
    }

    #[test]
    fn test_binary_content_type() {
        if let Some(info) = lookup_by_content_type("application/x-compressed") {
            assert!(info.is_binary());
        }
        assert!(lookup_by_content_type("something-fake").is_none());
        if let Some(info) = lookup_by_content_type("text/plain") {
            assert!(!info.is_binary());
        }
    }

    #[test]
    fn test_prioritize_extensions_correctly() {
        if let Some(info) = lookup_by_content_type("text/plain") {
            assert_eq!(info.extension, "txt");
        }
    }

    #[test]
    fn test_lookup_by_filename() {
        if let Some(info) = lookup_by_filename("document.pdf") {
            assert_eq!(info.content_type, "application/pdf");
        }
    }

    #[test]
    fn test_lookup_by_extension() {
        if let Some(info) = lookup_by_extension("pdf") {
            assert_eq!(info.content_type, "application/pdf");
        }
    }
}