| 1 | module mime |
| 2 | |
| 3 | pub struct MimeType { |
| 4 | source string |
| 5 | extensions []string |
| 6 | compressible bool |
| 7 | charset string |
| 8 | } |
| 9 | |
| 10 | // returns a `MimeType` for the given MIME type |
| 11 | pub fn get_complete_mime_type(mt string) MimeType { |
| 12 | return db[mt] |
| 13 | } |
| 14 | |
| 15 | // returns the MIME type for the given file extension |
| 16 | pub fn get_mime_type(ext string) string { |
| 17 | return ext_to_mt_str[ext] |
| 18 | } |
| 19 | |
| 20 | // returns a `content-type` header ready to use for the given MIME type |
| 21 | pub fn get_content_type(mt string) string { |
| 22 | mt_struct := db[mt] |
| 23 | charset := if mt_struct.charset.len > 0 { mt_struct.charset.to_lower() } else { 'utf-8' } |
| 24 | return '${mt}; charset=${charset}' |
| 25 | } |
| 26 | |
| 27 | // returns the default extension for the given MIME type |
| 28 | pub fn get_default_ext(mt string) string { |
| 29 | return if db[mt].extensions.len > 0 { |
| 30 | db[mt].extensions[0] |
| 31 | } else { |
| 32 | '' |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | // returns true if the given MIME type exists |
| 37 | pub fn exists(mt string) bool { |
| 38 | return mt in db |
| 39 | } |
| 40 | |