add basic redirect for webhook avatars

This commit is contained in:
Luna 2019-04-26 04:10:29 -03:00
parent 0ab3f2bfa4
commit 5a3740f41b
2 changed files with 25 additions and 6 deletions

View File

@ -18,7 +18,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
""" """
from os.path import splitext 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__) 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) return await send_icon('guild', guild_id, icon_hash, ext=ext)
@bp.route('/embed/avatars/<int:discrim>.png') @bp.route('/embed/avatars/<int:default_id>.png')
async def _get_default_user_avatar(discrim: int): 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 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/<int:user_id>/<avatar_file>') @bp.route('/avatars/<int:user_id>/<avatar_file>')
async def _get_user_avatar(user_id, avatar_file): async def _get_user_avatar(user_id, avatar_file):
avatar_hash, ext = splitext_(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) return await send_icon('user', user_id, avatar_hash, ext=ext)

View File

@ -96,7 +96,7 @@ def _md_base(config) -> tuple:
return proto, md_base_url 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 """Make a mediaproxy request URL given the config, scope, and the url
to be proxied.""" to be proxied."""
proto, base_url = _md_base(config) proto, base_url = _md_base(config)
@ -111,7 +111,7 @@ def proxify(url, *, config=None) -> str:
if isinstance(url, str): if isinstance(url, str):
url = EmbedURL(url) 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, 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): if not isinstance(url, EmbedURL):
url = EmbedURL(url) 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: async with session.get(request_url) as resp:
if resp.status == 200: if resp.status == 200: