reduce storage usage by user banners

This commit is contained in:
rimu 2024-02-10 06:41:24 +13:00
parent 602c83699a
commit 639d920d05
5 changed files with 24 additions and 12 deletions

View file

@ -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

View file

@ -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:

View file

@ -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']

View file

@ -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/', '/')

View file

@ -70,7 +70,7 @@
{% if post.type == POST_TYPE_LINK and post.image_id and not (post.url and 'youtube.com' in post.url) %}
<div class="url_thumbnail">
<a href="{{ post.url }}" target="_blank" rel="nofollow ugc" class="post_link"><img src="{{ post.image.thumbnail_url() }}" alt="{{ post.image.alt_text if post.image.alt_text else post.title }}"
width="{{ post.image.thumbnail_width }}" height="{{ post.image.thumbnail_height }}" /></a>
width="{{ post.image.thumbnail_width }}" height="{{ post.image.thumbnail_height }}" loading="lazy" /></a>
</div>
{% endif %}
<p>{% 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 %}
<div class="post_image">
<a href="{{ post.image.view_url() }}" target="_blank" class="post_link" rel="nofollow ugc"><img src="{{ post.image.view_url() }}" alt="{{ post.image.alt_text if post.image.alt_text else post.title }}"
width="{{ post.image.width }}" height="{{ post.image.height }}" /></a>
width="{{ post.image.width }}" height="{{ post.image.height }}" loading="lazy" /></a>
</div>
{% else %}
{% if post.image_id and not (post.url and 'youtube.com' in post.url) %}
<a href="{{ post.image.view_url() }}" target="_blank" class="post_link" rel="nofollow ugc"><img src="{{ post.image.thumbnail_url() }}" alt="{{ post.image.alt_text if post.image.alt_text else post.title }}"
width="{{ post.image.thumbnail_width }}" height="{{ post.image.thumbnail_height }}" /></a>
width="{{ post.image.thumbnail_width }}" height="{{ post.image.thumbnail_height }}" loading="lazy" /></a>
{% endif %}
{% endif %}
<div class="post_body">