- allow audio/webm mime type - normalize content-type values with codec parameters before validation - use normalized mime for extension resolution
This commit is contained in:
@@ -21,14 +21,19 @@ ALLOWED_MIME_TYPES = {
|
|||||||
"video/webm",
|
"video/webm",
|
||||||
"audio/mpeg",
|
"audio/mpeg",
|
||||||
"audio/ogg",
|
"audio/ogg",
|
||||||
|
"audio/webm",
|
||||||
"audio/wav",
|
"audio/wav",
|
||||||
"application/pdf",
|
"application/pdf",
|
||||||
"application/zip",
|
"application/zip",
|
||||||
"text/plain",
|
"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:
|
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:
|
if not ext:
|
||||||
return ".bin"
|
return ".bin"
|
||||||
if ext == ".jpe":
|
if ext == ".jpe":
|
||||||
@@ -50,7 +55,8 @@ def _allowed_file_url_prefixes() -> tuple[str, ...]:
|
|||||||
|
|
||||||
|
|
||||||
def _validate_media(file_type: str, file_size: int) -> None:
|
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")
|
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail="Unsupported file type")
|
||||||
if file_size > settings.max_upload_size_bytes:
|
if file_size > settings.max_upload_size_bytes:
|
||||||
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail="File size exceeds limit")
|
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail="File size exceeds limit")
|
||||||
|
|||||||
Reference in New Issue
Block a user