From 74d9163dde3270abfeeb89af3a2371d52f27bd46 Mon Sep 17 00:00:00 2001 From: benya Date: Sun, 8 Mar 2026 02:48:44 +0300 Subject: [PATCH] fix(media): support voice audio/webm uploads - allow audio/webm mime type - normalize content-type values with codec parameters before validation - use normalized mime for extension resolution --- app/media/service.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/app/media/service.py b/app/media/service.py index 70eafc4..fcaaaa9 100644 --- a/app/media/service.py +++ b/app/media/service.py @@ -21,14 +21,19 @@ ALLOWED_MIME_TYPES = { "video/webm", "audio/mpeg", "audio/ogg", + "audio/webm", "audio/wav", "application/pdf", "application/zip", "text/plain", } +def _normalize_mime(file_type: str) -> str: + return file_type.split(";", maxsplit=1)[0].strip().lower() + + def _extension_from_mime(file_type: str) -> str: - ext = mimetypes.guess_extension(file_type) + ext = mimetypes.guess_extension(_normalize_mime(file_type)) if not ext: return ".bin" if ext == ".jpe": @@ -50,7 +55,8 @@ def _allowed_file_url_prefixes() -> tuple[str, ...]: def _validate_media(file_type: str, file_size: int) -> None: - if file_type not in ALLOWED_MIME_TYPES: + normalized_file_type = _normalize_mime(file_type) + if normalized_file_type not in ALLOWED_MIME_TYPES: raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail="Unsupported file type") if file_size > settings.max_upload_size_bytes: raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail="File size exceeds limit")