mirror of
https://github.com/zulip/zulip.git
synced 2025-11-02 21:13:36 +00:00
Extract camo encoding to a library.
This commit is contained in:
@@ -3,7 +3,6 @@ from __future__ import absolute_import
|
||||
# detailed documentation on our markdown syntax.
|
||||
from typing import Any
|
||||
|
||||
import codecs
|
||||
import markdown
|
||||
import logging
|
||||
import traceback
|
||||
@@ -20,10 +19,7 @@ import itertools
|
||||
from six.moves import urllib
|
||||
import xml.etree.cElementTree as etree
|
||||
|
||||
import hashlib
|
||||
|
||||
from collections import defaultdict
|
||||
import hmac
|
||||
|
||||
import requests
|
||||
|
||||
@@ -33,6 +29,7 @@ from django.conf import settings
|
||||
from zerver.lib.avatar import gravatar_hash
|
||||
from zerver.lib.bugdown import codehilite, fenced_code
|
||||
from zerver.lib.bugdown.fenced_code import FENCE_RE
|
||||
from zerver.lib.camo import get_camo_url
|
||||
from zerver.lib.timeout import timeout, TimeoutExpired
|
||||
from zerver.lib.cache import cache_with_key, cache_get_many, cache_set_many
|
||||
import zerver.lib.alert_words as alert_words
|
||||
@@ -246,11 +243,7 @@ class InlineHttpsProcessor(markdown.treeprocessors.Treeprocessor):
|
||||
if not url.startswith("http://"):
|
||||
# Don't rewrite images on our own site (e.g. emoji).
|
||||
continue
|
||||
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()
|
||||
hex_encoded_url = codecs.encode(encoded_url, "hex")
|
||||
img.set("src", "%s%s/%s" % (settings.CAMO_URI, digest, hex_encoded_url.decode("utf-8")))
|
||||
img.set("src", get_camo_url(url))
|
||||
|
||||
class InlineInterestingLinkProcessor(markdown.treeprocessors.Treeprocessor):
|
||||
TWITTER_MAX_IMAGE_HEIGHT = 400
|
||||
|
||||
16
zerver/lib/camo.py
Normal file
16
zerver/lib/camo.py
Normal file
@@ -0,0 +1,16 @@
|
||||
from django.conf import settings
|
||||
import codecs
|
||||
import hashlib
|
||||
import hmac
|
||||
|
||||
# Encodes the provided URL using the same algorithm used by the camo
|
||||
# caching https image proxy
|
||||
def get_camo_url(url):
|
||||
# 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()
|
||||
hex_encoded_url = codecs.encode(encoded_url, "hex")
|
||||
return "%s%s/%s" % (settings.CAMO_URI, digest, hex_encoded_url.decode("utf-8"))
|
||||
Reference in New Issue
Block a user