Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use upstream thumbnail if available #898

Merged
merged 5 commits into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 1 addition & 12 deletions api/catalog/api/views/image_views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import io
import re
import struct

from django.conf import settings
Expand Down Expand Up @@ -107,17 +106,7 @@ def oembed(self, request, *_, **__):
)
def thumbnail(self, request, *_, **__):
image = self.get_object()

image_url = image.url
if not image_url:
raise NotFound("Could not find image.", 404)
sarayourfriend marked this conversation as resolved.
Show resolved Hide resolved

# 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)

Expand Down
21 changes: 21 additions & 0 deletions api/test/unit/views/image_views_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
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
Expand Down Expand Up @@ -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


@pytest.mark.django_db
@pytest.mark.parametrize(
"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)