Add multi-level thumbnails
This commit is contained in:
parent
89cae279cd
commit
31f57e1f12
|
@ -388,7 +388,7 @@ class Package(db.Model):
|
||||||
setattr(self, e.name, getattr(package, e.name))
|
setattr(self, e.name, getattr(package, e.name))
|
||||||
|
|
||||||
def getAsDictionaryShort(self, base_url):
|
def getAsDictionaryShort(self, base_url):
|
||||||
tnurl = self.getThumbnailURL()
|
tnurl = self.getThumbnailURL(1)
|
||||||
return {
|
return {
|
||||||
"name": self.name,
|
"name": self.name,
|
||||||
"title": self.title,
|
"title": self.title,
|
||||||
|
@ -401,7 +401,7 @@ class Package(db.Model):
|
||||||
}
|
}
|
||||||
|
|
||||||
def getAsDictionary(self, base_url):
|
def getAsDictionary(self, base_url):
|
||||||
tnurl = self.getThumbnailURL()
|
tnurl = self.getThumbnailURL(1)
|
||||||
return {
|
return {
|
||||||
"author": self.author.display_name,
|
"author": self.author.display_name,
|
||||||
"name": self.name,
|
"name": self.name,
|
||||||
|
@ -429,9 +429,9 @@ class Package(db.Model):
|
||||||
"score": round(self.score * 10) / 10
|
"score": round(self.score * 10) / 10
|
||||||
}
|
}
|
||||||
|
|
||||||
def getThumbnailURL(self):
|
def getThumbnailURL(self, level=2):
|
||||||
screenshot = self.screenshots.filter_by(approved=True).first()
|
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):
|
def getMainScreenshotURL(self):
|
||||||
screenshot = self.screenshots.filter_by(approved=True).first()
|
screenshot = self.screenshots.filter_by(approved=True).first()
|
||||||
|
@ -644,8 +644,10 @@ class PackageScreenshot(db.Model):
|
||||||
name=self.package.name,
|
name=self.package.name,
|
||||||
id=self.id)
|
id=self.id)
|
||||||
|
|
||||||
def getThumbnailURL(self):
|
def getThumbnailURL(self, level=2):
|
||||||
return self.url.replace("/uploads/", "/thumbnails/350x233/")
|
return self.url.replace("/uploads/", ("/thumbnails/{:d}/").format(level))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class EditRequest(db.Model):
|
class EditRequest(db.Model):
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
|
|
|
@ -21,7 +21,7 @@ from app import app
|
||||||
import os
|
import os
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
ALLOWED_RESOLUTIONS=[(350,233)]
|
ALLOWED_RESOLUTIONS=[(100,67), (270,180), (350,233)]
|
||||||
|
|
||||||
def mkdir(path):
|
def mkdir(path):
|
||||||
if not os.path.isdir(path):
|
if not os.path.isdir(path):
|
||||||
|
@ -56,15 +56,17 @@ def resize_and_crop(img_path, modified_path, size):
|
||||||
|
|
||||||
img.save(modified_path)
|
img.save(modified_path)
|
||||||
|
|
||||||
@app.route("/thumbnails/<img>")
|
|
||||||
@app.route("/thumbnails/<int:w>x<int:h>/<img>")
|
@app.route("/thumbnails/<int:level>/<img>")
|
||||||
def make_thumbnail(img, w=350, h=233):
|
def make_thumbnail(img, level):
|
||||||
if not (w, h) in ALLOWED_RESOLUTIONS:
|
if level > len(ALLOWED_RESOLUTIONS) or level <= 0:
|
||||||
abort(403)
|
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
|
source_filepath = "public/uploads/" + img
|
||||||
|
|
||||||
resize_and_crop("app/" + source_filepath, "app/" + cache_filepath, (w, h))
|
resize_and_crop("app/" + source_filepath, "app/" + cache_filepath, (w, h))
|
||||||
|
|
Loading…
Reference in New Issue