39 lines
1.1 KiB
Python
39 lines
1.1 KiB
Python
from enum import Enum
|
|
|
|
class DocumentType(str, Enum):
|
|
PDF = "pdf"
|
|
DOCX = "docx"
|
|
XLSX = "xlsx"
|
|
JPG = "jpg"
|
|
JPEG = "jpeg"
|
|
PNG = "png"
|
|
GIF = "gif"
|
|
|
|
@classmethod
|
|
def from_mime_type(cls, mime_type: str) -> "DocumentType":
|
|
"""Map MIME type to DocumentType"""
|
|
mapping = {
|
|
"application/pdf": cls.PDF,
|
|
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": cls.DOCX,
|
|
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": cls.XLSX,
|
|
"image/jpeg": cls.JPG,
|
|
"image/png": cls.PNG,
|
|
"image/gif": cls.GIF,
|
|
}
|
|
return mapping.get(mime_type.lower())
|
|
|
|
@classmethod
|
|
def from_extension(cls, filename: str) -> "DocumentType":
|
|
"""Map file extension to DocumentType"""
|
|
ext = filename.split(".")[-1].lower()
|
|
mapping = {
|
|
"pdf": cls.PDF,
|
|
"docx": cls.DOCX,
|
|
"xlsx": cls.XLSX,
|
|
"jpg": cls.JPG,
|
|
"jpeg": cls.JPEG,
|
|
"png": cls.PNG,
|
|
"gif": cls.GIF,
|
|
}
|
|
return mapping.get(ext)
|