[main] Image webp
All checks were successful
Build, Push, and Deploy to Nomad / docker-nomad (push) Successful in 47s

This commit is contained in:
2024-12-30 20:47:04 +01:00
parent bf1401c32b
commit 13bc417d45
11 changed files with 12 additions and 2 deletions

View File

@@ -1,4 +1,6 @@
import os
from pathlib import Path
from fastapi import HTTPException
from fastapi.responses import FileResponse
from fastapi import APIRouter, Request, FastAPI
@@ -27,6 +29,13 @@ class FileHandler:
def dest_filename(self) -> str:
base_url = "static/images/{category}/{image_type}/{filename}"
return base_url.format( category = self.category, image_type = self.image_type, filename = self.filename )
@property
def dest_filename_webp(self) -> str:
base_url = "static/images/{category}/{image_type}/{filename}"
path = Path( base_url.format( category = self.category, image_type = self.image_type, filename = self.filename ) )
if path.suffix != ".webp":
path = path.with_suffix( ".webp" )
return str(path)
@property
def dest_path(self) -> str:
@@ -139,7 +148,8 @@ class ImageService:
resized_img = img.resize( (width, height), Image.Resampling.LANCZOS )
os.makedirs(file_path.dest_path,exist_ok = True)
resized_img.save( file_path.dest_filename )
resized_img.save( file_path.dest_filename_webp, format = "WEBP", quality = 90 ) # Adjust quality as needed
print(file_path.dest_filename_webp)
async def get_image(self, category: str, type: str, filename: str):
file_path = self._resolve_path( category, type, filename )
@@ -157,7 +167,7 @@ class ImageService:
file_path = FileHandler(category = category,image_type = image_type,filename = filename)
self.validate_image( file_path, width = width,height=height, overwrite = True )
tag = f'<img src="/{file_path.dest_filename}" alt="{alt}"'
tag = f'<img src="/{file_path.dest_filename_webp}" alt="{alt}"'
if width:
tag += f' width="{width}"'
if height: