Hard-code more mime types

If I had a dollar for all the weird and wonderful ways Windows systems
can be broken, I'd be a very rich man.

https://forums.ankiweb.net/t/the-gear-icon-in-the-anki-interface-is-not-displaying-properly/66274
This commit is contained in:
Damien Elmes 2025-09-23 17:59:47 +10:00
parent fb332c4fe1
commit 0d31c6de4a

View file

@ -170,13 +170,42 @@ def favicon() -> Response:
def _mime_for_path(path: str) -> str:
"Mime type for provided path/filename."
if path.endswith(".css"):
# some users may have invalid mime type in the Windows registry
return "text/css"
elif path.endswith(".js") or path.endswith(".mjs"):
return "application/javascript"
_, ext = os.path.splitext(path)
ext = ext.lower()
# Badly-behaved apps on Windows can alter the standard mime types in the registry, which can completely
# break Anki's UI. So we hard-code the most common extensions.
mime_types = {
".css": "text/css",
".js": "application/javascript",
".mjs": "application/javascript",
".html": "text/html",
".htm": "text/html",
".svg": "image/svg+xml",
".png": "image/png",
".jpg": "image/jpeg",
".jpeg": "image/jpeg",
".gif": "image/gif",
".webp": "image/webp",
".ico": "image/x-icon",
".json": "application/json",
".woff": "font/woff",
".woff2": "font/woff2",
".ttf": "font/ttf",
".otf": "font/otf",
".mp3": "audio/mpeg",
".mp4": "video/mp4",
".webm": "video/webm",
".ogg": "audio/ogg",
".pdf": "application/pdf",
".txt": "text/plain",
}
if mime := mime_types.get(ext):
return mime
else:
# autodetect
# fallback to mimetypes, which may consult the registry
mime, _encoding = mimetypes.guess_type(path)
return mime or "application/octet-stream"