Skip to content

Commit

Permalink
Add MimeTypeByExtension
Browse files Browse the repository at this point in the history
  • Loading branch information
dhaavi committed Oct 11, 2023
1 parent 5150a03 commit 2c0a2b2
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
78 changes: 78 additions & 0 deletions utils/mimetypes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package utils

import "strings"

// Do not depend on the OS for mimetypes.
// A Windows update screwed us over here and broke all the automatic mime
// typing via Go in April 2021.

// MimeTypeByExtension returns a mimetype for the given file name extension,
// which must including the leading dot.
// If the extension is not known, the call returns with ok=false and,
// additionally, a default "application/octet-stream" mime type is returned.
func MimeTypeByExtension(ext string) (mimeType string, ok bool) {
mimeType, ok = mimeTypes[strings.ToLower(ext)]
if ok {
return
}

return defaultMimeType, false
}

var (
defaultMimeType = "application/octet-stream"

mimeTypes = map[string]string{
".7z": "application/x-7z-compressed",
".atom": "application/atom+xml",
".css": "text/css; charset=utf-8",
".csv": "text/csv; charset=utf-8",
".deb": "application/x-debian-package",
".epub": "application/epub+zip",
".es": "application/ecmascript",
".flv": "video/x-flv",
".gif": "image/gif",
".gz": "application/gzip",
".htm": "text/html; charset=utf-8",
".html": "text/html; charset=utf-8",
".jpeg": "image/jpeg",
".jpg": "image/jpeg",
".js": "text/javascript; charset=utf-8",
".json": "application/json; charset=utf-8",
".m3u": "audio/mpegurl",
".m4a": "audio/mpeg",
".md": "text/markdown; charset=utf-8",
".mjs": "text/javascript; charset=utf-8",
".mov": "video/quicktime",
".mp3": "audio/mpeg",
".mp4": "video/mp4",
".mpeg": "video/mpeg",
".mpg": "video/mpeg",
".ogg": "audio/ogg",
".ogv": "video/ogg",
".otf": "font/otf",
".pdf": "application/pdf",
".png": "image/png",
".qt": "video/quicktime",
".rar": "application/rar",
".rtf": "application/rtf",
".svg": "image/svg+xml",
".tar": "application/x-tar",
".tiff": "image/tiff",
".ts": "video/MP2T",
".ttc": "font/collection",
".ttf": "font/ttf",
".txt": "text/plain; charset=utf-8",
".wasm": "application/wasm",
".wav": "audio/x-wav",
".webm": "video/webm",
".webp": "image/webp",
".woff": "font/woff",
".woff2": "font/woff2",
".xml": "text/xml; charset=utf-8",
".xz": "application/x-xz",
".yaml": "application/yaml; charset=utf-8",
".yml": "application/yaml; charset=utf-8",
".zip": "application/zip",
}
)
2 changes: 0 additions & 2 deletions utils/stablepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package utils

import "sync"

// This file is forked from https://github.com/golang/go/blob/bc593eac2dc63d979a575eccb16c7369a5ff81e0/src/sync/once.go.

// A StablePool is a drop-in replacement for sync.Pool that is slower, but
// predictable.
// A StablePool is a set of temporary objects that may be individually saved and
Expand Down

0 comments on commit 2c0a2b2

Please sign in to comment.