Refactor thumbnails
All checks were successful
ci/woodpecker/manual/woodpecker Pipeline was successful

This commit is contained in:
Madis Mägi 2024-07-26 20:11:02 +03:00
parent e86ad3e6ec
commit df2a7206ab
2 changed files with 24 additions and 19 deletions

View File

@ -1,6 +1,7 @@
import boto3
import pymongo
from datetime import datetime
from botocore.exceptions import ClientError
from bson.objectid import ObjectId
from flask import Blueprint, abort, g, make_response, redirect, render_template, request
from jpegtran import JPEGImage
@ -33,17 +34,23 @@ def view_inventory_view(item_id):
can_audit = "k-space:janitors" in user["groups"]
can_edit = check_edit_permission(item_id)
is_using = item_user and item_user == user["username"]
bucket=get_bucket()
photo_url = bucket.generate_presigned_url(
ClientMethod='get_object',
Params={
'Bucket': const.BUCKET_NAME,
'Key': item_id
},
ExpiresIn=3600
)
photo_url = get_image_url(item_id)
return render_template(template , **locals())
def get_image_url(item_id):
bucket=get_bucket()
try:
return bucket.generate_presigned_url(
ClientMethod='get_object',
Params={
'Bucket': const.BUCKET_NAME,
'Key': item_id
},
ExpiresIn=3600
)
except ClientError:
return None
def fetch_members_select():
choices = [(None, None)]
for username, user in g.users_lookup.items():
@ -284,6 +291,10 @@ def upload_photo(item_id):
return "File is not valid", 400
@page_inventory.app_template_filter('thumbnail')
def thumbnail_filter(item_id, dimension):
return get_scaled_photo(item_id, dimension)
@page_inventory.route("/m/photo/<item_id>/<dimension>")
def get_scaled_photo(item_id=None, dimension=0, retry=2):
dimension = int(dimension) #why
@ -296,21 +307,15 @@ def get_scaled_photo(item_id=None, dimension=0, retry=2):
return abort(404)
if item.get("thumbs", {}).get(str(dimension)):
thumb = item["thumbs"][str(dimension)]
img = get_item(thumb["name"])
if not img:
img_url = get_image_url(thumb["name"])
if not img_url:
delete_thumbs(item)
return get_scaled_photo(item_id, dimension, retry - 1)
img = JPEGImage(blob=img)
else:
make_thumb(item, dimension)
return get_scaled_photo(item_id, dimension, retry - 1)
response = make_response(img.as_blob())
response.headers.set('Content-Type', 'image/jpeg')
response.cache_control.public = True
response.cache_control.immutable = True
response.cache_control.max_age = 31536000
return response
return img_url
def get_item(key):
try:

View File

@ -8,7 +8,7 @@
<div class="col s4">
<div class="card medium">
<div class="card-image">
<img src="{% if item.has_photo %}/m/photo/{{ item._id }}/576{% else %}/static/No_image_available.svg{% endif %}" alt="no photo" loading="lazy">
<img src="{% if item.has_photo %}{{ item._id | thumbnail(576)}}{% else %}/static/No_image_available.svg{% endif %}" alt="no photo" loading="lazy">
</div>
<div class="card-content">
<span class="line-clamp card-title activator grey-text text-darken-4">{{ item.name }}</span>