diff --git a/boards/forms.py b/boards/forms.py --- a/boards/forms.py +++ b/boards/forms.py @@ -1,8 +1,9 @@ import hashlib import re import time +import logging +import pytz -import pytz from django import forms from django.core.files.uploadedfile import SimpleUploadedFile from django.core.exceptions import ObjectDoesNotExist @@ -45,6 +46,16 @@ TEXTAREA_ROWS = 4 TRIPCODE_DELIM = '#' +# TODO Maybe this may be converted into the database table? +MIMETYPE_EXTENSIONS = { + 'image/jpeg': 'jpeg', + 'image/png': 'png', + 'image/gif': 'gif', + 'video/webm': 'webm', + 'application/pdf': 'pdf', + 'x-diff': 'diff', +} + def get_timezones(): timezones = [] @@ -160,11 +171,17 @@ class PostForm(NeboardForm): def _update_file_extension(self, file): if file: - extension = get_file_mimetype(file) - filename = file.name.split(FILE_EXTENSION_DELIMITER, 1)[0] - new_filename = filename + FILE_EXTENSION_DELIMITER + extension + mimetype = get_file_mimetype(file) + extension = MIMETYPE_EXTENSIONS.get(mimetype) + if extension: + filename = file.name.split(FILE_EXTENSION_DELIMITER, 1)[0] + new_filename = filename + FILE_EXTENSION_DELIMITER + extension - file.name = new_filename + file.name = new_filename + else: + logger = logging.getLogger('boards.forms.extension') + + logger.info('Unrecognized file mimetype: {}'.format(mimetype)) def clean_title(self): title = self.cleaned_data['title'] diff --git a/boards/models/attachment/__init__.py b/boards/models/attachment/__init__.py --- a/boards/models/attachment/__init__.py +++ b/boards/models/attachment/__init__.py @@ -12,7 +12,8 @@ class AttachmentManager(models.Manager): if len(existing) > 0: attachment = existing[0] else: - file_type = get_file_mimetype(file) + # FIXME Use full mimetype here, need to modify viewers too + file_type = get_file_mimetype(file).split('/')[-1] attachment = self.create(file=file, mimetype=file_type, hash=file_hash) diff --git a/boards/static/js/image.js b/boards/static/js/image.js --- a/boards/static/js/image.js +++ b/boards/static/js/image.js @@ -112,13 +112,13 @@ PopupImageViewer.prototype.view = functi .attr('id', thumb_id) .attr('src', postNode.attr('href')) .attr(ATTR_SCALE, scale) - .appendTo(postNode) .css({ 'width': img_w, 'height': img_h, 'left': (win_w - img_w) / 2, 'top': ((win_h - img_h) / 2) }) + .appendTo(postNode) //scaling preview .mousewheel(function(event, delta) { var cx = event.originalEvent.clientX; diff --git a/boards/utils.py b/boards/utils.py --- a/boards/utils.py +++ b/boards/utils.py @@ -129,10 +129,7 @@ def validate_file_size(size: int): def get_upload_filename(model_instance, old_filename): # TODO Use something other than random number in file name - if hasattr(model_instance, 'mimetype'): - extension = model_instance.mimetype - else: - extension = old_filename.split(FILE_EXTENSION_DELIMITER)[-1:][0] + extension = old_filename.split(FILE_EXTENSION_DELIMITER)[-1:][0] new_name = '{}{}.{}'.format( str(int(time.mktime(time.gmtime()))), str(int(random() * 1000)), @@ -144,5 +141,4 @@ def get_upload_filename(model_instance, def get_file_mimetype(file) -> str: - return magic.from_buffer(file.chunks().__next__(), mime=True) \ - .decode().split('/')[-1] + return magic.from_buffer(file.chunks().__next__(), mime=True).decode()