Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Use upstream thumbnail if available (#898)

Krystle Salazarc45844af97d2
diff --git a/api/catalog/api/views/image_views.py b/api/catalog/api/views/image_views.py
index 05389b532..eeeacabbd 100644
--- a/api/catalog/api/views/image_views.py
+++ b/api/catalog/api/views/image_views.py
@@ -1,5 +1,4 @@
import io
-import re
import struct
from django.conf import settings
@@ -107,17 +106,7 @@ class ImageViewSet(MediaViewSet):
)
def thumbnail(self, request, *_, **__):
image = self.get_object()
-
- image_url = image.url
- if not image_url:
- raise NotFound("Could not find image.", 404)
-
- # Hotfix to use scaled down version of the image from SMK
- # TODO Remove when this issue is addressed:
- # TODO https://github.com/WordPress/openverse-catalog/issues/698
- if "iip.smk.dk" in image_url:
- width = settings.THUMBNAIL_WIDTH_PX
- image_url = re.sub(r"!\d+,", f"!{width},", image_url)
+ image_url = image.thumbnail or image.url
return super().thumbnail(image_url, request)
diff --git a/api/test/unit/views/image_views_test.py b/api/test/unit/views/image_views_test.py
index c463532d6..b6795475a 100644
--- a/api/test/unit/views/image_views_test.py
+++ b/api/test/unit/views/image_views_test.py
@@ -3,6 +3,9 @@ from collections.abc import Callable
from dataclasses import dataclass
from pathlib import Path
from test.factory.models.image import ImageFactory
+from unittest.mock import ANY, patch
+
+from django.http import HttpResponse
import pytest
from requests import Request, Response
@@ -56,3 +59,21 @@ def test_oembed_sends_ua_header(api_client, requests):
assert len(requests.requests) > 0
for r in requests.requests:
assert r.headers == ImageViewSet.OEMBED_HEADERS
+
+
+ "has_thumbnail, expected_thumb_url",
+ [(True, "http://example.com/thumb.jpg"), (False, "http://example.com/image.jpg")],
+)
+def test_thumbnail_uses_upstream_thumb(api_client, has_thumbnail, expected_thumb_url):
+ thumb_url = "http://example.com/thumb.jpg" if has_thumbnail else None
+ image = ImageFactory.create(
+ url="http://example.com/image.jpg",
+ thumbnail=thumb_url,
+ )
+ with patch("catalog.api.views.media_views.MediaViewSet.thumbnail") as thumb_call:
+ mock_response = HttpResponse("mock_response")
+ thumb_call.return_value = mock_response
+ api_client.get(f"/v1/images/{image.identifier}/thumb/")
+ thumb_call.assert_called_once_with(expected_thumb_url, ANY)