2016-04-28 05:40:58 +02:00
|
|
|
from django.conf import settings
|
|
|
|
import codecs
|
|
|
|
import hashlib
|
|
|
|
import hmac
|
|
|
|
|
2016-12-21 13:17:53 +01:00
|
|
|
from typing import Text
|
2016-06-04 20:38:42 +02:00
|
|
|
|
2016-04-28 05:40:58 +02:00
|
|
|
# Encodes the provided URL using the same algorithm used by the camo
|
|
|
|
# caching https image proxy
|
2017-11-05 11:15:10 +01:00
|
|
|
def get_camo_url(url: Text) -> Text:
|
2016-04-28 05:40:58 +02:00
|
|
|
# Only encode the url if Camo is enabled
|
|
|
|
if settings.CAMO_URI == '':
|
|
|
|
return url
|
|
|
|
encoded_url = url.encode("utf-8")
|
|
|
|
encoded_camo_key = settings.CAMO_KEY.encode("utf-8")
|
|
|
|
digest = hmac.new(encoded_camo_key, encoded_url, hashlib.sha1).hexdigest()
|
2017-05-07 17:06:01 +02:00
|
|
|
hex_encoded_url = codecs.encode(encoded_url, "hex") # type: ignore # https://github.com/python/typeshed/issues/300
|
2016-04-28 05:40:58 +02:00
|
|
|
return "%s%s/%s" % (settings.CAMO_URI, digest, hex_encoded_url.decode("utf-8"))
|