thumbnails: Extract user_uploads_or_external as a function.

This is a prepartory commit for the upcoming changes. It was meaningful
to extract this one out because this function is essentially a condition
check on whether a given url is one of the user_uploads or an external
one. Based on its value we decide whether a url must be thumbnailed or
not and thus this function will also be used in an upcoming commit
patching lib/thumbnail.py to do the same check before thumbnail url
generation.
This commit is contained in:
Aditya Bansal
2018-09-01 17:09:16 +05:30
committed by Tim Abbott
parent 9a411a5765
commit 5d68bd92ad
2 changed files with 5 additions and 3 deletions

View File

@@ -35,7 +35,7 @@ from zerver.lib.emoji import translate_emoticons, emoticon_regex
from zerver.lib.mention import possible_mentions, \ from zerver.lib.mention import possible_mentions, \
possible_user_group_mentions, extract_user_group possible_user_group_mentions, extract_user_group
from zerver.lib.url_encoding import encode_stream from zerver.lib.url_encoding import encode_stream
from zerver.lib.thumbnail import is_thumbor_enabled from zerver.lib.thumbnail import is_thumbor_enabled, user_uploads_or_external
from zerver.lib.timeout import timeout, TimeoutExpired from zerver.lib.timeout import timeout, TimeoutExpired
from zerver.lib.cache import cache_with_key, NotFoundInCache from zerver.lib.cache import cache_with_key, NotFoundInCache
from zerver.lib.url_preview import preview as link_preview from zerver.lib.url_preview import preview as link_preview
@@ -225,8 +225,7 @@ def add_a(
if data_id is not None: if data_id is not None:
a.set("data-id", data_id) a.set("data-id", data_id)
img = markdown.util.etree.SubElement(a, "img") img = markdown.util.etree.SubElement(a, "img")
user_uploads_or_external = url.startswith('http') or url.lstrip('/').startswith('user_uploads/') if is_thumbor_enabled() and use_thumbnails and user_uploads_or_external(url):
if is_thumbor_enabled() and use_thumbnails and user_uploads_or_external:
# See docs/thumbnailing.md for some high-level documentation. # See docs/thumbnailing.md for some high-level documentation.
# #
# We strip leading '/' from relative URLs here to ensure # We strip leading '/' from relative URLs here to ensure

View File

@@ -18,6 +18,9 @@ from zerver.lib.camo import get_camo_url
def is_thumbor_enabled() -> bool: def is_thumbor_enabled() -> bool:
return settings.THUMBOR_URL != '' return settings.THUMBOR_URL != ''
def user_uploads_or_external(url: str) -> bool:
return url.startswith('http') or url.lstrip('/').startswith('user_uploads/')
def get_source_type(url: str) -> str: def get_source_type(url: str) -> str:
if not (url.startswith('/user_uploads/') or url.startswith('/user_avatars/')): if not (url.startswith('/user_uploads/') or url.startswith('/user_avatars/')):
return THUMBOR_EXTERNAL_TYPE return THUMBOR_EXTERNAL_TYPE