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}")