]> git.lizzy.rs Git - cheatdb.git/blob - app/views/thumbnails.py
Add support for importing generic git releases
[cheatdb.git] / app / views / thumbnails.py
1 # Content DB
2 # Copyright (C) 2018  rubenwardy
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
16
17
18 from flask import *
19 from app import app
20
21 import glob, os
22 from PIL import Image
23
24 ALLOWED_RESOLUTIONS=[(350,233)]
25
26 def mkdir(path):
27         if not os.path.isdir(path):
28                 os.mkdir(path)
29
30 mkdir("app/public/thumbnails/")
31
32 @app.route("/thumbnails/<img>")
33 @app.route("/thumbnails/<int:w>x<int:h>/<img>")
34 def make_thumbnail(img, w=350, h=233):
35         if not (w, h) in ALLOWED_RESOLUTIONS:
36                 abort(403)
37
38         mkdir("app/public/thumbnails/{}x{}/".format(w, h))
39
40         cache_filepath  = "public/thumbnails/{}x{}/{}".format(w, h, img)
41         source_filepath = "public/uploads/" + img
42
43         im = Image.open("app/" + source_filepath)
44         im.thumbnail((w, h), Image.ANTIALIAS)
45         im.save("app/" + cache_filepath, optimize=True)
46         return send_file(cache_filepath)