Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Make improvements to the oEmbed endpoint (#934)
Dhruv Bhanushalie1266ff4c09b
diff --git a/api/catalog/api/views/image_views.py b/api/catalog/api/views/image_views.pyindex c3dc304d3..154f5d45a 100644--- a/api/catalog/api/views/image_views.py+++ b/api/catalog/api/views/image_views.py@@ -81,11 +81,13 @@ class ImageViewSet(MediaViewSet):context = self.get_serializer_context()url = params.validated_data["url"]+ if url.endswith("/"):+ url = url[:-1]identifier = url.rsplit("/", 1)[1]try:image = self.get_queryset().get(identifier=identifier)except Image.DoesNotExist:- return get_api_exception("Could not find image.", 404)+ raise get_api_exception("Could not find image.", 404)if not (image.height and image.width):image_file = requests.get(image.url, headers=self.OEMBED_HEADERS)width, height = PILImage.open(io.BytesIO(image_file.content)).sizediff --git a/api/test/image_integration_test.py b/api/test/image_integration_test.pyindex 1f3945782..8c8dc63e5 100644--- a/api/test/image_integration_test.py+++ b/api/test/image_integration_test.py@@ -74,6 +74,32 @@ def test_audio_report(image_fixture):report("images", image_fixture)+def test_oembed_endpoint_with_non_existent_image():+ params = {+ "url": "https://any.domain/any/path/00000000-0000-0000-0000-000000000000",+ }+ response = requests.get(+ f"{API_URL}/v1/images/oembed?{urlencode(params)}", verify=False+ )+ assert response.status_code == 404+++ "url",+ [+ f"https://any.domain/any/path/{identifier}", # no trailing slash+ f"https://any.domain/any/path/{identifier}/", # trailing slash+ identifier, # just identifier instead of URL+ ],+)+def test_oembed_endpoint_with_fuzzy_input(url):+ params = {"url": url}+ response = requests.get(+ f"{API_URL}/v1/images/oembed?{urlencode(params)}", verify=False+ )+ assert response.status_code == 200++def test_oembed_endpoint_for_json():params = {"url": f"https://any.domain/any/path/{identifier}",