From 639d920d05ca907fffea2951f78a96f961a8ab22 Mon Sep 17 00:00:00 2001 From: rimu <3310831+rimu@users.noreply.github.com> Date: Sat, 10 Feb 2024 06:41:24 +1300 Subject: [PATCH] reduce storage usage by user banners --- app/activitypub/util.py | 2 +- app/community/util.py | 5 +++-- app/main/routes.py | 15 +++++++++++++-- app/models.py | 8 ++++---- app/templates/post/_post_full.html | 6 +++--- 5 files changed, 24 insertions(+), 12 deletions(-) diff --git a/app/activitypub/util.py b/app/activitypub/util.py index 138ecbd6..e87dd99d 100644 --- a/app/activitypub/util.py +++ b/app/activitypub/util.py @@ -374,7 +374,7 @@ def actor_json_to_model(activity_json, address, server): if user.avatar_id: make_image_sizes(user.avatar_id, 40, 250, 'users') if user.cover_id: - make_image_sizes(user.cover_id, 700, 1600, 'users') + make_image_sizes(user.cover_id, 878, None, 'users') return user elif activity_json['type'] == 'Group': if 'attributedTo' in activity_json: # lemmy and mbin diff --git a/app/community/util.py b/app/community/util.py index 36b423f3..90dc0706 100644 --- a/app/community/util.py +++ b/app/community/util.py @@ -366,13 +366,14 @@ def save_banner_file(banner_file, directory='communities') -> File: img_height = img.height # save a second, smaller, version as a thumbnail - img.thumbnail((700, 500)) + img.thumbnail((878, 500)) img.save(final_place_thumbnail, format="WebP", quality=93) thumbnail_width = img.width thumbnail_height = img.height file = File(file_path=final_place, file_name=new_filename + file_ext, alt_text=f'{directory} banner', - width=img_width, height=img_height, thumbnail_width=thumbnail_width, thumbnail_height=thumbnail_height) + width=img_width, height=img_height, thumbnail_path=final_place_thumbnail, + thumbnail_width=thumbnail_width, thumbnail_height=thumbnail_height) db.session.add(file) return file else: diff --git a/app/main/routes.py b/app/main/routes.py index 6c5df246..91351b54 100644 --- a/app/main/routes.py +++ b/app/main/routes.py @@ -1,3 +1,4 @@ +import os.path from datetime import datetime, timedelta from math import log from random import randint @@ -248,10 +249,20 @@ def keyboard_shortcuts(): @bp.route('/test') def test(): + deleted = 0 + for user in User.query.all(): + if not user.is_local(): + if user.cover_id: + file = user.cover + if file.file_path and file.thumbnail_path: + if os.path.exists(file.file_path): + os.unlink(file.file_path) + deleted += 1 + file.file_path = '' - themes = theme_list() + db.session.commit() - return str(themes) + return str(deleted) + ' done' return current_app.config['SERVER_NAME'] diff --git a/app/models.py b/app/models.py index ae274b6d..2145fb49 100644 --- a/app/models.py +++ b/app/models.py @@ -433,11 +433,11 @@ class User(UserMixin, db.Model): def cover_image(self) -> str: if self.cover_id is not None: - if self.cover.file_path is not None: - if self.cover.file_path.startswith('app/'): - return self.cover.file_path.replace('app/', '/') + if self.cover.thumbnail_path is not None: + if self.cover.thumbnail_path.startswith('app/'): + return self.cover.thumbnail_path.replace('app/', '/') else: - return self.cover.file_path + return self.cover.thumbnail_path if self.cover.source_url is not None: if self.cover.source_url.startswith('app/'): return self.cover.source_url.replace('app/', '/') diff --git a/app/templates/post/_post_full.html b/app/templates/post/_post_full.html index eea01101..66173d30 100644 --- a/app/templates/post/_post_full.html +++ b/app/templates/post/_post_full.html @@ -70,7 +70,7 @@ {% if post.type == POST_TYPE_LINK and post.image_id and not (post.url and 'youtube.com' in post.url) %}
{% if post.reports and current_user.is_authenticated and post.community.is_moderator(current_user) %} @@ -88,12 +88,12 @@ {% elif post.type == POST_TYPE_IMAGE %}
{% else %} {% if post.image_id and not (post.url and 'youtube.com' in post.url) %} + width="{{ post.image.thumbnail_width }}" height="{{ post.image.thumbnail_height }}" loading="lazy" /> {% endif %} {% endif %}