diff --git a/suchwow/models.py b/suchwow/models.py index fc6ea67..9c254a9 100644 --- a/suchwow/models.py +++ b/suchwow/models.py @@ -1,5 +1,7 @@ from peewee import * +from os import path from datetime import datetime +from PIL import Image from suchwow import config @@ -21,6 +23,28 @@ class Post(Model): to_discord = BooleanField(default=False) approved = BooleanField(default=False) + def get_image_path(self, thumbnail=False): + save_path_base = path.join(config.DATA_FOLDER, "uploads") + if thumbnail: + save_path = path.join(save_path_base, self.get_thumbnail_name()) + else: + save_path = path.join(save_path_base, self.image_name) + return save_path + + def save_thumbnail(self): + try: + image = Image.open(self.get_image_path()) + image.thumbnail((200,200), Image.ANTIALIAS) + image.save(self.get_image_path(True), format=image.format, quality=90) + image.close() + return True + except: + return False + + def get_thumbnail_name(self): + s = path.splitext(self.image_name) + return s[0] + '.thumbnail' + s[1] + def show(self): return { 'id': self.id, @@ -28,6 +52,9 @@ class Post(Model): 'text': self.text, 'submitter': self.submitter, 'image_name': self.image_name, + 'image_path': self.get_image_path(), + 'thumbnail_name': self.get_thumbnail_name(), + 'thumbnail_path': self.get_image_path(True), 'readonly': self.readonly, 'hidden': self.hidden, 'account_index': self.account_index, diff --git a/suchwow/routes/post.py b/suchwow/routes/post.py index b5b40e0..239a51b 100644 --- a/suchwow/routes/post.py +++ b/suchwow/routes/post.py @@ -109,6 +109,7 @@ def create(): address_index=0 ) post.save() + post.save_thumbnail() url = url_for('post.read', id=post.id, _external=True) post_webhook(f"New post :doge2: [{post.id}]({url}) by `{submitter}` :neckbeard:") return redirect(url) diff --git a/suchwow/templates/index.html b/suchwow/templates/index.html index 77d20c2..8e9eaf8 100644 --- a/suchwow/templates/index.html +++ b/suchwow/templates/index.html @@ -14,6 +14,7 @@