From 31f57e1f1238a5e410f6304b72de7584a4f6d9b5 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Fri, 21 Dec 2018 21:22:15 +0000 Subject: [PATCH] Add multi-level thumbnails --- app/models.py | 14 ++++++++------ app/views/thumbnails.py | 16 +++++++++------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/app/models.py b/app/models.py index 9bfe87a..488cc35 100644 --- a/app/models.py +++ b/app/models.py @@ -388,7 +388,7 @@ class Package(db.Model): setattr(self, e.name, getattr(package, e.name)) def getAsDictionaryShort(self, base_url): - tnurl = self.getThumbnailURL() + tnurl = self.getThumbnailURL(1) return { "name": self.name, "title": self.title, @@ -401,7 +401,7 @@ class Package(db.Model): } def getAsDictionary(self, base_url): - tnurl = self.getThumbnailURL() + tnurl = self.getThumbnailURL(1) return { "author": self.author.display_name, "name": self.name, @@ -429,9 +429,9 @@ class Package(db.Model): "score": round(self.score * 10) / 10 } - def getThumbnailURL(self): + def getThumbnailURL(self, level=2): screenshot = self.screenshots.filter_by(approved=True).first() - return screenshot.getThumbnailURL() if screenshot is not None else None + return screenshot.getThumbnailURL(level) if screenshot is not None else None def getMainScreenshotURL(self): screenshot = self.screenshots.filter_by(approved=True).first() @@ -644,8 +644,10 @@ class PackageScreenshot(db.Model): name=self.package.name, id=self.id) - def getThumbnailURL(self): - return self.url.replace("/uploads/", "/thumbnails/350x233/") + def getThumbnailURL(self, level=2): + return self.url.replace("/uploads/", ("/thumbnails/{:d}/").format(level)) + + class EditRequest(db.Model): id = db.Column(db.Integer, primary_key=True) diff --git a/app/views/thumbnails.py b/app/views/thumbnails.py index 19ab0be..8303067 100644 --- a/app/views/thumbnails.py +++ b/app/views/thumbnails.py @@ -21,7 +21,7 @@ from app import app import os from PIL import Image -ALLOWED_RESOLUTIONS=[(350,233)] +ALLOWED_RESOLUTIONS=[(100,67), (270,180), (350,233)] def mkdir(path): if not os.path.isdir(path): @@ -56,15 +56,17 @@ def resize_and_crop(img_path, modified_path, size): img.save(modified_path) -@app.route("/thumbnails/") -@app.route("/thumbnails/x/") -def make_thumbnail(img, w=350, h=233): - if not (w, h) in ALLOWED_RESOLUTIONS: + +@app.route("/thumbnails//") +def make_thumbnail(img, level): + if level > len(ALLOWED_RESOLUTIONS) or level <= 0: abort(403) - mkdir("app/public/thumbnails/{}x{}/".format(w, h)) + w, h = ALLOWED_RESOLUTIONS[level - 1] - cache_filepath = "public/thumbnails/{}x{}/{}".format(w, h, img) + mkdir("app/public/thumbnails/{:d}/".format(level)) + + cache_filepath = "public/thumbnails/{:d}/{}".format(level, img) source_filepath = "public/uploads/" + img resize_and_crop("app/" + source_filepath, "app/" + cache_filepath, (w, h))