# HG changeset patch # User neko259 # Date 2015-10-17 16:19:37 # Node ID ca8794518303d23ca16bf56db22fe080f113bee6 # Parent cc49db453424cbc5f11a245988858ca312cfab6c Get mimetype for file in the form and use it for both images and attachments diff --git a/boards/forms.py b/boards/forms.py --- a/boards/forms.py +++ b/boards/forms.py @@ -13,7 +13,8 @@ from boards.mdx_neboard import formatter from boards.models.attachment.downloaders import Downloader from boards.models.post import TITLE_MAX_LENGTH from boards.models import Tag, Post -from boards.utils import validate_file_size +from boards.utils import validate_file_size, get_file_mimetype, \ + FILE_EXTENSION_DELIMITER from neboard import settings import boards.settings as board_settings import neboard @@ -157,6 +158,14 @@ class PostForm(NeboardForm): session = None need_to_ban = False + 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 + + file.name = new_filename + def clean_title(self): title = self.cleaned_data['title'] if title: @@ -180,6 +189,7 @@ class PostForm(NeboardForm): if file: validate_file_size(file.size) + self._update_file_extension(file) return file @@ -194,6 +204,7 @@ class PostForm(NeboardForm): raise forms.ValidationError(_('Invalid URL')) else: validate_file_size(file.size) + self._update_file_extension(file) return file 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 @@ -1,13 +1,8 @@ -import magic - from django.db import models from boards import utils from boards.models.attachment.viewers import get_viewers, AbstractViewer -from boards.utils import get_upload_filename - -FILES_DIRECTORY = 'files/' -FILE_EXTENSION_DELIMITER = '.' +from boards.utils import get_upload_filename, get_file_mimetype class AttachmentManager(models.Manager): @@ -17,10 +12,9 @@ class AttachmentManager(models.Manager): if len(existing) > 0: attachment = existing[0] else: - file_type = magic.from_buffer(file.chunks().__next__(), mime=True)\ - .decode().split('/')[-1] - attachment = Attachment.objects.create( - file=file, mimetype=file_type, hash=file_hash) + file_type = get_file_mimetype(file) + attachment = self.create(file=file, mimetype=file_type, + hash=file_hash) return attachment diff --git a/boards/models/image.py b/boards/models/image.py --- a/boards/models/image.py +++ b/boards/models/image.py @@ -1,8 +1,3 @@ -import hashlib -import os -from random import random -import time - from django.db import models from django.template.defaultfilters import filesizeformat diff --git a/boards/utils.py b/boards/utils.py --- a/boards/utils.py +++ b/boards/utils.py @@ -11,6 +11,7 @@ from django.db.models import Model from django import forms from django.utils import timezone from django.utils.translation import ugettext_lazy as _ +import magic from portage import os import boards @@ -140,3 +141,8 @@ def get_upload_filename(model_instance, directory = UPLOAD_DIRS[type(model_instance).__name__] return os.path.join(directory, new_name) + + +def get_file_mimetype(file) -> str: + return magic.from_buffer(file.chunks().__next__(), mime=True) \ + .decode().split('/')[-1]