feat: load default thumbnail on fetch rather than copying default thumbnail to user/product on creation

This commit is contained in:
s3lph 2024-12-11 00:04:36 +01:00
parent 583107ac63
commit 418d7ffea9
Signed by: s3lph
GPG key ID: 0AA29A52FB33CFB5
4 changed files with 14 additions and 34 deletions

View file

@ -61,6 +61,17 @@ def _init(config: Dict[str, Any]):
template_init(config)
@bottle.route('/static/upload/thumbnails/<resource>/<filename>')
def get_thumbnail(resource: str, filename: str):
config = get_config()
static: str = os.path.abspath(config['staticroot'])
rpath: str = os.path.join(static, 'upload/thumbnails', resource)
resp = bottle.static_file(filename, root=rpath)
if resp.status_code != 200:
resp = bottle.static_file('default.png', root=rpath)
return resp
@bottle.route('/static/<filename:path>')
def serve_static_files(filename: str):
config = get_config()

View file

@ -84,17 +84,6 @@ def handle_change(args: FormsDict, files: FormsDict, db: MatematDatabase):
newuser: User = db.create_user(username, password, email, member=is_member, admin=is_admin,
logout_after_purchase=logout_after_purchase)
# If a default avatar is set, copy it to the user's avatar path
# Create the absolute path of the upload directory
abspath: str = os.path.join(os.path.abspath(config['UploadDir']), 'thumbnails/users/')
# Derive the individual paths
default: str = os.path.join(abspath, 'default.png')
userimg: str = os.path.join(abspath, f'{newuser.id}.png')
# Copy the default image, if it exists
if os.path.exists(default):
copyfile(default, userimg, follow_symlinks=True)
# The user requested to create a new product
elif change == 'newproduct':
# Only create a new product if all required properties of the product are present in the request arguments
@ -130,17 +119,6 @@ def handle_change(args: FormsDict, files: FormsDict, db: MatematDatabase):
except OSError as e:
Notification.error(str(e), decay=True)
return
else:
# If no image was uploaded and a default avatar is set, copy it to the product's avatar path
# Create the absolute path of the upload directory
abspath: str = os.path.join(os.path.abspath(config['UploadDir']), 'thumbnails/products/')
# Derive the individual paths
default: str = os.path.join(abspath, 'default.png')
userimg: str = os.path.join(abspath, f'{newproduct.id}.png')
# Copy the default image, if it exists
if os.path.exists(default):
copyfile(default, userimg, follow_symlinks=True)
# The user requested to restock a product
elif change == 'restock':

View file

@ -48,16 +48,7 @@ def signup_user(args: FormsDict, files: FormsDict, db: MatematDatabase) -> User:
image.thumbnail((150, 150), Image.LANCZOS)
# Write the image to the file
image.save(os.path.join(abspath, f'{new_user.id}.png'), 'PNG')
else:
# If a default avatar is set, copy it to the user's avatar path
# Create the absolute path of the upload directory
abspath: str = os.path.join(os.path.abspath(config['UploadDir']), 'thumbnails/users/')
# Derive the individual paths
default: str = os.path.join(abspath, 'default.png')
userimg: str = os.path.join(abspath, f'{new_user.id}.png')
# Copy the default image, if it exists
if os.path.exists(default):
copyfile(default, userimg, follow_symlinks=True)
return new_user

View file

@ -134,7 +134,7 @@
<div class="col-sm-2 g-4">
<div class="card h-100">
<img class="card-img-top" src="/static/upload/thumbnails/users/default.png" alt="Default user avatar" />
<img class="card-img-top" src="/static/upload/thumbnails/users/default.png?cacheBuster={{ now }}" alt="Default user avatar" />
<div class="card-body">
<label class="card-title" for="admin-default-images-user">Default user avatar</label>
<div class="card-text">
@ -146,7 +146,7 @@
<div class="col-sm-2 g-4">
<div class="card h-100">
<img class="card-img-top" src="/static/upload/thumbnails/product/default.png" alt="Default product image" />
<img class="card-img-top" src="/static/upload/thumbnails/products/default.png?cacheBuster={{ now }}" alt="Default product image" />
<div class="card-body">
<label class="card-title" for="admin-default-images-product">Default product image</label>
<div class="card-text">