feat: add patch_mimetype function for consistent mimetype handling

This commit is contained in:
2026-07-06 22:41:01 +08:00
parent 0ca581f1e6
commit bf3cbeaaba
2 changed files with 23 additions and 1 deletions
+2 -1
View File
@@ -51,7 +51,7 @@ from module.webui.fake import (
) )
from module.webui.fastapi import asgi_app from module.webui.fastapi import asgi_app
from module.webui.lang import _t, t from module.webui.lang import _t, t
from module.webui.patch import patch_executor from module.webui.patch import patch_executor, patch_mimetype
from module.webui.pin import put_input, put_select from module.webui.pin import put_input, put_select
from module.webui.process_manager import ProcessManager from module.webui.process_manager import ProcessManager
from module.webui.remote_access import RemoteAccess from module.webui.remote_access import RemoteAccess
@@ -83,6 +83,7 @@ from module.webui.widgets import (
) )
patch_executor() patch_executor()
patch_mimetype()
task_handler = TaskHandler() task_handler = TaskHandler()
+21
View File
@@ -39,3 +39,24 @@ def patch_executor():
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
loop.set_default_executor(CachedThreadPoolExecutor.executor) loop.set_default_executor(CachedThreadPoolExecutor.executor)
def patch_mimetype():
"""
Patch mimetype db to use the builtin table instead of reading from environment.
By default, mimetype reads user configured mimetype table from environment. It's good for server but bad on our
side, because we deploy on user's machine which may have polluted environment. To have a consistent behaviour on
all deployment, we use the builtin mimetype table only.
"""
import mimetypes
# lock as inited
mimetypes.inited = True
# create a new clean instance
db = mimetypes.MimeTypes(filenames=())
mimetypes._db = db
# override global variable
mimetypes.encodings_map = db.encodings_map
mimetypes.suffix_map = db.suffix_map
mimetypes.types_map = db.types_map[True]
mimetypes.common_types = db.types_map[False]