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
+21
View File
@@ -39,3 +39,24 @@ def patch_executor():
loop = asyncio.get_event_loop()
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]