From 797507d24b91e27a95186c15b808776312f83667 Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Wed, 21 Jan 2026 15:01:22 +0800 Subject: [PATCH] =?UTF-8?q?feat(storage):=20=E6=B7=BB=E5=8A=A0=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E4=B8=8A=E4=BC=A0=E7=9A=84=20Content-Type=20=E6=A3=80?= =?UTF-8?q?=E6=B5=8B=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加文件扩展名到 Content-Type 的映射表 - 实现根据文件扩展名获取对应 Content-Type 的函数 - 将上传日志中的调试信息改为信息级别并显示 Content-Type - 使用正确的 Content-Type 替换默认的 application/octet-stream - 支持 mp --- services/storage.py | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/services/storage.py b/services/storage.py index ca31313..6cd9b22 100644 --- a/services/storage.py +++ b/services/storage.py @@ -15,6 +15,29 @@ import requests logger = logging.getLogger(__name__) +# 文件扩展名到 Content-Type 的映射 +_CONTENT_TYPE_MAP = { + '.mp4': 'video/mp4', + '.aac': 'audio/aac', + '.ts': 'video/mp2t', + '.m4a': 'audio/mp4', +} + + +def _get_content_type(file_path: str) -> str: + """ + 根据文件扩展名获取 Content-Type + + Args: + file_path: 文件路径 + + Returns: + Content-Type 字符串 + """ + ext = os.path.splitext(file_path)[1].lower() + return _CONTENT_TYPE_MAP.get(ext, 'application/octet-stream') + + def _apply_http_replace_map(url: str) -> str: """ 应用 HTTP_REPLACE_MAP 环境变量替换 URL @@ -69,7 +92,8 @@ def upload_file(url: str, file_path: str, max_retries: int = 5, timeout: int = 6 # 应用 HTTP_REPLACE_MAP 替换 URL http_url = _apply_http_replace_map(url) - logger.debug(f"Uploading to: {http_url}") + content_type = _get_content_type(file_path) + logger.info(f"Uploading to: {http_url} (Content-Type: {content_type})") retries = 0 while retries < max_retries: @@ -80,7 +104,7 @@ def upload_file(url: str, file_path: str, max_retries: int = 5, timeout: int = 6 data=f, stream=True, timeout=timeout, - headers={"Content-Type": "application/octet-stream"} + headers={"Content-Type": content_type} ) as response: response.raise_for_status() logger.info(f"Upload succeeded: {file_path}")