##// END OF EJS Templates
Get mimetype for file in the form and use it for both images and attachments
neko259 -
r1371:ca879451 default
parent child Browse files
Show More
@@ -13,7 +13,8 b' from boards.mdx_neboard import formatter'
13 13 from boards.models.attachment.downloaders import Downloader
14 14 from boards.models.post import TITLE_MAX_LENGTH
15 15 from boards.models import Tag, Post
16 from boards.utils import validate_file_size
16 from boards.utils import validate_file_size, get_file_mimetype, \
17 FILE_EXTENSION_DELIMITER
17 18 from neboard import settings
18 19 import boards.settings as board_settings
19 20 import neboard
@@ -157,6 +158,14 b' class PostForm(NeboardForm):'
157 158 session = None
158 159 need_to_ban = False
159 160
161 def _update_file_extension(self, file):
162 if file:
163 extension = get_file_mimetype(file)
164 filename = file.name.split(FILE_EXTENSION_DELIMITER, 1)[0]
165 new_filename = filename + FILE_EXTENSION_DELIMITER + extension
166
167 file.name = new_filename
168
160 169 def clean_title(self):
161 170 title = self.cleaned_data['title']
162 171 if title:
@@ -180,6 +189,7 b' class PostForm(NeboardForm):'
180 189
181 190 if file:
182 191 validate_file_size(file.size)
192 self._update_file_extension(file)
183 193
184 194 return file
185 195
@@ -194,6 +204,7 b' class PostForm(NeboardForm):'
194 204 raise forms.ValidationError(_('Invalid URL'))
195 205 else:
196 206 validate_file_size(file.size)
207 self._update_file_extension(file)
197 208
198 209 return file
199 210
@@ -1,13 +1,8 b''
1 import magic
2
3 1 from django.db import models
4 2
5 3 from boards import utils
6 4 from boards.models.attachment.viewers import get_viewers, AbstractViewer
7 from boards.utils import get_upload_filename
8
9 FILES_DIRECTORY = 'files/'
10 FILE_EXTENSION_DELIMITER = '.'
5 from boards.utils import get_upload_filename, get_file_mimetype
11 6
12 7
13 8 class AttachmentManager(models.Manager):
@@ -17,10 +12,9 b' class AttachmentManager(models.Manager):'
17 12 if len(existing) > 0:
18 13 attachment = existing[0]
19 14 else:
20 file_type = magic.from_buffer(file.chunks().__next__(), mime=True)\
21 .decode().split('/')[-1]
22 attachment = Attachment.objects.create(
23 file=file, mimetype=file_type, hash=file_hash)
15 file_type = get_file_mimetype(file)
16 attachment = self.create(file=file, mimetype=file_type,
17 hash=file_hash)
24 18
25 19 return attachment
26 20
@@ -1,8 +1,3 b''
1 import hashlib
2 import os
3 from random import random
4 import time
5
6 1 from django.db import models
7 2 from django.template.defaultfilters import filesizeformat
8 3
@@ -11,6 +11,7 b' from django.db.models import Model'
11 11 from django import forms
12 12 from django.utils import timezone
13 13 from django.utils.translation import ugettext_lazy as _
14 import magic
14 15 from portage import os
15 16
16 17 import boards
@@ -140,3 +141,8 b' def get_upload_filename(model_instance, '
140 141 directory = UPLOAD_DIRS[type(model_instance).__name__]
141 142
142 143 return os.path.join(directory, new_name)
144
145
146 def get_file_mimetype(file) -> str:
147 return magic.from_buffer(file.chunks().__next__(), mime=True) \
148 .decode().split('/')[-1]
General Comments 0
You need to be logged in to leave comments. Login now