diff options
| author | Vosjedev <vosje@vosjedev.net> | 2026-01-24 13:39:50 +0100 |
|---|---|---|
| committer | Vosjedev <vosje@vosjedev.net> | 2026-01-24 13:39:50 +0100 |
| commit | df5c5e937c91785c26b72cc3b4670f37d59316cb (patch) | |
| tree | 7d5165a9a819f4a4eafe09d302fd7838a9ffa87f /src/discord_image_bridge | |
| parent | 8f5dbde6ec162722424b6f75cd483c22fb02c3ef (diff) | |
| download | discord_image_bridge-df5c5e937c91785c26b72cc3b4670f37d59316cb.tar.gz discord_image_bridge-df5c5e937c91785c26b72cc3b4670f37d59316cb.tar.bz2 discord_image_bridge-df5c5e937c91785c26b72cc3b4670f37d59316cb.tar.xz | |
utils.py: allow downloading uncached files, properly form filename, switch back to sha256
Signed-off-by: Vosjedev <vosje@vosjedev.net>
Diffstat (limited to 'src/discord_image_bridge')
| -rw-r--r-- | src/discord_image_bridge/utils.py | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/src/discord_image_bridge/utils.py b/src/discord_image_bridge/utils.py index 923e17f..e614e50 100644 --- a/src/discord_image_bridge/utils.py +++ b/src/discord_image_bridge/utils.py @@ -1,7 +1,7 @@ import os import cherrypy import requests -from hashlib import md5 as do_hash +from hashlib import sha256 as do_hash from .dbpool import DBPoolManager from . import discord @@ -13,7 +13,7 @@ def download_and_cache(url, filename): hash=do_hash(resp.content).hexdigest() try: os.mkdir(hash[:2]) - fname=hash[:2]+hash + fname=os.path.join(hash[:2],hash) with open(fname,'wb') as fd: fd.write(resp.content) return hash, fname @@ -21,6 +21,13 @@ def download_and_cache(url, filename): cherrypy.log("Error writing "+filename+" to disk: "+repr(e)) return None, None +def download_uncached(channel, msgid, attachmentid): + status,data=discord.channel_message_get(channel_id=channel, message_id=msgid) + for attachment in data["attachments"]: + if attachment["id"]==str(attachmentid): + return download_and_cache(attachment["url"],attachment["filename"]) + return (None, None) + def on_ready(plugin:discord.DiscordWsManager, client:discord.DiscordWsClient): dbpool:DBPoolManager=plugin.dbpool @@ -32,7 +39,7 @@ def on_ready(plugin:discord.DiscordWsManager, client:discord.DiscordWsClient): "channel BIGINT UNSIGNED," "message BIGINT UNSIGNED," "id BIGINT UNSIGNED," - "hash CHAR(32)" + "hash CHAR(64)" ")" ) conn.commit() |
