|
| 1 | +package utils |
| 2 | + |
| 3 | +import "strings" |
| 4 | + |
| 5 | +// Do not depend on the OS for mimetypes. |
| 6 | +// A Windows update screwed us over here and broke all the automatic mime |
| 7 | +// typing via Go in April 2021. |
| 8 | + |
| 9 | +// MimeTypeByExtension returns a mimetype for the given file name extension, |
| 10 | +// which must including the leading dot. |
| 11 | +// If the extension is not known, the call returns with ok=false and, |
| 12 | +// additionally, a default "application/octet-stream" mime type is returned. |
| 13 | +func MimeTypeByExtension(ext string) (mimeType string, ok bool) { |
| 14 | + mimeType, ok = mimeTypes[strings.ToLower(ext)] |
| 15 | + if ok { |
| 16 | + return |
| 17 | + } |
| 18 | + |
| 19 | + return defaultMimeType, false |
| 20 | +} |
| 21 | + |
| 22 | +var ( |
| 23 | + defaultMimeType = "application/octet-stream" |
| 24 | + |
| 25 | + mimeTypes = map[string]string{ |
| 26 | + ".7z": "application/x-7z-compressed", |
| 27 | + ".atom": "application/atom+xml", |
| 28 | + ".css": "text/css; charset=utf-8", |
| 29 | + ".csv": "text/csv; charset=utf-8", |
| 30 | + ".deb": "application/x-debian-package", |
| 31 | + ".epub": "application/epub+zip", |
| 32 | + ".es": "application/ecmascript", |
| 33 | + ".flv": "video/x-flv", |
| 34 | + ".gif": "image/gif", |
| 35 | + ".gz": "application/gzip", |
| 36 | + ".htm": "text/html; charset=utf-8", |
| 37 | + ".html": "text/html; charset=utf-8", |
| 38 | + ".jpeg": "image/jpeg", |
| 39 | + ".jpg": "image/jpeg", |
| 40 | + ".js": "text/javascript; charset=utf-8", |
| 41 | + ".json": "application/json; charset=utf-8", |
| 42 | + ".m3u": "audio/mpegurl", |
| 43 | + ".m4a": "audio/mpeg", |
| 44 | + ".md": "text/markdown; charset=utf-8", |
| 45 | + ".mjs": "text/javascript; charset=utf-8", |
| 46 | + ".mov": "video/quicktime", |
| 47 | + ".mp3": "audio/mpeg", |
| 48 | + ".mp4": "video/mp4", |
| 49 | + ".mpeg": "video/mpeg", |
| 50 | + ".mpg": "video/mpeg", |
| 51 | + ".ogg": "audio/ogg", |
| 52 | + ".ogv": "video/ogg", |
| 53 | + ".otf": "font/otf", |
| 54 | + ".pdf": "application/pdf", |
| 55 | + ".png": "image/png", |
| 56 | + ".qt": "video/quicktime", |
| 57 | + ".rar": "application/rar", |
| 58 | + ".rtf": "application/rtf", |
| 59 | + ".svg": "image/svg+xml", |
| 60 | + ".tar": "application/x-tar", |
| 61 | + ".tiff": "image/tiff", |
| 62 | + ".ts": "video/MP2T", |
| 63 | + ".ttc": "font/collection", |
| 64 | + ".ttf": "font/ttf", |
| 65 | + ".txt": "text/plain; charset=utf-8", |
| 66 | + ".wasm": "application/wasm", |
| 67 | + ".wav": "audio/x-wav", |
| 68 | + ".webm": "video/webm", |
| 69 | + ".webp": "image/webp", |
| 70 | + ".woff": "font/woff", |
| 71 | + ".woff2": "font/woff2", |
| 72 | + ".xml": "text/xml; charset=utf-8", |
| 73 | + ".xz": "application/x-xz", |
| 74 | + ".yaml": "application/yaml; charset=utf-8", |
| 75 | + ".yml": "application/yaml; charset=utf-8", |
| 76 | + ".zip": "application/zip", |
| 77 | + } |
| 78 | +) |
0 commit comments