mirror of
https://github.com/hay-kot/mealie.git
synced 2025-08-22 14:33:33 -07:00
minify on migration
This commit is contained in:
parent
36ac416f26
commit
275f0d65f9
4 changed files with 32 additions and 5 deletions
File diff suppressed because one or more lines are too long
|
@ -127,5 +127,3 @@ class AppSettings(BaseSettings):
|
|||
|
||||
|
||||
settings = AppSettings()
|
||||
|
||||
print(settings.dict())
|
||||
|
|
|
@ -1,8 +1,12 @@
|
|||
import shutil
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi.logger import logger
|
||||
from mealie.core.config import app_dirs
|
||||
from mealie.db.database import db
|
||||
from mealie.db.db_setup import create_session
|
||||
from PIL import Image
|
||||
from sqlalchemy.orm.session import Session
|
||||
|
||||
|
||||
def minify_image(image_file: Path, min_dest: Path, tiny_dest: Path):
|
||||
|
@ -59,6 +63,28 @@ def move_all_images():
|
|||
image_file.rename(new_folder.joinpath(f"original{image_file.suffix}"))
|
||||
|
||||
|
||||
def validate_slugs_in_database(session: Session = None):
|
||||
def check_image_path(image_name: str, slug_path: str) -> bool:
|
||||
existing_path: Path = app_dirs.IMG_DIR.joinpath(image_name)
|
||||
slug_path: Path = app_dirs.IMG_DIR.joinpath(slug_path)
|
||||
|
||||
if existing_path.is_dir():
|
||||
slug_path.rename(existing_path)
|
||||
else:
|
||||
logger.info("No Image Found")
|
||||
|
||||
session = session or create_session()
|
||||
all_recipes = db.recipes.get_all(session)
|
||||
|
||||
slugs_and_images = [(x.slug, x.image) for x in all_recipes]
|
||||
|
||||
for slug, image in slugs_and_images:
|
||||
image_slug = image.split(".")[0] # Remove Extension
|
||||
if slug != image_slug:
|
||||
logger.info(f"{slug}, Doesn't Match '{image_slug}'")
|
||||
check_image_path(image, slug)
|
||||
|
||||
|
||||
def migrate_images():
|
||||
print("Checking for Images to Minify...")
|
||||
|
||||
|
@ -77,10 +103,11 @@ def migrate_images():
|
|||
org_size = sizeof_fmt(image.stat().st_size)
|
||||
dest_size = sizeof_fmt(min_dest.stat().st_size)
|
||||
tiny_size = sizeof_fmt(tiny_dest.stat().st_size)
|
||||
print(f"{image.name} Minified: {org_size} -> {dest_size} -> {tiny_size}")
|
||||
logger.info(f"{image.name} Minified: {org_size} -> {dest_size} -> {tiny_size}")
|
||||
|
||||
print("Finished Minification Check")
|
||||
logger.info("Finished Minification Check")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
migrate_images()
|
||||
validate_slugs_in_database()
|
||||
|
|
|
@ -6,6 +6,7 @@ from fastapi.logger import logger
|
|||
from mealie.core.config import app_dirs
|
||||
from mealie.db.database import db
|
||||
from mealie.schema.recipe import Recipe
|
||||
from mealie.services.image.minify import migrate_images
|
||||
from mealie.utils.unzip import unpack_zip
|
||||
from sqlalchemy.orm.session import Session
|
||||
|
||||
|
@ -89,4 +90,5 @@ def chowdown_migrate(session: Session, zip_file: Path):
|
|||
failed_images.append(image.name)
|
||||
report = {"successful": successful_recipes, "failed": failed_recipes}
|
||||
|
||||
migrate_images()
|
||||
return report
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue