Remove dependence on OS for mimetypes

This commit is contained in:
Daniel 2021-04-07 16:44:55 +02:00
parent 9ff824967e
commit 83ffd04d18

View file

@ -3,7 +3,6 @@ package ui
import (
"fmt"
"io"
"mime"
"net/http"
"net/url"
"path/filepath"
@ -143,7 +142,7 @@ func ServeFileFromBundle(w http.ResponseWriter, r *http.Request, bundleName stri
// set content type
_, ok := w.Header()["Content-Type"]
if !ok {
contentType := mime.TypeByExtension(filepath.Ext(path))
contentType := mimeTypeByExtension(filepath.Ext(path))
if contentType != "" {
w.Header().Set("Content-Type", contentType)
}
@ -179,3 +178,72 @@ func redirectToDefault(w http.ResponseWriter, r *http.Request) {
func redirAddSlash(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, r.RequestURI+"/", http.StatusPermanentRedirect)
}
// We now do our mimetypes ourselves, because, as far as we analyzed, a Windows
// update screwed us over here and broke all the mime typing.
// (April 2021)
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",
".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",
".zip": "application/zip",
}
)
func mimeTypeByExtension(ext string) string {
mimeType, ok := mimeTypes[ext]
if ok {
return mimeType
}
return defaultMimeType
}