fix(phase1): add logging, remove unused import, add error handling
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
"""Image storage utilities for managing image files and directory structure."""
|
||||
import logging
|
||||
import re
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
from typing import List, Optional
|
||||
from urllib.parse import quote
|
||||
|
||||
# Root directory for all images
|
||||
IMAGES_ROOT = Path("images")
|
||||
@@ -84,10 +84,8 @@ def get_unique_filename(
|
||||
# Build the base filename without UUID
|
||||
base_filename = f"{sanitized_name}_{variant}.jpg"
|
||||
|
||||
# Check for collision (case-insensitive)
|
||||
existing_lower = [f.lower() for f in existing_files]
|
||||
|
||||
if base_filename.lower() not in existing_lower:
|
||||
# Check for collision (existing_files should already be lowercased)
|
||||
if base_filename.lower() not in existing_files:
|
||||
# No collision
|
||||
return base_filename
|
||||
|
||||
@@ -116,7 +114,7 @@ def ensure_image_directories() -> None:
|
||||
except Exception:
|
||||
# If get_categories fails (e.g., DB not ready), just create root
|
||||
# Categories will be created on-demand in save_image
|
||||
pass
|
||||
logging.exception("Failed to ensure image directories")
|
||||
|
||||
|
||||
def save_image(
|
||||
@@ -158,12 +156,18 @@ def save_image(
|
||||
# Get existing files in the category
|
||||
existing_files = [f.name for f in cat_dir.iterdir() if f.is_file()]
|
||||
|
||||
# Pre-lowercase existing filenames to avoid redundant conversions in get_unique_filename
|
||||
existing_files_lower = [f.lower() for f in existing_files]
|
||||
|
||||
# Get unique filename
|
||||
unique_filename = get_unique_filename(filename_base, sanitized_category, existing_files, variant)
|
||||
unique_filename = get_unique_filename(filename_base, sanitized_category, existing_files_lower, variant)
|
||||
|
||||
# Write file
|
||||
file_path = cat_dir / unique_filename
|
||||
file_path.write_bytes(file_bytes)
|
||||
try:
|
||||
file_path.write_bytes(file_bytes)
|
||||
except OSError as e:
|
||||
raise IOError(f"Failed to write image to {file_path}: {str(e)}")
|
||||
|
||||
# Return relative path with forward slashes
|
||||
relative_path = f"/images/{sanitized_category}/{unique_filename}"
|
||||
|
||||
Reference in New Issue
Block a user