0001-misc-caching-for-thumbnails-52528.patch
tests/test_upload_storage.py | ||
---|---|---|
1 | 1 |
# -*- coding: utf-8 -*- |
2 | 2 | |
3 |
import hashlib |
|
3 | 4 |
import json |
4 | 5 |
import os |
5 | 6 | |
6 | 7 |
import mock |
7 | 8 |
import pytest |
9 |
from django.utils.encoding import force_bytes |
|
8 | 10 |
from webtest import Upload |
9 | 11 | |
10 | 12 |
from wcs import fields |
... | ... | |
241 | 243 |
} |
242 | 244 | |
243 | 245 | |
246 |
def test_thumbnail_caching(pub): |
|
247 |
create_user_and_admin(pub) |
|
248 |
FormDef.wipe() |
|
249 |
formdef = FormDef() |
|
250 |
formdef.name = 'test' |
|
251 |
formdef.fields = [ |
|
252 |
fields.FileField(id='0', label='file', varname='file'), |
|
253 |
] |
|
254 |
formdef.store() |
|
255 | ||
256 |
assert formdef.fields[0].storage == 'default' |
|
257 | ||
258 |
image_content = open(os.path.join(os.path.dirname(__file__), 'image-with-gps-data.jpeg'), 'rb').read() |
|
259 |
upload = Upload('file.jpg', image_content, 'image/jpeg') |
|
260 | ||
261 |
resp = get_app(pub).get('/test/') |
|
262 |
resp.forms[0]['f0$file'] = upload |
|
263 |
resp = resp.forms[0].submit('submit') |
|
264 |
assert 'Check values then click submit.' in resp.text |
|
265 |
resp = resp.forms[0].submit('submit') |
|
266 |
assert resp.status_int == 302 |
|
267 |
resp = resp.follow() |
|
268 |
assert 'The form has been recorded' in resp.text |
|
269 | ||
270 |
data_file = formdef.data_class().get(1).data['0'] |
|
271 |
thumbs_dir = os.path.join(pub.app_dir, 'thumbs') |
|
272 |
thumb_filepath = os.path.join( |
|
273 |
thumbs_dir, hashlib.sha256(force_bytes(data_file.get_fs_filename())).hexdigest() |
|
274 |
) |
|
275 |
assert os.path.exists(thumb_filepath) is False |
|
276 |
admin_app = login(get_app(pub), username='admin', password='admin') |
|
277 |
admin_app.get('/backoffice/management/test/1/download?f=0&thumbnail=1').follow() |
|
278 |
assert os.path.exists(thumb_filepath) is True |
|
279 | ||
280 | ||
244 | 281 |
@mock.patch('wcs.wscalls.call_webservice') |
245 | 282 |
def test_remoteopaque_in_attachmentevolutionpart(wscall, pub): |
246 | 283 |
create_user_and_admin(pub) |
wcs/qommon/misc.py | ||
---|---|---|
40 | 40 |
from django.conf import settings |
41 | 41 |
from django.template import TemplateSyntaxError, VariableDoesNotExist |
42 | 42 |
from django.utils import datetime_safe |
43 |
from django.utils.encoding import force_text |
|
43 |
from django.utils.encoding import force_bytes, force_text
|
|
44 | 44 |
from django.utils.formats import localize |
45 | 45 |
from django.utils.html import strip_tags |
46 | 46 |
from django.utils.text import Truncator |
... | ... | |
655 | 655 |
if not can_thumbnail(content_type or ''): |
656 | 656 |
raise ThumbnailError() |
657 | 657 | |
658 |
# check if thumbnail already exists |
|
659 |
thumbs_dir = os.path.join(get_publisher().app_dir, 'thumbs') |
|
660 |
if not os.path.exists(thumbs_dir): |
|
661 |
os.mkdir(thumbs_dir) |
|
662 |
thumb_filepath = os.path.join(thumbs_dir, hashlib.sha256(force_bytes(filepath)).hexdigest()) |
|
663 |
if os.path.exists(thumb_filepath): |
|
664 |
with open(thumb_filepath, 'rb') as f: |
|
665 |
return f.read() |
|
666 | ||
667 |
# generate thumbnail |
|
658 | 668 |
if content_type == 'application/pdf': |
659 | 669 |
try: |
660 | 670 |
fp = io.BytesIO( |
... | ... | |
705 | 715 |
except IOError: |
706 | 716 |
# failed to create thumbnail. |
707 | 717 |
raise ThumbnailError() |
718 | ||
719 |
# store thumbnail |
|
720 |
with open(thumb_filepath, 'wb') as f: |
|
721 |
f.write(image_thumb_fp.getvalue()) |
|
722 | ||
708 | 723 |
return image_thumb_fp.getvalue() |
709 | 724 | |
710 | 725 | |
711 |
- |