mirror of
https://github.com/mealie-recipes/mealie.git
synced 2025-12-28 05:05:12 -05:00
bug/bug-fixes (#424)
* fix image write/caching * Caddyfile Caching header * more aggressive caching Co-authored-by: hay-kot <hay-kot@pm.me>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
from logging import getLogger
|
||||
from random import randint
|
||||
|
||||
from mealie.db.db_base import BaseDocument
|
||||
from mealie.db.models.event import Event, EventNotification
|
||||
@@ -34,10 +35,10 @@ class _Recipes(BaseDocument):
|
||||
|
||||
def update_image(self, session: Session, slug: str, extension: str = None) -> str:
|
||||
entry: RecipeModel = self._query_one(session, match_value=slug)
|
||||
entry.image = f"{slug}.{extension}"
|
||||
entry.image = randint(0, 255)
|
||||
session.commit()
|
||||
|
||||
return f"{slug}.{extension}"
|
||||
return entry.image
|
||||
|
||||
def count_uncategorized(self, session: Session, count=True, override_schema=None) -> int:
|
||||
return self._count_attribute(
|
||||
|
||||
@@ -128,19 +128,18 @@ def delete_recipe(
|
||||
raise HTTPException(status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
|
||||
@router.put("/{recipe_slug}/image")
|
||||
@router.put("/{recipe_slug}/image", dependencies=[Depends(get_current_user)])
|
||||
def update_recipe_image(
|
||||
recipe_slug: str,
|
||||
image: bytes = File(...),
|
||||
extension: str = Form(...),
|
||||
session: Session = Depends(generate_session),
|
||||
current_user=Depends(get_current_user),
|
||||
):
|
||||
""" Removes an existing image and replaces it with the incoming file. """
|
||||
response = write_image(recipe_slug, image, extension)
|
||||
db.recipes.update_image(session, recipe_slug, extension)
|
||||
write_image(recipe_slug, image, extension)
|
||||
new_version = db.recipes.update_image(session, recipe_slug, extension)
|
||||
|
||||
return response
|
||||
return {"image": new_version}
|
||||
|
||||
|
||||
@router.post("/{recipe_slug}/image")
|
||||
|
||||
@@ -36,7 +36,7 @@ def write_image(recipe_slug: str, file_data: bytes, extension: str) -> Path:
|
||||
shutil.copyfileobj(file_data, f)
|
||||
|
||||
print(image_path)
|
||||
minify.minify_image(image_path)
|
||||
minify.minify_image(image_path, force=True)
|
||||
|
||||
return image_path
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ def get_image_sizes(org_img: Path, min_img: Path, tiny_img: Path) -> ImageSizes:
|
||||
return ImageSizes(org=sizeof_fmt(org_img), min=sizeof_fmt(min_img), tiny=sizeof_fmt(tiny_img))
|
||||
|
||||
|
||||
def minify_image(image_file: Path) -> ImageSizes:
|
||||
def minify_image(image_file: Path, force=False) -> ImageSizes:
|
||||
"""Minifies an image in it's original file format. Quality is lost
|
||||
|
||||
Args:
|
||||
@@ -39,7 +39,7 @@ def minify_image(image_file: Path) -> ImageSizes:
|
||||
min_dest = image_file.parent.joinpath("min-original.webp")
|
||||
tiny_dest = image_file.parent.joinpath("tiny-original.webp")
|
||||
|
||||
if min_dest.exists() and tiny_dest.exists() and org_dest.exists():
|
||||
if min_dest.exists() and tiny_dest.exists() and org_dest.exists() and not force:
|
||||
return
|
||||
try:
|
||||
img = Image.open(image_file)
|
||||
@@ -56,7 +56,8 @@ def minify_image(image_file: Path) -> ImageSizes:
|
||||
|
||||
cleanup_images = True
|
||||
|
||||
except Exception:
|
||||
except Exception as e:
|
||||
logger.error(e)
|
||||
shutil.copy(image_file, min_dest)
|
||||
shutil.copy(image_file, tiny_dest)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user