Skip to content

Commit 7872911

Browse files
authored
Merge pull request #223 from safing/feature/mimetype-by-extension
Add MimeTypeByExtension
2 parents 5150a03 + 2c0a2b2 commit 7872911

2 files changed

Lines changed: 78 additions & 2 deletions

File tree

utils/mimetypes.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
)

utils/stablepool.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package utils
22

33
import "sync"
44

5-
// This file is forked from https://github.com/golang/go/blob/bc593eac2dc63d979a575eccb16c7369a5ff81e0/src/sync/once.go.
6-
75
// A StablePool is a drop-in replacement for sync.Pool that is slower, but
86
// predictable.
97
// A StablePool is a set of temporary objects that may be individually saved and

0 commit comments

Comments
 (0)