From dcdeafaae1664b1cbe42c1d0afd71742c262f62d Mon Sep 17 00:00:00 2001 From: Mahdi Dibaiee Date: Sun, 20 Sep 2026 02:38:46 +0330 Subject: [PATCH] Accept HEIC image format labels --- photo-upload/app.py | 6 ++++-- photo-upload/tests/test_upload.py | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/photo-upload/app.py b/photo-upload/app.py index 716c414..ce8be74 100644 --- a/photo-upload/app.py +++ b/photo-upload/app.py @@ -128,8 +128,10 @@ def public_text(text): def web_image(raw, destination): with Image.open(io.BytesIO(raw)) as original: - if original.format not in {"JPEG", "PNG", "WEBP", "HEIF", "AVIF", "TIFF"}: - raise ValueError("Use JPEG, PNG, WebP, HEIC, AVIF, or TIFF photos") + if original.format not in {"JPEG", "PNG", "WEBP", "HEIF", "HEIC", "AVIF", "TIFF"}: + raise ValueError( + f"Detected {original.format or 'unknown'}; use JPEG, PNG, WebP, " + "HEIF/HEIC, AVIF, or TIFF photos") taken_at = exif_photo_date(original.getexif()) image = ImageOps.exif_transpose(original) image.thumbnail((GALLERY_MAX_EDGE, GALLERY_MAX_EDGE), Image.Resampling.LANCZOS) diff --git a/photo-upload/tests/test_upload.py b/photo-upload/tests/test_upload.py index 37828c2..db81df6 100644 --- a/photo-upload/tests/test_upload.py +++ b/photo-upload/tests/test_upload.py @@ -171,6 +171,28 @@ def test_tiff_and_invalid_color_profile_are_accepted(service): assert image.format == "JPEG" +def test_heic_format_label_is_accepted(service, monkeypatch): + _, client, _ = service + actual_open = Image.open + + class HeicLabel: + def __init__(self, image): + self.image = image + self.format = "HEIC" + + def __enter__(self): + return self + + def __exit__(self, *args): + self.image.close() + + def __getattr__(self, name): + return getattr(self.image, name) + + monkeypatch.setattr(Image, "open", lambda source: HeicLabel(actual_open(source))) + assert upload(client, [photo()]).status_code == 202 + + def test_photo_date_comes_from_original_metadata(service): _, client, data = service exif = Image.Exif()