Add multi-level thumbnails

This commit is contained in:
rubenwardy 2018-12-21 21:22:15 +00:00
parent 89cae279cd
commit 31f57e1f12
2 changed files with 17 additions and 13 deletions

@ -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)

@ -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]
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))