start docs (AI) fix some caching logic
All checks were successful
Datadog Secrets Scanning / Datadog Static Analyzer (push) Successful in 9s
Datadog Software Composition Analysis / Datadog SBOM Generation and Upload (push) Successful in 15s
Datadog Static Analysis / Datadog Static Analyzer (push) Successful in 19s
Release / build (release) Successful in 39s

This commit is contained in:
2025-03-15 15:55:40 -04:00
parent df0610284d
commit fc211edc77
23 changed files with 496 additions and 17 deletions

View File

@ -1,12 +1,9 @@
from PIL import Image
from io import BytesIO
from functools import cache
@cache
def generate_thumbnail(image_path, resize_percent, min_width):
# Generate a unique key based on the image path, resize percentage, and minimum width
key = f"{image_path}_{resize_percent}_{min_width}"
from functools import lru_cache
@lru_cache(maxsize=512)
def generate_thumbnail(image_path, resize_percent, min_width, max_width):
# Open the image file
with Image.open(image_path) as img:
# Calculate the new size based on the resize percentage
@ -20,13 +17,19 @@ def generate_thumbnail(image_path, resize_percent, min_width):
new_width = min_width
new_height = int(new_height * scale_factor)
# Ensure the maximum width is not exceeded
if new_width > max_width:
scale_factor = max_width / new_width
new_width = max_width
new_height = int(new_height * scale_factor)
# Resize the image while maintaining the aspect ratio
img.thumbnail((new_width, new_height))
# Rotate the image based on the EXIF orientation tag
try:
exif = img._getexif()
orientation = exif.get(0x0112, 1) # 0x0112 is the EXIF orientation tag
exif = img.info['exif']
orientation = img._getexif().get(0x0112, 1) # 0x0112 is the EXIF orientation tag
if orientation == 3:
img = img.rotate(180, expand=True)
elif orientation == 6:
@ -35,12 +38,12 @@ def generate_thumbnail(image_path, resize_percent, min_width):
img = img.rotate(90, expand=True)
except (AttributeError, KeyError, IndexError):
# cases: image don't have getexif
pass
exif = None
# Save the thumbnail to a BytesIO object
thumbnail_io = BytesIO()
img_format = img.format if img.format in ["JPEG", "JPG", "PNG"] else "JPEG"
img.save(thumbnail_io, format=img_format)
img.save(thumbnail_io, format=img_format, exif=exif)
thumbnail_io.seek(0)
return (thumbnail_io.getvalue(), img_format)

View File

@ -1,9 +1,8 @@
from pathlib import Path
from src.config.config import Configuration
from src.rendering.renderer import render_page, render_error_page
from flask import send_file
from flask import send_file, request
from src.rendering.image import generate_thumbnail
from functools import lru_cache
import os
@ -114,13 +113,15 @@ class RouteManager:
if file_path.exists():
# Check to see if the file is an image, if it is, render a thumbnail
if file_path.suffix.lower() in [".jpg", ".jpeg", ".png", ".gif"]:
max_width = request.args.get("max_width", default=2048, type=int)
thumbnail_bytes, img_format = generate_thumbnail(
str(file_path), 10, 2048
str(file_path), 10, 2048, max_width
)
return (
thumbnail_bytes,
200,
{"Content-Type": f"image/{img_format.lower()}"},
{"Content-Type": f"image/{img_format.lower()}",
"cache-control": "public, max-age=31536000"},
)
return send_file(file_path)
else: