diff options
author | rubenwardy <rw@rubenwardy.com> | 2018-12-21 21:22:15 +0000 |
---|---|---|
committer | rubenwardy <rw@rubenwardy.com> | 2018-12-22 21:20:25 +0000 |
commit | 31f57e1f1238a5e410f6304b72de7584a4f6d9b5 (patch) | |
tree | 6a5a4634a2e8023702c516bdcd97ee78c2cb90f1 | |
parent | 89cae279cd0b31cd257e3983a4f2b285beb719e1 (diff) | |
download | cheatdb-31f57e1f1238a5e410f6304b72de7584a4f6d9b5.tar.xz |
Add multi-level thumbnails
-rw-r--r-- | app/models.py | 14 | ||||
-rw-r--r-- | 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/<img>") -@app.route("/thumbnails/<int:w>x<int:h>/<img>") -def make_thumbnail(img, w=350, h=233): - if not (w, h) in ALLOWED_RESOLUTIONS: + +@app.route("/thumbnails/<int:level>/<img>") +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] + + mkdir("app/public/thumbnails/{:d}/".format(level)) - cache_filepath = "public/thumbnails/{}x{}/{}".format(w, h, img) + cache_filepath = "public/thumbnails/{:d}/{}".format(level, img) source_filepath = "public/uploads/" + img resize_and_crop("app/" + source_filepath, "app/" + cache_filepath, (w, h)) |