@@ -17,34 +17,18 @@ def _remove_optional_info_from_mime_type(content_type: str | None) -> str | None
1717 return content_type .split (";" )[0 ]
1818
1919
20- def get_validated_mimetype (file : UploadFile , content_type_hint : str | None = None ) -> Optional [str ]:
20+ def get_validated_mimetype (file : UploadFile ) -> Optional [str ]:
2121 """Given the incoming file, identify and return the correct mimetype.
2222
23- Order of operations:
24- - If user passed content_type as a form param, take it as truth.
25- - Otherwise, use file.content_type (as set by the Content-Type header)
26- - If no content_type was passed and the header wasn't useful, call the library's detect_filetype
23+ Always inspects the actual file bytes to determine the true file type,
24+ ignoring client-provided Content-Type headers which can be misleading.
2725
2826 Once we have a filteype, check is_partitionable and return 400 if we don't support this file.
2927 """
30- content_type : str | None = None
31-
32- if content_type_hint is not None :
33- content_type = content_type_hint
34- else :
35- content_type = _remove_optional_info_from_mime_type (file .content_type )
36-
37- filetype = FileType .from_mime_type (content_type )
38-
39- # If content_type was not specified, use the library to identify the file
40- # We inspect the bytes to do this, so we need to buffer the file
41- if not filetype or filetype == FileType .UNK :
42- file_buffer = BytesIO (file .file .read ())
43- file .file .seek (0 )
44-
45- file_buffer .name = file .filename
46-
47- filetype = detect_filetype (file = file_buffer )
28+ file_buffer = BytesIO (file .file .read ())
29+ file .file .seek (0 )
30+ file_buffer .name = file .filename
31+ filetype = detect_filetype (file = file_buffer )
4832
4933 if not filetype .is_partitionable :
5034 raise HTTPException (
0 commit comments