2020-06-11 00:54:34 +02:00
|
|
|
import binascii
|
|
|
|
|
2018-12-17 17:27:05 +01:00
|
|
|
from django.conf import settings
|
2020-06-11 00:54:34 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse, HttpResponseForbidden, HttpResponseNotFound
|
2018-12-17 17:27:05 +01:00
|
|
|
from django.shortcuts import redirect
|
|
|
|
from django.utils.translation import ugettext as _
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2018-12-17 17:27:05 +01:00
|
|
|
from zerver.lib.camo import is_camo_url_valid
|
|
|
|
from zerver.lib.thumbnail import generate_thumbnail_url
|
|
|
|
|
|
|
|
|
|
|
|
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')
|
2019-08-10 00:30:33 +02:00
|
|
|
hex_decoded_url = binascii.a2b_hex(hex_encoded_url)
|
|
|
|
original_url = hex_decoded_url.decode('utf-8')
|
2018-12-17 17:27:05 +01:00
|
|
|
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>"))
|