Show More
@@ -1,8 +1,9 b'' | |||||
1 | import hashlib |
|
1 | import hashlib | |
2 | import re |
|
2 | import re | |
3 | import time |
|
3 | import time | |
|
4 | import logging | |||
|
5 | import pytz | |||
4 |
|
6 | |||
5 | import pytz |
|
|||
6 | from django import forms |
|
7 | from django import forms | |
7 | from django.core.files.uploadedfile import SimpleUploadedFile |
|
8 | from django.core.files.uploadedfile import SimpleUploadedFile | |
8 | from django.core.exceptions import ObjectDoesNotExist |
|
9 | from django.core.exceptions import ObjectDoesNotExist | |
@@ -45,6 +46,16 b' TEXTAREA_ROWS = 4' | |||||
45 |
|
46 | |||
46 | TRIPCODE_DELIM = '#' |
|
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 | def get_timezones(): |
|
60 | def get_timezones(): | |
50 | timezones = [] |
|
61 | timezones = [] | |
@@ -160,11 +171,17 b' class PostForm(NeboardForm):' | |||||
160 |
|
171 | |||
161 | def _update_file_extension(self, file): |
|
172 | def _update_file_extension(self, file): | |
162 | if file: |
|
173 | if file: | |
163 |
e |
|
174 | mimetype = get_file_mimetype(file) | |
|
175 | extension = MIMETYPE_EXTENSIONS.get(mimetype) | |||
|
176 | if extension: | |||
164 | filename = file.name.split(FILE_EXTENSION_DELIMITER, 1)[0] |
|
177 | filename = file.name.split(FILE_EXTENSION_DELIMITER, 1)[0] | |
165 | new_filename = filename + FILE_EXTENSION_DELIMITER + extension |
|
178 | new_filename = filename + FILE_EXTENSION_DELIMITER + extension | |
166 |
|
179 | |||
167 | file.name = new_filename |
|
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 | def clean_title(self): |
|
186 | def clean_title(self): | |
170 | title = self.cleaned_data['title'] |
|
187 | title = self.cleaned_data['title'] |
@@ -12,7 +12,8 b' class AttachmentManager(models.Manager):' | |||||
12 | if len(existing) > 0: |
|
12 | if len(existing) > 0: | |
13 | attachment = existing[0] |
|
13 | attachment = existing[0] | |
14 | else: |
|
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 | attachment = self.create(file=file, mimetype=file_type, |
|
17 | attachment = self.create(file=file, mimetype=file_type, | |
17 | hash=file_hash) |
|
18 | hash=file_hash) | |
18 |
|
19 |
@@ -112,13 +112,13 b' PopupImageViewer.prototype.view = functi' | |||||
112 | .attr('id', thumb_id) |
|
112 | .attr('id', thumb_id) | |
113 | .attr('src', postNode.attr('href')) |
|
113 | .attr('src', postNode.attr('href')) | |
114 | .attr(ATTR_SCALE, scale) |
|
114 | .attr(ATTR_SCALE, scale) | |
115 | .appendTo(postNode) |
|
|||
116 | .css({ |
|
115 | .css({ | |
117 | 'width': img_w, |
|
116 | 'width': img_w, | |
118 | 'height': img_h, |
|
117 | 'height': img_h, | |
119 | 'left': (win_w - img_w) / 2, |
|
118 | 'left': (win_w - img_w) / 2, | |
120 | 'top': ((win_h - img_h) / 2) |
|
119 | 'top': ((win_h - img_h) / 2) | |
121 | }) |
|
120 | }) | |
|
121 | .appendTo(postNode) | |||
122 | //scaling preview |
|
122 | //scaling preview | |
123 | .mousewheel(function(event, delta) { |
|
123 | .mousewheel(function(event, delta) { | |
124 | var cx = event.originalEvent.clientX; |
|
124 | var cx = event.originalEvent.clientX; |
@@ -129,9 +129,6 b' def validate_file_size(size: int):' | |||||
129 |
|
129 | |||
130 | def get_upload_filename(model_instance, old_filename): |
|
130 | def get_upload_filename(model_instance, old_filename): | |
131 | # TODO Use something other than random number in file name |
|
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 | new_name = '{}{}.{}'.format( |
|
133 | new_name = '{}{}.{}'.format( | |
137 | str(int(time.mktime(time.gmtime()))), |
|
134 | str(int(time.mktime(time.gmtime()))), | |
@@ -144,5 +141,4 b' def get_upload_filename(model_instance, ' | |||||
144 |
|
141 | |||
145 |
|
142 | |||
146 | def get_file_mimetype(file) -> str: |
|
143 | def get_file_mimetype(file) -> str: | |
147 |
return magic.from_buffer(file.chunks().__next__(), mime=True) |
|
144 | return magic.from_buffer(file.chunks().__next__(), mime=True).decode() | |
148 | .decode().split('/')[-1] |
|
General Comments 0
You need to be logged in to leave comments.
Login now