milstian_internet_framework/
mime.rs

1//! # Handling MIME types
2
3/// Determine MIME based on filename.
4// # Example
5/// ```rust
6/// use milstian_internet_framework::mime;
7/// let mime_type = mime::from_filename("random.aac");
8/// assert_eq!("audio/aac", mime_type);
9/// ```
10// @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Complete_list_of_MIME_types
11pub fn from_filename(filename: &str) -> String {
12    let mut mime = "application/octet-stream";
13    let parts: Vec<&str> = filename.rsplitn(2, ".").collect();
14    if !parts.is_empty() {
15        if let Some(extension) = parts.get(0) {
16            let extension: String = extension.to_lowercase();
17            mime = match extension.as_ref() {
18                "aac" => "audio/aac",
19                "abw" => "application/x-abiword",
20                "avi" => "video/x-msvideo",
21                "azw" => "application/vnd.amazon.ebook",
22                "bmp" => "image/bmp",
23                "bz" => "application/x-bzip",
24                "bz2" => "application/x-bzip2",
25                "csh" => "application/x-csh",
26                "css" => "text/css",
27                "csv" => "text/csv",
28                "doc" => "application/msword",
29                "docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
30                "eot" => "application/vnd.ms-fontobject",
31                "epub" => "application/epub+zip",
32                "es" => "application/ecmascript",
33                "gif" => "image/gif",
34                "html" => "text/html",
35                "htm" => "text/html",
36                "ico" => "image/x-icon",
37                "ics" => "text/calendar",
38                "jar" => "application/java-archive",
39                "jpeg" => "image/jpeg",
40                "jpg" => "image/jpg",
41                "js" => "application/javascript",
42                "json" => "application/json",
43                "mid" => "audo/midi",
44                "midi" => "audo/midi",
45                "mpeg" => "video/mpeg",
46                "mpkg" => "application/vnd.apple.installer+xml",
47                "odp" => "application/vnd.oasis.opendocument.presentation",
48                "ods" => "application/vnd.oasis.opendocument.spreadsheet",
49                "odt" => "application/vnd.oasis.opendocument.text",
50                "oga" => "audio/ogg",
51                "ogv" => "video/ogg",
52                "ogx" => "application/ogg",
53                "otf" => "font/otf",
54                "png" => "image/png",
55                "pdf" => "application/pdf",
56                "ppt" => "application/vnd.ms-powerpoint",
57                "pptx" => {
58                    "application/vnd.openxmlformats-officedocument.presentationml.presentation"
59                }
60                "rar" => "application/x-rar-compressed",
61                "rtf" => "application/rtf",
62                "sh" => "application/x-sh",
63                "svg" => "image/svg+xml",
64                "swf" => "application/x-shockwave-flash",
65                "tar" => "application/x-tar",
66                "tif" => "image/tiff",
67                "tiff" => "image/tiff",
68                "ts" => "application/typescript",
69                "ttf" => "font/ttf",
70                "txt" => "text/plain",
71                "vsd" => "application/vnd.visio",
72                "wav" => "audio/wav",
73                "weba" => "audio/webm",
74                "webm" => "video/webm",
75                "webp" => "image/webp",
76                "woff" => "font/woff",
77                "woff2" => "font/woff2",
78                "xhtml" => "application/xhtml+xml",
79                "xls" => "application/vnd.ms-excel",
80                "xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
81                "xml" => "application/xml",
82                "xul" => "application/vnd.mozilla.xul+xml",
83                "zip" => "application/zip",
84                "3gp" => "video/3gpp",
85                "3g2" => "video/3gpp2",
86                "7z" => "application/x-7z-compressed",
87                _ => "application/octet-stream",
88            };
89        }
90    }
91    mime.to_string()
92}
93
94#[cfg(test)]
95mod tests
96{
97    use super::*;
98
99    #[test]
100    fn test_from_filename()
101    {
102        assert_eq!(
103            from_filename("random.webp"),
104            "image/webp"
105        );
106        assert_eq!(
107            from_filename("random.random"),
108            "application/octet-stream"
109        );
110    }
111}