Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Raise 404 when the image for thumbnail creation cannot be open (#15644)

Iga Karbowiak67cd71a9e0b7
diff --git a/saleor/thumbnail/tests/test_views.py b/saleor/thumbnail/tests/test_views.py
index dce41b0671..6790097fc8 100644
--- a/saleor/thumbnail/tests/test_views.py
+++ b/saleor/thumbnail/tests/test_views.py
@@ -279,6 +279,27 @@ def test_handle_thumbnail_view_invalid_image_mime_type(
assert Thumbnail.objects.count() == thumbnail_count
+def test_handle_thumbnail_view_image_does_not_exist(
+ client, staff_user, product_with_image, settings
+):
+ # given
+ product_media = product_with_image.media.first()
+ product_media.image.name = "invalid_image.jpg"
+ product_media.save(update_fields=["image"])
+
+ size = 500
+ product_media_id = graphene.Node.to_global_id("ProductMedia", product_media.id)
+ thumbnail_count = Thumbnail.objects.count()
+
+ # when
+ response = client.get(f"/thumbnail/{product_media_id}/{size}/")
+
+ # then
+ assert response.status_code == 404
+ assert response.content.decode("utf-8") == "Cannot found image file."
+ assert Thumbnail.objects.count() == thumbnail_count
+
+
def test_handle_icon_thumbnail_view_with_format(
client, app, icon_image, media_root, settings
):
diff --git a/saleor/thumbnail/views.py b/saleor/thumbnail/views.py
index 8da346adec..1472a6fce8 100644
--- a/saleor/thumbnail/views.py
+++ b/saleor/thumbnail/views.py
@@ -108,6 +108,9 @@ def handle_thumbnail(
processed_image = ProcessedImage(image.name, size_px, format)
try:
thumbnail_file, _ = processed_image.create_thumbnail()
+ except FileNotFoundError as error:
+ logger.info(str(error))
+ return HttpResponseNotFound("Cannot found image file.")
except ValueError as error:
logger.info(str(error))
return HttpResponseBadRequest("Invalid image.")