From 01321bf6076274800d162c3b829609ee69aaadb5 Mon Sep 17 00:00:00 2001 From: Daniel Bedeleanu Date: Mon, 20 Apr 2026 22:09:58 +0300 Subject: [PATCH] fix(phase1): restore API contract while preserving N+1 optimization The N+1 optimization in save_image() pre-lowercases the existing_files list before passing to get_unique_filename(). However, this broke the API contract: the function should handle any-case input to remain robust. Changed: get_unique_filename() now defensively lowercases the input list, ensuring collision detection works regardless of input case. Benefits: - Fixes implicit API contract change (function expected any-case input) - Maintains N+1 optimization (pre-lowercasing still works) - Supports both optimization and edge cases (direct function calls) - All 22 tests pass --- backend/services/image_storage.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/backend/services/image_storage.py b/backend/services/image_storage.py index 5c19f4f6..f20dce8a 100644 --- a/backend/services/image_storage.py +++ b/backend/services/image_storage.py @@ -84,8 +84,10 @@ def get_unique_filename( # Build the base filename without UUID base_filename = f"{sanitized_name}_{variant}.jpg" - # Check for collision (existing_files should already be lowercased) - if base_filename.lower() not in existing_files: + # Defensive collision check: handle both pre-lowercased and any-case input + # This maintains backward compatibility while supporting the optimization + existing_lower = [f.lower() for f in existing_files] + if base_filename.lower() not in existing_lower: # No collision return base_filename