mirror of
				https://github.com/mealie-recipes/mealie.git
				synced 2025-10-31 02:03:35 -04:00 
			
		
		
		
	refactor/image-minification (#285)
* refactor image minification calls * remove nusiance logs Co-authored-by: hay-kot <hay-kot@pm.me>
This commit is contained in:
		| @@ -61,8 +61,8 @@ class Category(SqlAlchemyBase): | |||||||
|         test_slug = slugify(name) |         test_slug = slugify(name) | ||||||
|         result = session.query(Category).filter(Category.slug == test_slug).one_or_none() |         result = session.query(Category).filter(Category.slug == test_slug).one_or_none() | ||||||
|         if result: |         if result: | ||||||
|             logger.info("Category exists, associating recipe") |             logger.debug("Category exists, associating recipe") | ||||||
|             return result |             return result | ||||||
|         else: |         else: | ||||||
|             logger.info("Category doesn't exists, creating tag") |             logger.debug("Category doesn't exists, creating tag") | ||||||
|             return Category(name=name) |             return Category(name=name) | ||||||
|   | |||||||
| @@ -37,8 +37,8 @@ class Tag(SqlAlchemyBase): | |||||||
|         result = session.query(Tag).filter(Tag.slug == test_slug).one_or_none() |         result = session.query(Tag).filter(Tag.slug == test_slug).one_or_none() | ||||||
|  |  | ||||||
|         if result: |         if result: | ||||||
|             logger.info("Tag exists, associating recipe") |             logger.debug("Tag exists, associating recipe") | ||||||
|             return result |             return result | ||||||
|         else: |         else: | ||||||
|             logger.info("Tag doesn't exists, creating tag") |             logger.debug("Tag doesn't exists, creating tag") | ||||||
|             return Tag(name=name) |             return Tag(name=name) | ||||||
|   | |||||||
| @@ -51,7 +51,7 @@ def rename_image(original_slug, new_slug) -> Path: | |||||||
|     return new_path |     return new_path | ||||||
|  |  | ||||||
|  |  | ||||||
| def write_image(recipe_slug: str, file_data: bytes, extension: str) -> Path.name: | def write_image(recipe_slug: str, file_data: bytes, extension: str) -> Path: | ||||||
|     try: |     try: | ||||||
|         delete_image(recipe_slug) |         delete_image(recipe_slug) | ||||||
|     except: |     except: | ||||||
| @@ -68,7 +68,7 @@ def write_image(recipe_slug: str, file_data: bytes, extension: str) -> Path.name | |||||||
|     else: |     else: | ||||||
|         shutil.copy2(file_data, image_path) |         shutil.copy2(file_data, image_path) | ||||||
|  |  | ||||||
|     minify.migrate_images() |     minify.minify_image(image_path) | ||||||
|  |  | ||||||
|     return image_path |     return image_path | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,4 +1,5 @@ | |||||||
| import shutil | import shutil | ||||||
|  | from dataclasses import dataclass | ||||||
| from pathlib import Path | from pathlib import Path | ||||||
|  |  | ||||||
| from mealie.core import root_logger | from mealie.core import root_logger | ||||||
| @@ -11,7 +12,22 @@ from sqlalchemy.orm.session import Session | |||||||
| logger = root_logger.get_logger() | logger = root_logger.get_logger() | ||||||
|  |  | ||||||
|  |  | ||||||
| def minify_image(image_file: Path, min_dest: Path, tiny_dest: Path): | @dataclass | ||||||
|  | class ImageSizes: | ||||||
|  |     org: str | ||||||
|  |     min: str | ||||||
|  |     tiny: str | ||||||
|  |  | ||||||
|  |  | ||||||
|  | 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: | ||||||
|     """Minifies an image in it's original file format. Quality is lost |     """Minifies an image in it's original file format. Quality is lost | ||||||
|  |  | ||||||
|     Args: |     Args: | ||||||
| @@ -19,6 +35,11 @@ def minify_image(image_file: Path, min_dest: Path, tiny_dest: Path): | |||||||
|         min_dest (Path): FULL Destination File Path |         min_dest (Path): FULL Destination File Path | ||||||
|         tiny_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}") | ||||||
|  |  | ||||||
|  |     if min_dest.exists() and tiny_dest.exists(): | ||||||
|  |         return | ||||||
|     try: |     try: | ||||||
|         img = Image.open(image_file) |         img = Image.open(image_file) | ||||||
|         basewidth = 720 |         basewidth = 720 | ||||||
| @@ -34,6 +55,12 @@ def minify_image(image_file: Path, min_dest: Path, tiny_dest: Path): | |||||||
|         shutil.copy(image_file, min_dest) |         shutil.copy(image_file, min_dest) | ||||||
|         shutil.copy(image_file, tiny_dest) |         shutil.copy(image_file, tiny_dest) | ||||||
|  |  | ||||||
|  |     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}") | ||||||
|  |      | ||||||
|  |     return image_sizes | ||||||
|  |  | ||||||
|  |  | ||||||
| def crop_center(pil_img, crop_width=300, crop_height=300): | def crop_center(pil_img, crop_width=300, crop_height=300): | ||||||
|     img_width, img_height = pil_img.size |     img_width, img_height = pil_img.size | ||||||
| @@ -47,7 +74,10 @@ def crop_center(pil_img, crop_width=300, crop_height=300): | |||||||
|     ) |     ) | ||||||
|  |  | ||||||
|  |  | ||||||
| def sizeof_fmt(size, decimal_places=2): | def sizeof_fmt(file_path: Path, decimal_places=2): | ||||||
|  |     if not file_path.exists(): | ||||||
|  |         return "(File Not Found)" | ||||||
|  |     size = file_path.stat().st_size | ||||||
|     for unit in ["B", "kB", "MB", "GB", "TB", "PB"]: |     for unit in ["B", "kB", "MB", "GB", "TB", "PB"]: | ||||||
|         if size < 1024.0 or unit == "PiB": |         if size < 1024.0 or unit == "PiB": | ||||||
|             break |             break | ||||||
| @@ -91,24 +121,13 @@ def validate_slugs_in_database(session: Session = None): | |||||||
|  |  | ||||||
|  |  | ||||||
| def migrate_images(): | def migrate_images(): | ||||||
|     print("Checking for Images to Minify...") |     logger.info("Checking for Images to Minify...") | ||||||
|  |  | ||||||
|     move_all_images() |     move_all_images() | ||||||
|  |  | ||||||
|     # Minify Loop |  | ||||||
|     for image in app_dirs.IMG_DIR.glob("*/original.*"): |     for image in app_dirs.IMG_DIR.glob("*/original.*"): | ||||||
|         min_dest = image.parent.joinpath(f"min-original{image.suffix}") |  | ||||||
|         tiny_dest = image.parent.joinpath(f"tiny-original{image.suffix}") |  | ||||||
|  |  | ||||||
|         if min_dest.exists() and tiny_dest.exists(): |         minify_image(image) | ||||||
|             continue |  | ||||||
|  |  | ||||||
|         minify_image(image, min_dest, tiny_dest) |  | ||||||
|  |  | ||||||
|         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) |  | ||||||
|         logger.info(f"{image.name} Minified: {org_size} -> {dest_size} -> {tiny_size}") |  | ||||||
|  |  | ||||||
|     logger.info("Finished Minification Check") |     logger.info("Finished Minification Check") | ||||||
|  |  | ||||||
|   | |||||||
| @@ -110,7 +110,6 @@ class MigrationBase(BaseModel): | |||||||
|         after the migration occurs. |         after the migration occurs. | ||||||
|         """ |         """ | ||||||
|         image.write_image(dest_slug, src, extension=src.suffix) |         image.write_image(dest_slug, src, extension=src.suffix) | ||||||
|         minify.migrate_images()  # TODO: Refactor to support single file minification that doesn't suck |  | ||||||
|  |  | ||||||
|     def rewrite_alias(self, recipe_dict: dict) -> dict: |     def rewrite_alias(self, recipe_dict: dict) -> dict: | ||||||
|         """A helper function to reassign attributes by an alias using a list |         """A helper function to reassign attributes by an alias using a list | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user