upload.mime - Check file MIME types

class upload.mime.Mime

Bases: object

Guesses and checks MIME types.

MIME_TYPE_VARIATIONS: ClassVar[dict[str, set[str]]] = {'7z': {'application/x-7z-compressed'}, 'csv': {'application/csv', 'text/csv', 'text/plain'}, 'docx': {'application/vnd.openxmlformats-officedocument.wordprocessingml.document'}, 'flac': {'audio/flac', 'audio/x-flac'}, 'gif': {'image/gif'}, 'html': {'text/html'}, 'jpeg': {'image/jpeg'}, 'jpg': {'image/jpeg'}, 'mkv': {'video/mkv', 'video/x-matroska'}, 'mp3': {'audio/mp3', 'audio/mpeg'}, 'mp4': {'video/mp4'}, 'odt': {'application/vnd.oasis.opendocument.text'}, 'pdf': {'application/pdf'}, 'png': {'image/png'}, 'txt': {'text/plain'}, 'wav': {'audio/wav', 'audio/wave', 'audio/x-wav'}, 'xlsx': {'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'}, 'zip': {'application/x-zip-compressed', 'application/zip'}}
guess(extension: str) set[str]

Get expected MIME types for a file extension using python-magic.

Parameters:

extension – File extension (e.g., ‘.pdf’, ‘jpg’)

Returns:

Set of acceptable MIME types for the extension

Return type:

set

check(file_object: File) str

Check the file’s MIME type.

Parameters:

file_object – The file to check the MIME type of.

Returns:

The MIME type, as a string