mirror of
https://github.com/zulip/zulip.git
synced 2025-11-09 08:26:11 +00:00
This endpoint serves requests which might originate from an image preview link which had an http url and the message holding the image link was rendered before we introduced thumbnailing. In that case we would have used a camo proxy to proxy http content over https and avoid mix content warnings. In near future, we plan to drop use of camo and just rely on thumbor to serve such images. This endpoint helps maintain backward compatibility for links which were already rendered.
24 lines
965 B
Python
24 lines
965 B
Python
from django.conf import settings
|
|
from django.shortcuts import redirect
|
|
from django.utils.translation import ugettext as _
|
|
from django.http import (
|
|
HttpRequest, HttpResponse, HttpResponseForbidden, HttpResponseNotFound
|
|
)
|
|
from zerver.lib.camo import is_camo_url_valid
|
|
from zerver.lib.thumbnail import generate_thumbnail_url
|
|
|
|
import codecs
|
|
|
|
def handle_camo_url(request: HttpRequest, digest: str,
|
|
received_url: str) -> HttpResponse:
|
|
if not settings.THUMBOR_SERVES_CAMO:
|
|
return HttpResponseNotFound()
|
|
|
|
hex_encoded_url = received_url.encode('utf-8')
|
|
hex_decoded_url = codecs.decode(hex_encoded_url, 'hex')
|
|
original_url = hex_decoded_url.decode('utf-8') # type: ignore # https://github.com/python/typeshed/issues/300
|
|
if is_camo_url_valid(digest, original_url):
|
|
return redirect(generate_thumbnail_url(original_url, is_camo_url=True))
|
|
else:
|
|
return HttpResponseForbidden(_("<p>Not a valid URL.</p>"))
|