mirror of https://gitlab.com/litecord/litecord.git
embed.sanitizer: add proxify function
This commit is contained in:
parent
9ab3fa6b70
commit
13c9c4b826
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -54,6 +54,8 @@ EMBED_AUTHOR = {
|
|||
'icon_url': {
|
||||
'coerce': EmbedURL, 'required': False,
|
||||
}
|
||||
|
||||
# NOTE: proxy_icon_url set by us
|
||||
}
|
||||
|
||||
EMBED_FIELD = {
|
||||
|
|
|
|||
Loading…
Reference in New Issue