##// END OF EJS Templates
Autodetect only a white list of mimetypes
neko259 -
r1372:cab9c599 default
parent child Browse files
Show More
@@ -1,8 +1,9 b''
1 1 import hashlib
2 2 import re
3 3 import time
4 import logging
5 import pytz
4 6
5 import pytz
6 7 from django import forms
7 8 from django.core.files.uploadedfile import SimpleUploadedFile
8 9 from django.core.exceptions import ObjectDoesNotExist
@@ -45,6 +46,16 b' TEXTAREA_ROWS = 4'
45 46
46 47 TRIPCODE_DELIM = '#'
47 48
49 # TODO Maybe this may be converted into the database table?
50 MIMETYPE_EXTENSIONS = {
51 'image/jpeg': 'jpeg',
52 'image/png': 'png',
53 'image/gif': 'gif',
54 'video/webm': 'webm',
55 'application/pdf': 'pdf',
56 'x-diff': 'diff',
57 }
58
48 59
49 60 def get_timezones():
50 61 timezones = []
@@ -160,11 +171,17 b' class PostForm(NeboardForm):'
160 171
161 172 def _update_file_extension(self, file):
162 173 if file:
163 extension = get_file_mimetype(file)
174 mimetype = get_file_mimetype(file)
175 extension = MIMETYPE_EXTENSIONS.get(mimetype)
176 if extension:
164 177 filename = file.name.split(FILE_EXTENSION_DELIMITER, 1)[0]
165 178 new_filename = filename + FILE_EXTENSION_DELIMITER + extension
166 179
167 180 file.name = new_filename
181 else:
182 logger = logging.getLogger('boards.forms.extension')
183
184 logger.info('Unrecognized file mimetype: {}'.format(mimetype))
168 185
169 186 def clean_title(self):
170 187 title = self.cleaned_data['title']
@@ -12,7 +12,8 b' class AttachmentManager(models.Manager):'
12 12 if len(existing) > 0:
13 13 attachment = existing[0]
14 14 else:
15 file_type = get_file_mimetype(file)
15 # FIXME Use full mimetype here, need to modify viewers too
16 file_type = get_file_mimetype(file).split('/')[-1]
16 17 attachment = self.create(file=file, mimetype=file_type,
17 18 hash=file_hash)
18 19
@@ -112,13 +112,13 b' PopupImageViewer.prototype.view = functi'
112 112 .attr('id', thumb_id)
113 113 .attr('src', postNode.attr('href'))
114 114 .attr(ATTR_SCALE, scale)
115 .appendTo(postNode)
116 115 .css({
117 116 'width': img_w,
118 117 'height': img_h,
119 118 'left': (win_w - img_w) / 2,
120 119 'top': ((win_h - img_h) / 2)
121 120 })
121 .appendTo(postNode)
122 122 //scaling preview
123 123 .mousewheel(function(event, delta) {
124 124 var cx = event.originalEvent.clientX;
@@ -129,9 +129,6 b' def validate_file_size(size: int):'
129 129
130 130 def get_upload_filename(model_instance, old_filename):
131 131 # TODO Use something other than random number in file name
132 if hasattr(model_instance, 'mimetype'):
133 extension = model_instance.mimetype
134 else:
135 132 extension = old_filename.split(FILE_EXTENSION_DELIMITER)[-1:][0]
136 133 new_name = '{}{}.{}'.format(
137 134 str(int(time.mktime(time.gmtime()))),
@@ -144,5 +141,4 b' def get_upload_filename(model_instance, '
144 141
145 142
146 143 def get_file_mimetype(file) -> str:
147 return magic.from_buffer(file.chunks().__next__(), mime=True) \
148 .decode().split('/')[-1]
144 return magic.from_buffer(file.chunks().__next__(), mime=True).decode()
General Comments 0
You need to be logged in to leave comments. Login now