From 13c9c4b8264853a3a7bdf152547e9e05b9e21600 Mon Sep 17 00:00:00 2001 From: Luna Date: Wed, 5 Dec 2018 01:54:52 -0300 Subject: [PATCH] embed.sanitizer: add proxify function --- litecord/embed/sanitizer.py | 33 ++++++++++++++++++++++++--------- litecord/embed/schemas.py | 2 ++ 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/litecord/embed/sanitizer.py b/litecord/embed/sanitizer.py index 3051cd3..79e735f 100644 --- a/litecord/embed/sanitizer.py +++ b/litecord/embed/sanitizer.py @@ -5,6 +5,7 @@ litecord.embed.sanitizer """ from typing import Dict, Any from logbook import Logger +from quart import current_app as app log = Logger(__name__) Embed = Dict[str, Any] @@ -53,21 +54,35 @@ def path_exists(embed: Embed, components: str): return False +def proxify(url) -> str: + """Return a mediaproxy url for the given EmbedURL.""" + + md_base_url = app.config.MEDIA_PROXY + parsed = url.parsed + proto = 'https' if app.config.IS_SSL else 'http' + + return ( + # base mediaproxy url + f'{proto}://{md_base_url}/img/' + f'{parsed.scheme}/{parsed.netloc}/{parsed.path}' + ) + + async def fill_embed(embed: Embed) -> Embed: """Fill an embed with more information.""" embed = sanitize_embed(embed) if path_exists(embed, 'footer.icon_url'): - # TODO: make proxy_icon_url - log.warning('embed with footer.icon_url, ignoring') - - if path_exists(embed, 'image.url'): - # TODO: make proxy_icon_url, width, height - log.warning('embed with footer.image_url, ignoring') + embed['footer']['proxy_icon_url'] = \ + proxify(embed['footer']['icon_url']) if path_exists(embed, 'author.icon_url'): - # TODO: should we check icon_url and convert it into - # a proxied icon url? - log.warning('embed with author.icon_url, ignoring') + embed['author']['proxy_icon_url'] = \ + proxify(embed['author']['icon_url']) + + if path_exists(embed, 'image.url'): + # TODO: width, height + embed['image']['proxy_url'] = \ + proxify(embed['image']['url']) return embed diff --git a/litecord/embed/schemas.py b/litecord/embed/schemas.py index 39b5881..b26788e 100644 --- a/litecord/embed/schemas.py +++ b/litecord/embed/schemas.py @@ -54,6 +54,8 @@ EMBED_AUTHOR = { 'icon_url': { 'coerce': EmbedURL, 'required': False, } + + # NOTE: proxy_icon_url set by us } EMBED_FIELD = {