Files
dify/api/dify_graph/file/file_factory.py
-LAN- 56593f20b0 refactor(api): continue decoupling dify_graph from API concerns (#33580)
Signed-off-by: -LAN- <laipz8200@outlook.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: WH-2099 <wh2099@pm.me>
2026-03-25 20:32:24 +08:00

40 lines
1.3 KiB
Python

from .constants import AUDIO_EXTENSIONS, DOCUMENT_EXTENSIONS, IMAGE_EXTENSIONS, VIDEO_EXTENSIONS
from .enums import FileType
def standardize_file_type(*, extension: str = "", mime_type: str = "") -> FileType:
"""
Infer the actual file type from extension and mime type.
"""
guessed_type = None
if extension:
guessed_type = _get_file_type_by_extension(extension)
if guessed_type is None and mime_type:
guessed_type = get_file_type_by_mime_type(mime_type)
return guessed_type or FileType.CUSTOM
def _get_file_type_by_extension(extension: str) -> FileType | None:
normalized_extension = extension.lstrip(".")
if normalized_extension in IMAGE_EXTENSIONS:
return FileType.IMAGE
if normalized_extension in VIDEO_EXTENSIONS:
return FileType.VIDEO
if normalized_extension in AUDIO_EXTENSIONS:
return FileType.AUDIO
if normalized_extension in DOCUMENT_EXTENSIONS:
return FileType.DOCUMENT
return None
def get_file_type_by_mime_type(mime_type: str) -> FileType:
if "image" in mime_type:
return FileType.IMAGE
if "video" in mime_type:
return FileType.VIDEO
if "audio" in mime_type:
return FileType.AUDIO
if "text" in mime_type or "pdf" in mime_type:
return FileType.DOCUMENT
return FileType.CUSTOM