embed.sanitizer: add proxify function

This commit is contained in:
Luna 2018-12-05 01:54:52 -03:00
parent 9ab3fa6b70
commit 13c9c4b826
2 changed files with 26 additions and 9 deletions

View File

@ -5,6 +5,7 @@ litecord.embed.sanitizer
""" """
from typing import Dict, Any from typing import Dict, Any
from logbook import Logger from logbook import Logger
from quart import current_app as app
log = Logger(__name__) log = Logger(__name__)
Embed = Dict[str, Any] Embed = Dict[str, Any]
@ -53,21 +54,35 @@ def path_exists(embed: Embed, components: str):
return False 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: async def fill_embed(embed: Embed) -> Embed:
"""Fill an embed with more information.""" """Fill an embed with more information."""
embed = sanitize_embed(embed) embed = sanitize_embed(embed)
if path_exists(embed, 'footer.icon_url'): if path_exists(embed, 'footer.icon_url'):
# TODO: make proxy_icon_url embed['footer']['proxy_icon_url'] = \
log.warning('embed with footer.icon_url, ignoring') proxify(embed['footer']['icon_url'])
if path_exists(embed, 'image.url'):
# TODO: make proxy_icon_url, width, height
log.warning('embed with footer.image_url, ignoring')
if path_exists(embed, 'author.icon_url'): if path_exists(embed, 'author.icon_url'):
# TODO: should we check icon_url and convert it into embed['author']['proxy_icon_url'] = \
# a proxied icon url? proxify(embed['author']['icon_url'])
log.warning('embed with author.icon_url, ignoring')
if path_exists(embed, 'image.url'):
# TODO: width, height
embed['image']['proxy_url'] = \
proxify(embed['image']['url'])
return embed return embed

View File

@ -54,6 +54,8 @@ EMBED_AUTHOR = {
'icon_url': { 'icon_url': {
'coerce': EmbedURL, 'required': False, 'coerce': EmbedURL, 'required': False,
} }
# NOTE: proxy_icon_url set by us
} }
EMBED_FIELD = { EMBED_FIELD = {