Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
Use client-side accept header to automatically use WEBP output
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvkb committed Apr 8, 2022
1 parent c951fe7 commit 2bbe49a
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions api/catalog/api/views/media_views.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import json
import logging as log
from typing import List
from urllib.error import HTTPError
from urllib.parse import urlencode
from urllib.request import urlopen
from urllib.request import Request, urlopen

from catalog.api.controllers import search_controller
from catalog.api.models import ContentProvider
Expand Down Expand Up @@ -131,7 +132,11 @@ def thumbnail(self, image_url, request, *_, **__):
serializer = self.get_serializer(data=request.query_params)
if not serializer.is_valid():
raise get_api_exception("Invalid input.", 400)
return self._get_proxied_image(image_url, **serializer.validated_data)
return self._get_proxied_image(
image_url,
accept_header=request.headers.get("Accept", "image/*"),
**serializer.validated_data,
)

# Helper functions

Expand All @@ -152,14 +157,24 @@ def _get_user_ip(request):
return ip

@staticmethod
def _thumbnail_proxy_comm(path: str, params: dict):
def _thumbnail_proxy_comm(
path: str,
params: dict,
headers: List[
tuple[str, str]
] = None, # ``List`` because there is a ``list`` function
):
proxy_url = settings.THUMBNAIL_PROXY_URL
query_string = urlencode(params)
upstream_url = f"{proxy_url}/{path}?{query_string}"
log.debug(f"Image proxy upstream URL: {upstream_url}")

try:
upstream_response = urlopen(upstream_url)
req = Request(upstream_url)
if headers:
for key, val in headers:
req.add_header(key, val)
upstream_response = urlopen(req)

res_status = upstream_response.status
content_type = upstream_response.headers.get("Content-Type")
Expand All @@ -175,6 +190,7 @@ def _thumbnail_proxy_comm(path: str, params: dict):
@staticmethod
def _get_proxied_image(
image_url: str,
accept_header: str = "image/*",
is_full_size: bool = False,
is_compressed: bool = True,
):
Expand All @@ -190,11 +206,12 @@ def _get_proxied_image(
params |= {
"quality": settings.THUMBNAIL_JPG_QUALITY,
"compression": settings.THUMBNAIL_PNG_COMPRESSION,
"type": "auto", # uses ``Accept`` header to determine output type
}
else:
params |= {"quality": 100, "compression": 0}
img_res, res_status, content_type = MediaViewSet._thumbnail_proxy_comm(
path, params
path, params, [("Accept", accept_header)]
)
response = HttpResponse(
img_res.read(), status=res_status, content_type=content_type
Expand Down

0 comments on commit 2bbe49a

Please sign in to comment.