Add: file utils function for file type categorization
This commit is contained in:
parent
7dc8479801
commit
7fc88dc0e9
1 changed files with 51 additions and 0 deletions
|
@ -13,3 +13,54 @@ def get_mime_type(file_path: str) -> str:
|
||||||
"""
|
"""
|
||||||
mime_type, encoding = mimetypes.guess_type(file_path)
|
mime_type, encoding = mimetypes.guess_type(file_path)
|
||||||
return mime_type
|
return mime_type
|
||||||
|
|
||||||
|
|
||||||
|
def categorize_mime_type(mime_type: str) -> str:
|
||||||
|
image_types = {
|
||||||
|
"image/jpeg",
|
||||||
|
"image/png",
|
||||||
|
"image/bmp",
|
||||||
|
"image/tiff",
|
||||||
|
"image/webp",
|
||||||
|
"image/svg+xml",
|
||||||
|
"image/heif",
|
||||||
|
"image/heic",
|
||||||
|
}
|
||||||
|
gif_types = {"image/gif"}
|
||||||
|
video_types = {
|
||||||
|
"video/mp4",
|
||||||
|
"video/mpeg",
|
||||||
|
"video/quicktime",
|
||||||
|
"video/x-msvideo",
|
||||||
|
"video/x-matroska",
|
||||||
|
"video/webm",
|
||||||
|
"video/ogg",
|
||||||
|
}
|
||||||
|
flash_types = {"application/x-shockwave-flash", "application/vnd.adobe.flash.movie"}
|
||||||
|
archive_types = {
|
||||||
|
"application/zip",
|
||||||
|
"application/x-rar-compressed",
|
||||||
|
"application/x-tar",
|
||||||
|
"application/gzip",
|
||||||
|
"application/x-7z-compressed",
|
||||||
|
"application/x-bzip2",
|
||||||
|
}
|
||||||
|
pdf_types = {"application/pdf"}
|
||||||
|
audio_types = {"audio/mpeg", "audio/wav", "audio/ogg", "audio/flac", "audio/aac"}
|
||||||
|
|
||||||
|
if mime_type in image_types:
|
||||||
|
return "image"
|
||||||
|
elif mime_type in gif_types:
|
||||||
|
return "gif"
|
||||||
|
elif mime_type in video_types:
|
||||||
|
return "video"
|
||||||
|
elif mime_type in flash_types:
|
||||||
|
return "flash"
|
||||||
|
elif mime_type in archive_types:
|
||||||
|
return "archive"
|
||||||
|
elif mime_type in pdf_types:
|
||||||
|
return "pdf"
|
||||||
|
elif mime_type in audio_types:
|
||||||
|
return "audio"
|
||||||
|
else:
|
||||||
|
return "other"
|
||||||
|
|
Loading…
Add table
Reference in a new issue