init
This commit is contained in:
26
app/upload/utils.py
Normal file
26
app/upload/utils.py
Normal file
@@ -0,0 +1,26 @@
|
||||
import magic
|
||||
from upload.models import AbstractFile
|
||||
from functools import lru_cache
|
||||
|
||||
@lru_cache(maxsize=128)
|
||||
def get_mimetype_class_map():
|
||||
mapping = {}
|
||||
for cls in AbstractFile.__subclasses__():
|
||||
for mt in getattr(cls, 'allowed_mimetypes', []):
|
||||
mapping[mt] = cls
|
||||
return mapping
|
||||
|
||||
|
||||
def get_mimetype(file_obj):
|
||||
mimetype = magic.from_buffer(file_obj.read(2048), mime=True)
|
||||
file_obj.seek(0)
|
||||
return mimetype
|
||||
|
||||
|
||||
def get_upload_class_for_mimetype(mimetype):
|
||||
mimetype = mimetype.lower().replace('x-', '') # normalisation légère
|
||||
return get_mimetype_class_map().get(mimetype)
|
||||
|
||||
|
||||
def create_upload(request, file_obj):
|
||||
pass
|
||||
Reference in New Issue
Block a user