##// END OF EJS Templates
Use python-magic to get file types
neko259 -
r1369:224c8f01 default
parent child Browse files
Show More
@@ -1,3 +1,5 b''
1 import magic
2
1 from django.db import models
3 from django.db import models
2
4
3 from boards import utils
5 from boards import utils
@@ -15,7 +17,8 b' class AttachmentManager(models.Manager):'
15 if len(existing) > 0:
17 if len(existing) > 0:
16 attachment = existing[0]
18 attachment = existing[0]
17 else:
19 else:
18 file_type = file.name.split(FILE_EXTENSION_DELIMITER)[-1].lower()
20 file_type = magic.from_buffer(file.chunks().__next__(), mime=True)\
21 .decode().split('/')[-1]
19 attachment = Attachment.objects.create(
22 attachment = Attachment.objects.create(
20 file=file, mimetype=file_type, hash=file_hash)
23 file=file, mimetype=file_type, hash=file_hash)
21
24
@@ -128,10 +128,14 b' def validate_file_size(size: int):'
128
128
129 def get_upload_filename(model_instance, old_filename):
129 def get_upload_filename(model_instance, old_filename):
130 # TODO Use something other than random number in file name
130 # TODO Use something other than random number in file name
131 if hasattr(model_instance, 'file_type'):
132 extension = model_instance.file_type
133 else:
134 extension = old_filename.split(FILE_EXTENSION_DELIMITER)[-1:][0]
131 new_name = '{}{}.{}'.format(
135 new_name = '{}{}.{}'.format(
132 str(int(time.mktime(time.gmtime()))),
136 str(int(time.mktime(time.gmtime()))),
133 str(int(random() * 1000)),
137 str(int(random() * 1000)),
134 old_filename.split(FILE_EXTENSION_DELIMITER)[-1:][0])
138 extension)
135
139
136 directory = UPLOAD_DIRS[type(model_instance).__name__]
140 directory = UPLOAD_DIRS[type(model_instance).__name__]
137
141
@@ -1,3 +1,4 b''
1 magic
1 pytube
2 pytube
2 requests
3 requests
3 adjacent
4 adjacent
General Comments 0
You need to be logged in to leave comments. Login now