diff --git a/litecord/blueprints/icons.py b/litecord/blueprints/icons.py index 1525f2b..e5f65b0 100644 --- a/litecord/blueprints/icons.py +++ b/litecord/blueprints/icons.py @@ -18,7 +18,8 @@ along with this program. If not, see . """ from os.path import splitext -from quart import Blueprint, current_app as app, send_file +from quart import Blueprint, current_app as app, send_file, redirect +from litecord.embed.sanitizer import make_md_req_url bp = Blueprint('images', __name__) @@ -54,14 +55,32 @@ async def _get_guild_icon(guild_id: int, icon_file: str): return await send_icon('guild', guild_id, icon_hash, ext=ext) -@bp.route('/embed/avatars/.png') -async def _get_default_user_avatar(discrim: int): +@bp.route('/embed/avatars/.png') +async def _get_default_user_avatar(default_id: int): + # TODO: how do we determine which assets to use for this? + # I don't think we can use discord assets. pass +async def _handle_webhook_avatar(md_url_redir: str): + md_url = make_md_req_url(None, 'img', md_url_redir) + return redirect(md_url) + + @bp.route('/avatars//') async def _get_user_avatar(user_id, avatar_file): avatar_hash, ext = splitext_(avatar_file) + + # first, check if this is a webhook avatar to redir to + md_url_redir = await app.db.fetchval(""" + SELECT md_url_redir + FROM webhook_avatars + WHERE webhook_id = $1 AND hash = $2 + """, user_id, avatar_hash) + + if md_url_redir: + return await _handle_webhook_avatar(md_url_redir) + return await send_icon('user', user_id, avatar_hash, ext=ext) diff --git a/litecord/embed/sanitizer.py b/litecord/embed/sanitizer.py index bc9f761..9ef15d5 100644 --- a/litecord/embed/sanitizer.py +++ b/litecord/embed/sanitizer.py @@ -96,7 +96,7 @@ def _md_base(config) -> tuple: return proto, md_base_url -def _make_md_req_url(config, scope: str, url): +def make_md_req_url(config, scope: str, url): """Make a mediaproxy request URL given the config, scope, and the url to be proxied.""" proto, base_url = _md_base(config) @@ -111,7 +111,7 @@ def proxify(url, *, config=None) -> str: if isinstance(url, str): url = EmbedURL(url) - return _make_md_req_url(config, 'img', url) + return make_md_req_url(config, 'img', url) async def _md_client_req(config, session, scope: str, @@ -151,7 +151,7 @@ async def _md_client_req(config, session, scope: str, if not isinstance(url, EmbedURL): url = EmbedURL(url) - request_url = _make_md_req_url(config, scope, url) + request_url = make_md_req_url(config, scope, url) async with session.get(request_url) as resp: if resp.status == 200: