mirror of
https://github.com/mealie-recipes/mealie.git
synced 2025-12-12 21:35:18 -05:00
refactor/docker-updates (#369)
* convert all images to webp * consolidate docker files * serve images wiith caddy * consolidate docker files * new slim-buster image * set image url * add image path * remove print * set image path correctly * cleanup * caddy proxy path * docs Co-authored-by: hay-kot <hay-kot@pm.me>
This commit is contained in:
@@ -118,7 +118,6 @@ class RecipeModel(SqlAlchemyBase, BaseMixins):
|
||||
|
||||
# Mealie Specific
|
||||
self.settings = RecipeSettings(**settings) if settings else RecipeSettings()
|
||||
print(self.settings)
|
||||
self.tags = [Tag.create_if_not_exist(session=session, name=tag) for tag in tags]
|
||||
self.slug = slug
|
||||
self.date_added = date_added
|
||||
|
||||
@@ -2,6 +2,8 @@ from enum import Enum
|
||||
|
||||
from fastapi import APIRouter, Depends, File, Form, HTTPException, status
|
||||
from fastapi.responses import FileResponse
|
||||
from mealie.core.config import app_dirs
|
||||
from mealie.core.root_logger import get_logger
|
||||
from mealie.db.database import db
|
||||
from mealie.db.db_setup import generate_session
|
||||
from mealie.routes.deps import get_current_user
|
||||
@@ -11,6 +13,7 @@ from mealie.services.scraper.scraper import create_from_url
|
||||
from sqlalchemy.orm.session import Session
|
||||
|
||||
router = APIRouter(prefix="/api/recipes", tags=["Recipe CRUD"])
|
||||
logger = get_logger()
|
||||
|
||||
|
||||
@router.post("/create", status_code=201, response_model=str)
|
||||
@@ -104,22 +107,15 @@ def delete_recipe(
|
||||
|
||||
|
||||
class ImageType(str, Enum):
|
||||
original = "original"
|
||||
small = "small"
|
||||
tiny = "tiny"
|
||||
original = "original.webp"
|
||||
small = "min-original.webp"
|
||||
tiny = "tiny-original.webp"
|
||||
|
||||
|
||||
@router.get("/{recipe_slug}/image")
|
||||
async def get_recipe_img(recipe_slug: str, image_type: ImageType = ImageType.original):
|
||||
@router.get("/image/{recipe_slug}/{file_name}")
|
||||
async def get_recipe_img(recipe_slug: str, file_name: ImageType = ImageType.original):
|
||||
""" Takes in a recipe slug, returns the static image """
|
||||
if image_type == ImageType.original:
|
||||
which_image = IMG_OPTIONS.ORIGINAL_IMAGE
|
||||
elif image_type == ImageType.small:
|
||||
which_image = IMG_OPTIONS.MINIFIED_IMAGE
|
||||
elif image_type == ImageType.tiny:
|
||||
which_image = IMG_OPTIONS.TINY_IMAGE
|
||||
|
||||
recipe_image = read_image(recipe_slug, image_type=which_image)
|
||||
recipe_image = app_dirs.IMG_DIR.joinpath(recipe_slug, file_name.value)
|
||||
if recipe_image:
|
||||
return FileResponse(recipe_image)
|
||||
else:
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
#!/bin/sh
|
||||
#!/bin/bash
|
||||
|
||||
# Get Reload Arg `run.sh reload` for dev server
|
||||
ARG1=${1:-production}
|
||||
|
||||
# Set Script Directory - Used for running the script from a different directory.
|
||||
# DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
|
||||
|
||||
# # Initialize Database Prerun
|
||||
poetry run python /app/mealie/db/init_db.py
|
||||
poetry run python /app/mealie/services/image/minify.py
|
||||
@@ -15,12 +12,12 @@ poetry run python /app/mealie/services/image/minify.py
|
||||
# Migrations
|
||||
# Set Port from ENV Variable
|
||||
|
||||
if [[ "$ARG1" = "reload" ]]
|
||||
if [ "$ARG1" == "reload" ]
|
||||
then
|
||||
echo "Hot Reload!"
|
||||
|
||||
# Start API
|
||||
uvicorn mealie.app:app --host 0.0.0.0 --port 9000 --reload
|
||||
python /app/mealie/app.py
|
||||
else
|
||||
echo "Production"
|
||||
# Web Server
|
||||
|
||||
@@ -35,21 +35,31 @@ def minify_image(image_file: Path) -> ImageSizes:
|
||||
min_dest (Path): FULL Destination File Path
|
||||
tiny_dest (Path): FULL Destination File Path
|
||||
"""
|
||||
min_dest = image_file.parent.joinpath(f"min-original{image_file.suffix}")
|
||||
tiny_dest = image_file.parent.joinpath(f"tiny-original{image_file.suffix}")
|
||||
def cleanup(dir: Path) -> None:
|
||||
for file in dir.glob("*.*"):
|
||||
if file.suffix != ".webp":
|
||||
file.unlink()
|
||||
|
||||
if min_dest.exists() and tiny_dest.exists():
|
||||
org_dest = image_file.parent.joinpath(f"original.webp")
|
||||
min_dest = image_file.parent.joinpath(f"min-original.webp")
|
||||
tiny_dest = image_file.parent.joinpath(f"tiny-original.webp")
|
||||
|
||||
if min_dest.exists() and tiny_dest.exists() and org_dest.exists():
|
||||
return
|
||||
try:
|
||||
img = Image.open(image_file)
|
||||
|
||||
img.save(org_dest, "WEBP")
|
||||
basewidth = 720
|
||||
wpercent = basewidth / float(img.size[0])
|
||||
hsize = int((float(img.size[1]) * float(wpercent)))
|
||||
img = img.resize((basewidth, hsize), Image.ANTIALIAS)
|
||||
img.save(min_dest, quality=70)
|
||||
img.save(min_dest, "WEBP", quality=70)
|
||||
|
||||
tiny_image = crop_center(img)
|
||||
tiny_image.save(tiny_dest, quality=70)
|
||||
tiny_image.save(tiny_dest, "WEBP", quality=70)
|
||||
|
||||
cleanup_images = True
|
||||
|
||||
except Exception:
|
||||
shutil.copy(image_file, min_dest)
|
||||
@@ -58,7 +68,10 @@ def minify_image(image_file: Path) -> ImageSizes:
|
||||
image_sizes = get_image_sizes(image_file, min_dest, tiny_dest)
|
||||
|
||||
logger.info(f"{image_file.name} Minified: {image_sizes.org} -> {image_sizes.min} -> {image_sizes.tiny}")
|
||||
|
||||
|
||||
if cleanup_images:
|
||||
cleanup(image_file.parent)
|
||||
|
||||
return image_sizes
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user