##// 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 from boards.models.attachment.downloaders import Downloader
13 from boards.models.attachment.downloaders import Downloader
14 from boards.models.post import TITLE_MAX_LENGTH
14 from boards.models.post import TITLE_MAX_LENGTH
15 from boards.models import Tag, Post
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 from neboard import settings
18 from neboard import settings
18 import boards.settings as board_settings
19 import boards.settings as board_settings
19 import neboard
20 import neboard
@@ -157,6 +158,14 b' class PostForm(NeboardForm):'
157 session = None
158 session = None
158 need_to_ban = False
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 def clean_title(self):
169 def clean_title(self):
161 title = self.cleaned_data['title']
170 title = self.cleaned_data['title']
162 if title:
171 if title:
@@ -180,6 +189,7 b' class PostForm(NeboardForm):'
180
189
181 if file:
190 if file:
182 validate_file_size(file.size)
191 validate_file_size(file.size)
192 self._update_file_extension(file)
183
193
184 return file
194 return file
185
195
@@ -194,6 +204,7 b' class PostForm(NeboardForm):'
194 raise forms.ValidationError(_('Invalid URL'))
204 raise forms.ValidationError(_('Invalid URL'))
195 else:
205 else:
196 validate_file_size(file.size)
206 validate_file_size(file.size)
207 self._update_file_extension(file)
197
208
198 return file
209 return file
199
210
@@ -1,13 +1,8 b''
1 import magic
2
3 from django.db import models
1 from django.db import models
4
2
5 from boards import utils
3 from boards import utils
6 from boards.models.attachment.viewers import get_viewers, AbstractViewer
4 from boards.models.attachment.viewers import get_viewers, AbstractViewer
7 from boards.utils import get_upload_filename
5 from boards.utils import get_upload_filename, get_file_mimetype
8
9 FILES_DIRECTORY = 'files/'
10 FILE_EXTENSION_DELIMITER = '.'
11
6
12
7
13 class AttachmentManager(models.Manager):
8 class AttachmentManager(models.Manager):
@@ -17,10 +12,9 b' class AttachmentManager(models.Manager):'
17 if len(existing) > 0:
12 if len(existing) > 0:
18 attachment = existing[0]
13 attachment = existing[0]
19 else:
14 else:
20 file_type = magic.from_buffer(file.chunks().__next__(), mime=True)\
15 file_type = get_file_mimetype(file)
21 .decode().split('/')[-1]
16 attachment = self.create(file=file, mimetype=file_type,
22 attachment = Attachment.objects.create(
17 hash=file_hash)
23 file=file, mimetype=file_type, hash=file_hash)
24
18
25 return attachment
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 from django.db import models
1 from django.db import models
7 from django.template.defaultfilters import filesizeformat
2 from django.template.defaultfilters import filesizeformat
8
3
@@ -11,6 +11,7 b' from django.db.models import Model'
11 from django import forms
11 from django import forms
12 from django.utils import timezone
12 from django.utils import timezone
13 from django.utils.translation import ugettext_lazy as _
13 from django.utils.translation import ugettext_lazy as _
14 import magic
14 from portage import os
15 from portage import os
15
16
16 import boards
17 import boards
@@ -140,3 +141,8 b' def get_upload_filename(model_instance, '
140 directory = UPLOAD_DIRS[type(model_instance).__name__]
141 directory = UPLOAD_DIRS[type(model_instance).__name__]
141
142
142 return os.path.join(directory, new_name)
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