def generate_thumbnail_url(path: str, size: str = '0x0') -> str: if not (path.startswith('https://') or path.startswith('http://')): path = '/' + path if not is_thumbor_enabled(): if path.startswith('http://'): return get_camo_url(path) return path if not user_uploads_or_external(path): return path source_type = get_source_type(path) safe_url = base64.urlsafe_b64encode(path.encode()).decode('utf-8') image_url = '%s/source_type/%s' % (safe_url, source_type) width, height = map(int, size.split('x')) crypto = CryptoURL(key=settings.THUMBOR_KEY) encrypted_url = crypto.generate( width=width, height=height, smart=True, filters=['no_upscale()', 'sharpen(0.5,0.2,true)'], image_url=image_url) if settings.THUMBOR_URL == 'http://127.0.0.1:9995': # If THUMBOR_URL is the default then thumbor is hosted on same machine # as the Zulip server and we should serve a relative URL. # We add a /thumbor in front of the relative url because we make # use of a proxy pass to redirect request internally in Nginx to 9995 # port where thumbor is running. thumbnail_url = '/thumbor' + encrypted_url else: thumbnail_url = urllib.parse.urljoin(settings.THUMBOR_URL, encrypted_url) return thumbnail_url
def generate_thumbnail_url(path: str, size: str='0x0') -> str: if not (path.startswith('https://') or path.startswith('http://')): path = '/' + path if not is_thumbor_enabled(): if path.startswith('http://'): return get_camo_url(path) return path if not user_uploads_or_external(path): return path source_type = get_source_type(path) safe_url = base64.urlsafe_b64encode(path.encode()).decode('utf-8') image_url = '%s/source_type/%s' % (safe_url, source_type) width, height = map(int, size.split('x')) crypto = CryptoURL(key=settings.THUMBOR_KEY) encrypted_url = crypto.generate( width=width, height=height, smart=True, filters=['no_upscale()', 'sharpen(0.5,0.2,true)'], image_url=image_url ) if settings.THUMBOR_URL == 'http://127.0.0.1:9995': # If THUMBOR_URL is the default then thumbor is hosted on same machine # as the Zulip server and we should serve a relative URL. # We add a /thumbor in front of the relative url because we make # use of a proxy pass to redirect request internally in Nginx to 9995 # port where thumbor is running. thumbnail_url = '/thumbor' + encrypted_url else: thumbnail_url = urllib.parse.urljoin(settings.THUMBOR_URL, encrypted_url) return thumbnail_url
def run(self, root): # Get all URLs from the blob found_imgs = walk_tree(root, lambda e: e if e.tag == "img" else None) for img in found_imgs: url = img.get("src") if not url.startswith("http://"): # Don't rewrite images on our own site (e.g. emoji). continue img.set("src", get_camo_url(url))
def test_inline_url_embed_preview_with_camo(self) -> None: camo_url = re.sub( r"([^\w-])", r"\\\1", get_camo_url("http://ia.media-imdb.com/images/rock.jpg") ) with_preview = ( '<p><a href="http://test.org/">http://test.org/</a></p>\n<div class="message_embed"><a class="message_embed_image" href="http://test.org/" style="background-image: url(' + camo_url + ')"></a><div class="data-container"><div class="message_embed_title"><a href="http://test.org/" title="The Rock">The Rock</a></div><div class="message_embed_description">Description text</div></div></div>' ) msg = self._send_message_with_test_org_url(sender=self.example_user("hamlet")) self.assertEqual(msg.rendered_content, with_preview)
def generate_thumbnail_url(path: str, size: str = '0x0', is_camo_url: bool = False) -> str: path = urljoin("/", path) if not is_thumbor_enabled(): if is_safe_url(path, allowed_hosts=None): return path return get_camo_url(path) if is_safe_url( path, allowed_hosts=None) and not path.startswith("/user_uploads/"): return path source_type = get_source_type(path) safe_url = base64.urlsafe_b64encode(path.encode()).decode('utf-8') image_url = f'{safe_url}/source_type/{source_type}' width, height = map(int, size.split('x')) crypto = CryptoURL(key=settings.THUMBOR_KEY) smart_crop_enabled = True apply_filters = ['no_upscale()'] if is_camo_url: smart_crop_enabled = False apply_filters.append('quality(100)') if size != '0x0': apply_filters.append('sharpen(0.5,0.2,true)') encrypted_url = crypto.generate( width=width, height=height, smart=smart_crop_enabled, filters=apply_filters, image_url=image_url, ) if settings.THUMBOR_URL == 'http://127.0.0.1:9995': # If THUMBOR_URL is the default then thumbor is hosted on same machine # as the Zulip server and we should serve a relative URL. # We add a /thumbor in front of the relative url because we make # use of a proxy pass to redirect request internally in Nginx to 9995 # port where thumbor is running. thumbnail_url = '/thumbor' + encrypted_url else: thumbnail_url = urllib.parse.urljoin(settings.THUMBOR_URL, encrypted_url) return thumbnail_url
def generate_thumbnail_url(path: str, size: str = "0x0") -> str: path = urljoin("/", path) if url_has_allowed_host_and_scheme(path, allowed_hosts=None): return path return get_camo_url(path)
def emoji_img(name, url): return '<img alt="%s" class="emoji" src="%s" title="%s">' % (name, get_camo_url(url), name)