From addabb53efeecb7cafd563e729b932a6c7a4a04e Mon Sep 17 00:00:00 2001 From: Luna Date: Wed, 1 May 2019 18:51:57 -0300 Subject: [PATCH] add draft for webhook_avatars usage --- litecord/blueprints/webhooks.py | 30 ++++++++++++++++++++++-------- litecord/embed/schemas.py | 5 +++++ 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/litecord/blueprints/webhooks.py b/litecord/blueprints/webhooks.py index a812a2a..3669972 100644 --- a/litecord/blueprints/webhooks.py +++ b/litecord/blueprints/webhooks.py @@ -19,6 +19,7 @@ along with this program. If not, see . import secrets import base64 +import hashlib from typing import Dict, Any, Optional from quart import Blueprint, jsonify, current_app as app, request @@ -42,8 +43,9 @@ from litecord.blueprints.channel.messages import ( msg_create_request, msg_create_check_content, msg_add_attachment, msg_guild_text_mentions ) -from litecord.embed.sanitizer import fill_embed, fetch_raw_img +from litecord.embed.sanitizer import fill_embed, fetch_raw_img, proxify from litecord.embed.messages import process_url_embed, is_media_url +from litecord.embed.schemas import EmbedURL from litecord.utils import pg_set_json from litecord.enums import MessageType from litecord.images import STATIC_IMAGE_MIMES @@ -347,6 +349,18 @@ async def create_message_webhook(guild_id, channel_id, webhook_id, data): return message_id +async def _webhook_avy_redir(webhook_id: int, avatar_url): + eu_avatar_url = EmbedURL.from_parsed(avatar_url) + url_hash = hashlib.sha256(eu_avatar_url.to_md_path).hexdigest() + + await app.db.execute(""" + INSERT INTO webhook_avatars (webhook_id, hash, md_url_redir) + VALUES ($1, $2, $3) + """, webhook_id, url_hash, proxify(eu_avatar_url)) + + return url_hash + + async def _create_avatar(webhook_id: int, avatar_url) -> str: """Create an avatar for a webhook out of an avatar URL, given when executing the webhook. @@ -364,7 +378,7 @@ async def _create_avatar(webhook_id: int, avatar_url) -> str: # but in the end, we will store it under the webhook_avatars table, # not IconManager. resp, raw = await fetch_raw_img(avatar_url) - raw_b64 = base64.b64encode(raw).decode() + #raw_b64 = base64.b64encode(raw).decode() mime = resp.headers['content-type'] @@ -372,15 +386,15 @@ async def _create_avatar(webhook_id: int, avatar_url) -> str: if mime not in STATIC_IMAGE_MIMES: raise BadRequest('invalid mime type for given url') - b64_data = f'data:{mime};base64,{raw_b64}' + #b64_data = f'data:{mime};base64,{raw_b64}' # TODO: replace this by webhook_avatars - icon = await app.icons.put( - 'user', webhook_id, b64_data, - always_icon=True, size=(128, 128) - ) + #icon = await app.icons.put( + # 'user', webhook_id, b64_data, + # always_icon=True, size=(128, 128) + #) - return icon.icon_hash + return await _webhook_avy_redir(webhook_id, avatar_url) @bp.route('/webhooks//', methods=['POST']) diff --git a/litecord/embed/schemas.py b/litecord/embed/schemas.py index ef60702..6f3b306 100644 --- a/litecord/embed/schemas.py +++ b/litecord/embed/schemas.py @@ -35,6 +35,11 @@ class EmbedURL: self.raw_url = url self.parsed = parsed + @classmethod + def from_parsed(cls, parsed): + """Make an EmbedURL instance out of an already parsed 6-tuple.""" + return cls(parsed.geturl()) + @property def url(self) -> str: """Return the unparsed URL."""