Show More
@@ -0,0 +1,31 b'' | |||||
|
1 | # -*- coding: utf-8 -*- | |||
|
2 | # Generated by Django 1.10.5 on 2017-02-27 09:56 | |||
|
3 | from __future__ import unicode_literals | |||
|
4 | ||||
|
5 | from django.db import migrations | |||
|
6 | from django.db.models import Q | |||
|
7 | from django.db import models | |||
|
8 | from boards.utils import get_file_mimetype | |||
|
9 | ||||
|
10 | ||||
|
11 | class Migration(migrations.Migration): | |||
|
12 | ||||
|
13 | def extension_to_mimetype(apps, schema_editor): | |||
|
14 | Attachment = apps.get_model('boards', 'Attachment') | |||
|
15 | for attachment in Attachment.objects.filter(Q(url='') | Q(url=None)): | |||
|
16 | attachment.mimetype = get_file_mimetype(attachment.file) | |||
|
17 | attachment.save(update_fields=['mimetype']) | |||
|
18 | attachment.file.close() | |||
|
19 | ||||
|
20 | dependencies = [ | |||
|
21 | ('boards', '0058_auto_20170224_2310'), | |||
|
22 | ] | |||
|
23 | ||||
|
24 | operations = [ | |||
|
25 | migrations.AlterField( | |||
|
26 | model_name='attachment', | |||
|
27 | name='mimetype', | |||
|
28 | field=models.CharField(max_length=200, null=True), | |||
|
29 | ), | |||
|
30 | migrations.RunPython(extension_to_mimetype), | |||
|
31 | ] |
@@ -8,7 +8,8 b' from django.db import models' | |||||
8 | from boards import utils |
|
8 | from boards import utils | |
9 | from boards.models.attachment.viewers import get_viewers, AbstractViewer, \ |
|
9 | from boards.models.attachment.viewers import get_viewers, AbstractViewer, \ | |
10 | FILE_TYPES_IMAGE |
|
10 | FILE_TYPES_IMAGE | |
11 | from boards.utils import get_upload_filename, get_extension, cached_result |
|
11 | from boards.utils import get_upload_filename, get_extension, cached_result, \ | |
|
12 | get_file_mimetype | |||
12 |
|
13 | |||
13 |
|
14 | |||
14 | class AttachmentManager(models.Manager): |
|
15 | class AttachmentManager(models.Manager): | |
@@ -16,8 +17,7 b' class AttachmentManager(models.Manager):' | |||||
16 | file_hash = utils.get_file_hash(file) |
|
17 | file_hash = utils.get_file_hash(file) | |
17 | attachment = self.get_existing_duplicate(file_hash, file) |
|
18 | attachment = self.get_existing_duplicate(file_hash, file) | |
18 | if not attachment: |
|
19 | if not attachment: | |
19 | # FIXME Use full mimetype here, need to modify viewers too |
|
20 | file_type = get_file_mimetype(file) | |
20 | file_type = get_extension(file.name) |
|
|||
21 | attachment = self.create(file=file, mimetype=file_type, |
|
21 | attachment = self.create(file=file, mimetype=file_type, | |
22 | hash=file_hash) |
|
22 | hash=file_hash) | |
23 |
|
23 | |||
@@ -106,7 +106,7 b' class Attachment(models.Model):' | |||||
106 | ordering = ('id',) |
|
106 | ordering = ('id',) | |
107 |
|
107 | |||
108 | file = models.FileField(upload_to=get_upload_filename, null=True) |
|
108 | file = models.FileField(upload_to=get_upload_filename, null=True) | |
109 |
mimetype = models.CharField(max_length= |
|
109 | mimetype = models.CharField(max_length=200, null=True) | |
110 | hash = models.CharField(max_length=36, null=True) |
|
110 | hash = models.CharField(max_length=36, null=True) | |
111 | alias = models.TextField(unique=True, null=True) |
|
111 | alias = models.TextField(unique=True, null=True) | |
112 | url = models.TextField(blank=True, default='') |
|
112 | url = models.TextField(blank=True, default='') | |
@@ -144,11 +144,15 b' class Attachment(models.Model):' | |||||
144 |
|
144 | |||
145 | @cached_result() |
|
145 | @cached_result() | |
146 | def get_preview_size(self): |
|
146 | def get_preview_size(self): | |
|
147 | size = 200, 150 | |||
147 | if self.mimetype in FILE_TYPES_IMAGE: |
|
148 | if self.mimetype in FILE_TYPES_IMAGE: | |
148 | preview_path = self.file.path.replace('.', '.200x150.') |
|
149 | preview_path = self.file.path.replace('.', '.200x150.') | |
149 | return get_image_dimensions(preview_path) |
|
150 | try: | |
150 | else: |
|
151 | size = get_image_dimensions(preview_path) | |
151 | return 200, 150 |
|
152 | except Exception: | |
|
153 | pass | |||
|
154 | ||||
|
155 | return size | |||
152 |
|
156 | |||
153 | def is_internal(self): |
|
157 | def is_internal(self): | |
154 | return self.url is None or len(self.url) == 0 |
|
158 | return self.url is None or len(self.url) == 0 |
@@ -19,30 +19,31 b" FILE_FILEFORMAT = 'images/fileformats/{}" | |||||
19 |
|
19 | |||
20 |
|
20 | |||
21 | FILE_TYPES_VIDEO = ( |
|
21 | FILE_TYPES_VIDEO = ( | |
22 | 'webm', |
|
22 | 'video/webm', | |
23 | 'mp4', |
|
23 | 'video/mp4', | |
24 | 'mpeg', |
|
24 | 'video/mpeg', | |
25 | 'ogv', |
|
25 | 'video/ogv', | |
26 | ) |
|
26 | ) | |
27 | FILE_TYPE_SVG = 'svg' |
|
27 | FILE_TYPE_SVG = 'image/svg' | |
28 | FILE_TYPES_AUDIO = ( |
|
28 | FILE_TYPES_AUDIO = ( | |
29 | 'ogg', |
|
29 | 'audio/ogg', | |
30 | 'mp3', |
|
30 | 'audio/mp3', | |
31 | 'opus', |
|
31 | 'audio/opus', | |
|
32 | 'audio/flac', | |||
32 | ) |
|
33 | ) | |
33 | FILE_TYPES_IMAGE = ( |
|
34 | FILE_TYPES_IMAGE = ( | |
34 | 'jpeg', |
|
35 | 'image/jpeg', | |
35 | 'jpg', |
|
36 | 'image/jpg', | |
36 | 'png', |
|
37 | 'image/png', | |
37 | 'bmp', |
|
38 | 'image/bmp', | |
38 | 'gif', |
|
39 | 'image/gif', | |
39 | ) |
|
40 | ) | |
40 |
|
41 | |||
41 | PLAIN_FILE_FORMATS = { |
|
42 | PLAIN_FILE_FORMATS = { | |
42 | 'zip': 'archive', |
|
43 | 'application/zip': 'archive', | |
43 | 'tar': 'archive', |
|
44 | 'application/x-tar': 'archive', | |
44 | 'gz': 'archive', |
|
45 | 'gz': 'archive', | |
45 | 'mid' : 'midi', |
|
46 | 'audio/midi' : 'midi', | |
46 | } |
|
47 | } | |
47 |
|
48 | |||
48 | URL_PROTOCOLS = { |
|
49 | URL_PROTOCOLS = { |
General Comments 0
You need to be logged in to leave comments.
Login now