From c248a4c227b69ba181bed61808b6da9caf33f0c3 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Mon, 23 Dec 2013 12:32:23 +0000 Subject: [PATCH] return thumbnails for images instead of full image --- pandora/document/models.py | 21 ++++++++++++++++----- pandora/document/views.py | 2 +- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/pandora/document/models.py b/pandora/document/models.py index 1de59369..79b334e4 100644 --- a/pandora/document/models.py +++ b/pandora/document/models.py @@ -15,6 +15,7 @@ import Image import ox from item.models import Item +from archive.extract import resize_image import managers @@ -167,18 +168,28 @@ class Document(models.Model): return True return False - def thumbnail(self): + def thumbnail(self, size=None): + src = self.file.path if self.extension == 'pdf': - thumbnail = '%s.jpg' % self.file.path + src = '%s.jpg' % src + if size: + size = int(size) + path = src.replace('.jpg', '.%d.jpg'%size) else: - thumbnail = self.file.path - return thumbnail + path = src + if os.path.exists(src) and not os.path.exists(path): + image_size = max(*Image.open(src).size) + if size > image_size: + path = src + else: + resize_image(src, path, size=size) + return path def make_thumbnail(self, force=False): thumb = self.thumbnail() if not os.path.exists(thumb) or force: cmd = ['convert', '%s[0]' % self.file.path, - '-background', 'white', '-flatten', '-resize', '256x256', thumb] + '-background', 'white', '-flatten', '-resize', '1024x1024', thumb] p = subprocess.Popen(cmd) p.wait() diff --git a/pandora/document/views.py b/pandora/document/views.py index e4a89184..c97b3e46 100644 --- a/pandora/document/views.py +++ b/pandora/document/views.py @@ -227,7 +227,7 @@ def file(request, id, name=None): def thumbnail(request, id, size=256): size = int(size) document = models.Document.get(id) - return HttpFileResponse(document.thumbnail()) + return HttpFileResponse(document.thumbnail(size)) class ChunkForm(forms.Form): chunk = forms.FileField()