]> git.lizzy.rs Git - cheatdb.git/commitdiff
Add thumbnail support
authorrubenwardy <rw@rubenwardy.com>
Tue, 29 May 2018 15:19:17 +0000 (16:19 +0100)
committerrubenwardy <rw@rubenwardy.com>
Tue, 29 May 2018 15:19:17 +0000 (16:19 +0100)
.gitignore
app/models.py
app/templates/macros/packagegridtile.html
app/views/__init__.py
app/views/thumbnails.py [new file with mode: 0644]
requirements.txt

index b37ffcc963bf3fdb33196960c37ca72b15903f1b..57dae518b35a73c9a5452ddd42c9c57822d3b935 100644 (file)
@@ -6,6 +6,7 @@ tmp
 log.txt
 *.rdb
 uploads
+thumbnails
 
 # Created by https://www.gitignore.io/api/linux,macos,python,windows
 
index d414589a9843d6cb7e1150d7bc827b1980b51c10..b3a6d5ad1285b9fee763241adc86dfce23d124ca 100644 (file)
@@ -374,9 +374,18 @@ class Package(db.Model):
                        "repo": self.repo,
                        "url": base_url + self.getDownloadURL(),
                        "release": self.getDownloadRelease().id if self.getDownloadRelease() is not None else None,
-                       "screenshots": [base_url + ss.url for ss in self.screenshots]
+                       "screenshots": [base_url + ss.url for ss in self.screenshots],
+                       "thumbnail": base_url + self.getThumbnailURL()
                }
 
+       def getThumbnailURL(self):
+               screenshot = self.screenshots.filter_by(approved=True).first()
+               return screenshot.getThumbnailURL() if screenshot is not None else None
+
+       def getMainScreenshotURL(self):
+               screenshot = self.screenshots.filter_by(approved=True).first()
+               return screenshot.url if screenshot is not None else None
+
        def getDetailsURL(self):
                return url_for("package_page",
                                author=self.author.username, name=self.name)
@@ -409,10 +418,6 @@ class Package(db.Model):
                return url_for("package_download_page",
                                author=self.author.username, name=self.name)
 
-       def getMainScreenshotURL(self):
-               screenshot = self.screenshots.filter_by(approved=True).first()
-               return screenshot.url if screenshot is not None else None
-
        def getDownloadRelease(self):
                for rel in self.releases:
                        if rel.approved:
@@ -575,7 +580,7 @@ class PackageScreenshot(db.Model):
                                id=self.id)
 
        def getThumbnailURL(self):
-               return self.url  # TODO
+               return self.url.replace("/uploads/", "/thumbnails/332x221/")
 
 class EditRequest(db.Model):
        id           = db.Column(db.Integer, primary_key=True)
index 0ba39dbb5d7b520fe38e44c1d0fca16240690d96..45f1f4598eceeccf8a4557013cbf4e5da731fa78 100644 (file)
@@ -1,6 +1,6 @@
 {% macro render_pkgtile(package) -%}
        <li><a href="{{ package.getDetailsURL() }}"
-               style="background-image: url({{ package.getMainScreenshotURL() or '/static/placeholder.png' }});">
+               style="background-image: url({{ package.getThumbnailURL() or '/static/placeholder.png' }});">
                <div class="packagegridscrub"></div>
                <div class="packagegridinfo">
                        <h3>{{ package.title }} by {{ package.author.display_name }}</h3>
index c28ff56e4c378c3dc4cbf4b78d03d937d2b9ae33..21ed40d872dd0b20ae330737336efda5d6f5e236 100644 (file)
@@ -51,7 +51,7 @@ def home_page():
        packages = query.order_by(db.desc(Package.created_at)).limit(15).all()
        return render_template("index.html", packages=packages, count=count)
 
-from . import users, githublogin, packages, sass, tasks, admin, notifications, tagseditor, meta
+from . import users, githublogin, packages, sass, tasks, admin, notifications, tagseditor, meta, thumbnails
 
 @menu.register_menu(app, ".help", "Help", order=19, endpoint_arguments_constructor=lambda: { 'path': 'help' })
 @app.route('/<path:path>/')
diff --git a/app/views/thumbnails.py b/app/views/thumbnails.py
new file mode 100644 (file)
index 0000000..d33e74c
--- /dev/null
@@ -0,0 +1,45 @@
+# Content DB
+# Copyright (C) 2018  rubenwardy
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+
+from flask import *
+from app import app
+
+import glob, os
+from PIL import Image
+
+ALLOWED_RESOLUTIONS=[(332,221)]
+
+def mkdir(path):
+       if not os.path.isdir(path):
+               os.mkdir(path)
+
+@app.route("/thumbnails/<img>")
+@app.route("/thumbnails/<int:w>x<int:h>/<img>")
+def make_thumbnail(img, w=332, h=221):
+       if not (w, h) in ALLOWED_RESOLUTIONS:
+               abort(403)
+
+       mkdir("app/public/thumbnails/")
+       mkdir("app/public/thumbnails/332x221/")
+
+       cache_filepath  = "public/thumbnails/{}x{}/{}".format(w, h, img)
+       source_filepath = "public/uploads/" + img
+
+       im = Image.open("app/" + source_filepath)
+       im.thumbnail((w, h), Image.ANTIALIAS)
+       im.save("app/" + cache_filepath, optimize=True)
+       return send_file(cache_filepath)
index 49751a6e4da8b45d1f37e02bbae420e4076f236d..2caa7fc893252e7574fe7f51cb74a58cdcb1be66 100644 (file)
@@ -12,3 +12,4 @@ beautifulsoup4==4.6.0
 lxml==4.2.1
 Flask-FlatPages==0.6
 Flask-Migrate==2.1.1
+pillow==5.1.0