diff --git a/backend/app/database/images.py b/backend/app/database/images.py index 5c6057b98..b2725726b 100644 --- a/backend/app/database/images.py +++ b/backend/app/database/images.py @@ -495,7 +495,7 @@ def db_delete_images_by_ids(image_ids: List[ImageId]) -> bool: def db_toggle_image_favourite_status(image_id: str) -> bool: - conn = sqlite3.connect(DATABASE_PATH) + conn = _connect() cursor = conn.cursor() try: cursor.execute("SELECT id FROM images WHERE id = ?", (image_id,)) diff --git a/backend/app/utils/images.py b/backend/app/utils/images.py index dde078fff..3b011b0bf 100644 --- a/backend/app/utils/images.py +++ b/backend/app/utils/images.py @@ -443,19 +443,12 @@ def image_util_find_folder_id_for_image( def image_util_is_valid_image(file_path: str) -> bool: """Check if the file is a valid image with allowed extensions.""" - # Check file extension first allowed_extensions = {".jpg", ".jpeg", ".png"} - file_extension = Path(file_path).suffix.lower() - - if file_extension not in allowed_extensions: + if Path(file_path).suffix.lower() not in allowed_extensions: return False - - # Then verify it's a valid image try: - with Image.open(file_path) as img: - img.verify() - return True - except Exception: + return os.path.isfile(file_path) and os.path.getsize(file_path) > 0 + except OSError: return False diff --git a/backend/tests/test_images.py b/backend/tests/test_images.py new file mode 100644 index 000000000..63225ec1d --- /dev/null +++ b/backend/tests/test_images.py @@ -0,0 +1,32 @@ +from app.utils.images import image_util_is_valid_image + + +def test_image_util_is_valid_image(tmp_path): + # Setup valid image (e.g., non-zero size, correct extension) + valid_img = tmp_path / "valid.jpg" + valid_img.write_text("dummy content") + assert image_util_is_valid_image(str(valid_img)) is True + + # Setup valid image with uppercase extension + valid_img_upper = tmp_path / "VALID.JPG" + valid_img_upper.write_text("dummy content") + assert image_util_is_valid_image(str(valid_img_upper)) is True + + # Setup unsupported extension + invalid_ext = tmp_path / "invalid.txt" + invalid_ext.write_text("dummy content") + assert image_util_is_valid_image(str(invalid_ext)) is False + + # Setup zero-byte file + zero_byte_img = tmp_path / "empty.jpg" + zero_byte_img.touch() + assert image_util_is_valid_image(str(zero_byte_img)) is False + + # Missing file + missing_img = tmp_path / "missing.jpg" + assert image_util_is_valid_image(str(missing_img)) is False + + # Directory with an image extension + directory_img = tmp_path / "folder.jpg" + directory_img.mkdir() + assert image_util_is_valid_image(str(directory_img)) is False