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

View File

@ -8,7 +8,7 @@
<div class="col s4"> <div class="col s4">
<div class="card medium"> <div class="card medium">
<div class="card-image"> <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>
<div class="card-content"> <div class="card-content">
<span class="line-clamp card-title activator grey-text text-darken-4">{{ item.name }}</span> <span class="line-clamp card-title activator grey-text text-darken-4">{{ item.name }}</span>