From 44621fbbdb5fa4d36c607e73f37dd76e72fbf575 Mon Sep 17 00:00:00 2001 From: lza_menace Date: Sun, 27 Nov 2022 15:55:46 -0800 Subject: [PATCH] use thumbnails --- nerochan/cli.py | 5 +++-- nerochan/helpers.py | 2 +- nerochan/models.py | 26 +++++++++++++++++++++----- nerochan/templates/includes/head.html | 2 +- nerochan/templates/index.html | 2 +- 5 files changed, 27 insertions(+), 10 deletions(-) diff --git a/nerochan/cli.py b/nerochan/cli.py index eba2eba..d7c0cac 100644 --- a/nerochan/cli.py +++ b/nerochan/cli.py @@ -54,14 +54,15 @@ def cli(app): with open(fp, 'wb') as download: download.write(content) click.echo(f'[+] Downloaded {art}') - if not Artwork.select().where(Artwork.path == bn).first(): + if not Artwork.select().where(Artwork.image == bn).first(): artwork = Artwork( creator=_user, - path=bn, + image=bn, approved=True, title=f'i made {bn}', description='' ) artwork.save() + artwork.generate_thumbnail() click.echo(f'[+] Created artwork {artwork.id} for {bn}') return app diff --git a/nerochan/helpers.py b/nerochan/helpers.py index 4f4ad13..f90b4bf 100644 --- a/nerochan/helpers.py +++ b/nerochan/helpers.py @@ -16,4 +16,4 @@ def make_wallet_rpc(method, data={}): res = w._backend.raw_request(method, data) return res except Exception as e: - raise e + raise e \ No newline at end of file diff --git a/nerochan/models.py b/nerochan/models.py index daf4e50..93694dc 100644 --- a/nerochan/models.py +++ b/nerochan/models.py @@ -1,7 +1,8 @@ +from os import path from datetime import datetime from secrets import token_urlsafe -from flask import url_for +from PIL import Image import peewee as pw @@ -80,16 +81,31 @@ class Artwork(pw.Model): """ id = pw.AutoField() creator = pw.ForeignKeyField(User) - path = pw.CharField() + image = pw.CharField() + thumbnail = pw.CharField(null=True) upload_date = pw.DateTimeField(default=datetime.utcnow) last_edit_date = pw.DateTimeField(default=datetime.utcnow) approved = pw.BooleanField(default=False) hidden = pw.BooleanField(default=False) title = pw.CharField() description = pw.TextField(null=True) - - def get_image_url(self): - return url_for('main.uploaded_file', filename=self.path) + + def generate_thumbnail(self): + _t = f'thumbnail-{self.image}' + i = f'{config.DATA_PATH}/uploads/{self.image}' + t = f'{config.DATA_PATH}/uploads/{_t}' + if path.exists(t): + return True + try: + image = Image.open(i) + image.thumbnail((150,150), Image.ANTIALIAS) + image.save(t, format=image.format, quality=75) + image.close() + self.thumbnail = _t + self.save() + return True + except: + return False class Meta: database = db diff --git a/nerochan/templates/includes/head.html b/nerochan/templates/includes/head.html index dba4e24..473cb46 100644 --- a/nerochan/templates/includes/head.html +++ b/nerochan/templates/includes/head.html @@ -1,7 +1,7 @@ {{ config.SITE_NAME }} - + diff --git a/nerochan/templates/index.html b/nerochan/templates/index.html index 7f15809..82a8779 100644 --- a/nerochan/templates/index.html +++ b/nerochan/templates/index.html @@ -7,7 +7,7 @@

Latest Artworks

{% for artwork in feed['artwork'] %}

{{ artwork.title }}

- + {% endfor %}