feat: Disable image optimization in find_preview_file function for future configuration

This commit is contained in:
Will Miao
2025-08-25 09:03:28 +08:00
parent 623c28bfc3
commit 1823840456

View File

@@ -27,33 +27,34 @@ def find_preview_file(base_name: str, dir_path: str) -> str:
full_pattern = os.path.join(dir_path, f"{base_name}{ext}") full_pattern = os.path.join(dir_path, f"{base_name}{ext}")
if os.path.exists(full_pattern): if os.path.exists(full_pattern):
# Check if this is an image and not already webp # Check if this is an image and not already webp
if ext.lower().endswith(('.jpg', '.jpeg', '.png')) and not ext.lower().endswith('.webp'): # TODO: disable the optimization for now, maybe add a config option later
try: # if ext.lower().endswith(('.jpg', '.jpeg', '.png')) and not ext.lower().endswith('.webp'):
# Optimize the image to webp format # try:
webp_path = os.path.join(dir_path, f"{base_name}.webp") # # Optimize the image to webp format
# webp_path = os.path.join(dir_path, f"{base_name}.webp")
# Use ExifUtils to optimize the image # # Use ExifUtils to optimize the image
with open(full_pattern, 'rb') as f: # with open(full_pattern, 'rb') as f:
image_data = f.read() # image_data = f.read()
optimized_data, _ = ExifUtils.optimize_image( # optimized_data, _ = ExifUtils.optimize_image(
image_data=image_data, # image_data=image_data,
target_width=CARD_PREVIEW_WIDTH, # target_width=CARD_PREVIEW_WIDTH,
format='webp', # format='webp',
quality=85, # quality=85,
preserve_metadata=False # preserve_metadata=False
) # )
# Save the optimized webp file # # Save the optimized webp file
with open(webp_path, 'wb') as f: # with open(webp_path, 'wb') as f:
f.write(optimized_data) # f.write(optimized_data)
logger.debug(f"Optimized preview image from {full_pattern} to {webp_path}") # logger.debug(f"Optimized preview image from {full_pattern} to {webp_path}")
return webp_path.replace(os.sep, "/") # return webp_path.replace(os.sep, "/")
except Exception as e: # except Exception as e:
logger.error(f"Error optimizing preview image {full_pattern}: {e}") # logger.error(f"Error optimizing preview image {full_pattern}: {e}")
# Fall back to original file if optimization fails # # Fall back to original file if optimization fails
return full_pattern.replace(os.sep, "/") # return full_pattern.replace(os.sep, "/")
# Return the original path for webp images or non-image files # Return the original path for webp images or non-image files
return full_pattern.replace(os.sep, "/") return full_pattern.replace(os.sep, "/")