Centralize MIME type initialization into utils/mime_types.py

Move mimetypes.init() and all custom type registrations from server.py
and metadata_extract.py into a single init_mime_types() function called
once at startup in main.py.

Amp-Thread-ID: https://ampcode.com/threads/T-019cbb2a-513a-7458-9962-b4100e4f124d
Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
Luke Mino-Altherr 2026-03-04 15:31:08 -08:00
parent ba30d76d36
commit 788a98f294
4 changed files with 39 additions and 31 deletions

View File

@ -20,31 +20,6 @@ SAFETENSORS_EXTENSIONS = frozenset({".safetensors", ".sft"})
# Maximum safetensors header size to read (8MB) # Maximum safetensors header size to read (8MB)
MAX_SAFETENSORS_HEADER_SIZE = 8 * 1024 * 1024 MAX_SAFETENSORS_HEADER_SIZE = 8 * 1024 * 1024
def _register_custom_mime_types():
"""Register custom MIME types for model and config files.
Called before each use because mimetypes.init() in server.py resets the database.
Uses a quick check to avoid redundant registrations.
"""
# Quick check if already registered (avoids redundant add_type calls)
test_result, _ = mimetypes.guess_type("test.safetensors")
if test_result == "application/safetensors":
return
mimetypes.add_type("application/safetensors", ".safetensors")
mimetypes.add_type("application/safetensors", ".sft")
mimetypes.add_type("application/pytorch", ".pt")
mimetypes.add_type("application/pytorch", ".pth")
mimetypes.add_type("application/pickle", ".ckpt")
mimetypes.add_type("application/pickle", ".pkl")
mimetypes.add_type("application/gguf", ".gguf")
mimetypes.add_type("application/yaml", ".yaml")
mimetypes.add_type("application/yaml", ".yml")
# Register custom types at module load
_register_custom_mime_types()
@dataclass @dataclass
class ExtractedMetadata: class ExtractedMetadata:
@ -325,8 +300,6 @@ def extract_file_metadata(
_, ext = os.path.splitext(abs_path) _, ext = os.path.splitext(abs_path)
meta.format = ext.lstrip(".").lower() if ext else "" meta.format = ext.lstrip(".").lower() if ext else ""
# MIME type guess (re-register in case mimetypes.init() was called elsewhere)
_register_custom_mime_types()
mime_type, _ = mimetypes.guess_type(abs_path) mime_type, _ = mimetypes.guess_type(abs_path)
meta.content_type = mime_type meta.content_type = mime_type

View File

@ -10,6 +10,7 @@ from app.logger import setup_logger
from app.assets.seeder import asset_seeder from app.assets.seeder import asset_seeder
import itertools import itertools
import utils.extra_config import utils.extra_config
from utils.mime_types import init_mime_types
import logging import logging
import sys import sys
from comfy_execution.progress import get_progress_state from comfy_execution.progress import get_progress_state
@ -162,6 +163,7 @@ def execute_prestartup_script():
logging.info("") logging.info("")
apply_custom_paths() apply_custom_paths()
init_mime_types()
if args.enable_manager: if args.enable_manager:
comfyui_manager.prestartup() comfyui_manager.prestartup()

View File

@ -197,10 +197,6 @@ class PromptServer():
def __init__(self, loop): def __init__(self, loop):
PromptServer.instance = self PromptServer.instance = self
mimetypes.init()
mimetypes.add_type('application/javascript; charset=utf-8', '.js')
mimetypes.add_type('image/webp', '.webp')
self.user_manager = UserManager() self.user_manager = UserManager()
self.model_file_manager = ModelFileManager() self.model_file_manager = ModelFileManager()
self.custom_node_manager = CustomNodeManager() self.custom_node_manager = CustomNodeManager()

37
utils/mime_types.py Normal file
View File

@ -0,0 +1,37 @@
"""Centralized MIME type initialization.
Call init_mime_types() once at startup to initialize the MIME type database
and register all custom types used across ComfyUI.
"""
import mimetypes
_initialized = False
def init_mime_types():
"""Initialize the MIME type database and register all custom types.
Safe to call multiple times; only runs once.
"""
global _initialized
if _initialized:
return
_initialized = True
mimetypes.init()
# Web types (used by server.py for static file serving)
mimetypes.add_type('application/javascript; charset=utf-8', '.js')
mimetypes.add_type('image/webp', '.webp')
# Model and data file types (used by asset scanning / metadata extraction)
mimetypes.add_type("application/safetensors", ".safetensors")
mimetypes.add_type("application/safetensors", ".sft")
mimetypes.add_type("application/pytorch", ".pt")
mimetypes.add_type("application/pytorch", ".pth")
mimetypes.add_type("application/pickle", ".ckpt")
mimetypes.add_type("application/pickle", ".pkl")
mimetypes.add_type("application/gguf", ".gguf")
mimetypes.add_type("application/yaml", ".yaml")
mimetypes.add_type("application/yaml", ".yml")