0001-use-sorl-thumbnail-fixes-15857.patch
debian/control | ||
---|---|---|
13 | 13 |
python-django-filters (>= 1), |
14 | 14 |
python-django-filters (<< 2), |
15 | 15 |
python-gadjo, |
16 |
python-magic |
|
16 |
python-magic, |
|
17 |
python-sorl-thumbnail |
|
17 | 18 |
Recommends: python-django-mellon |
18 | 19 |
Description: Fargo Document Box (Python module) |
19 | 20 |
fargo/fargo/admin.py | ||
---|---|---|
14 | 14 |
'origin__label'] |
15 | 15 | |
16 | 16 |
def thumbnail(self, instance): |
17 |
data_url = instance.document.thumbnail_data_url |
|
18 |
if data_url: |
|
19 |
return format_html('<img src="{0}"/>', data_url) |
|
20 |
return '' |
|
17 |
return instance.document.thumbnail_img_tag |
|
21 | 18 |
thumbnail.short_description = _('thumbnail') |
22 | 19 | |
23 | 20 | |
... | ... | |
32 | 29 |
return u', '.join(unicode(u) for u in qs) |
33 | 30 | |
34 | 31 |
def thumbnail(self, instance): |
35 |
data_url = instance.thumbnail_data_url |
|
36 |
if data_url: |
|
37 |
return format_html('<img src="{0}"/>', data_url) |
|
38 |
return '' |
|
32 |
return instance.thumbnail_img_tag |
|
39 | 33 |
thumbnail.short_description = _('thumbnail') |
40 | 34 | |
41 | 35 |
fargo/fargo/models.py | ||
---|---|---|
11 | 11 |
from django.utils.translation import ugettext_lazy as _ |
12 | 12 |
from django.utils.text import slugify |
13 | 13 |
from django.utils.http import urlquote |
14 |
from django.utils.html import format_html |
|
14 | 15 |
from django.dispatch import receiver |
15 | 16 |
from django.db.models.signals import post_save, post_delete |
16 | 17 |
from django.core.files.storage import default_storage |
17 | 18 | |
19 |
from sorl.thumbnail import get_thumbnail, delete |
|
20 |
from sorl.thumbnail.conf import settings as thumbnail_settings |
|
21 | ||
18 | 22 |
from jsonfield import JSONField |
19 | 23 | |
20 | 24 |
from . import utils, managers |
... | ... | |
182 | 186 |
self.mime_type = utils.get_mime_type(self.content.file.name) or '' |
183 | 187 |
super(Document, self).save(*args, **kwargs) |
184 | 188 | |
185 |
@property |
|
186 |
def thumbnail_path(self): |
|
187 |
name = os.path.basename(self.content.name) |
|
188 |
return os.path.join('thumbmails', name + '.png') |
|
189 | ||
190 |
@property |
|
191 |
def thumbnail_full_path(self): |
|
192 |
return default_storage.path(self.thumbnail_path) |
|
193 | ||
194 | 189 |
@property |
195 | 190 |
def thumbnail(self): |
191 |
if not (self.mime_type.startswith('image/') or self.mime_type == 'application/pdf'): |
|
192 |
return None |
|
193 |
thumbnail = get_thumbnail(self.content, '200x200') |
|
196 | 194 |
try: |
197 |
return default_storage.open(self.thumbnail_path) |
|
195 |
# check file exists and is readable |
|
196 |
default_storage.open(thumbnail.name) |
|
197 |
return thumbnail |
|
198 | 198 |
except IOError: |
199 |
return None |
|
199 |
pass |
|
200 |
return None |
|
200 | 201 | |
201 | 202 |
@property |
202 |
def thumbnail_data_url(self): |
|
203 |
if self.thumbnail: |
|
204 |
return 'data:image/png;base64,%s' % base64.b64encode(self.thumbnail.read()) |
|
205 |
return None |
|
203 |
def thumbnail_img_tag(self): |
|
204 |
thumbnail = self.thumbnail |
|
205 |
if not thumbnail: |
|
206 |
return '' |
|
207 | ||
208 |
mime_type = 'image/' + thumbnail_settings.THUMBNAIL_FORMAT.lower() |
|
209 |
data_url = 'data:%s;base64,%s' % (mime_type, base64.b64encode(thumbnail.read())) |
|
210 |
return format_html('<img widht="{}" height="{}" src="{}"/>', |
|
211 |
thumbnail.width, |
|
212 |
thumbnail.height, |
|
213 |
data_url) |
|
206 | 214 | |
207 | 215 |
class Meta: |
208 | 216 |
verbose_name = _('document') |
... | ... | |
210 | 218 |
ordering = ('content_hash',) |
211 | 219 | |
212 | 220 | |
213 |
@receiver(post_save, sender=Document) |
|
214 |
def create_thumbnail(sender, instance, created, **kwargs): |
|
215 |
if not created: |
|
216 |
return |
|
217 | ||
218 |
def do(): |
|
219 |
dirpath = os.path.dirname(instance.thumbnail_full_path) |
|
220 |
if not os.path.isdir(dirpath): |
|
221 |
os.makedirs(dirpath) |
|
222 |
subprocess.call(['gm', 'convert', '-geometry', 'x200', |
|
223 |
instance.content.file.name, |
|
224 |
instance.thumbnail_full_path]) |
|
225 |
threading.Thread(target=do).start() |
|
226 | ||
227 | ||
228 | 221 |
@receiver(post_delete, sender=Document) |
229 | 222 |
def delete_file(sender, instance, **kwargs): |
230 | 223 |
if instance.content: |
231 | 224 |
if os.path.isfile(instance.content.path): |
232 | 225 |
os.remove(instance.content.path) |
226 |
delete(instance.content) |
fargo/fargo/views.py | ||
---|---|---|
179 | 179 |
def get(self, request, pk, filename): |
180 | 180 |
user_document = get_object_or_404(self.get_queryset(), pk=pk, |
181 | 181 |
user=self.request.user) |
182 |
return self.return_user_document(user_document) |
|
183 | ||
184 |
def return_user_document(self, user_document): |
|
185 | 182 |
thumbnail = user_document.document.thumbnail |
186 | 183 |
if not thumbnail: |
187 | 184 |
raise Http404 |
188 |
response = HttpResponse(thumbnail.chunks(), content_type='image/png') |
|
189 |
return response |
|
185 |
return HttpResponse(thumbnail.chunks(), content_type='image/jpeg') |
|
190 | 186 | |
191 | 187 | |
192 | 188 |
class RemoteDownload(Download): |
fargo/settings.py | ||
---|---|---|
41 | 41 |
'fargo.fargo', |
42 | 42 |
'rest_framework', |
43 | 43 |
'fargo.oauth2', |
44 |
'sorl.thumbnail', |
|
44 | 45 |
) |
45 | 46 | |
46 | 47 |
MIDDLEWARE_CLASSES = ( |
... | ... | |
226 | 227 | |
227 | 228 |
INCLUDE_EDIT_LINK = False |
228 | 229 | |
230 |
THUMBNAIL_ENGINE = 'sorl.thumbnail.engines.convert_engine.Engine' |
|
231 | ||
229 | 232 |
FARGO_CODE_LIFETIME = 300 |
230 | 233 |
FARGO_ACCESS_TOKEN_LIFETIME = 3600 |
231 | 234 |
FARGO_OAUTH2_TEMPFILE_LIFETIME = 86400 |
setup.py | ||
---|---|---|
105 | 105 |
'djangorestframework>=3.3,<3.4', |
106 | 106 |
'file-magic', |
107 | 107 |
'requests', |
108 |
'sorl-thumbnail', |
|
108 | 109 |
], |
109 | 110 |
zip_safe=False, |
110 | 111 |
cmdclass={ |
111 |
- |