Skip to content

Commit

Permalink
Permacache attachments when logging them
Browse files Browse the repository at this point in the history
  • Loading branch information
Erisa committed Aug 3, 2024
1 parent 17b23f3 commit 8e1e29c
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions core/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

from core.models import InvalidConfigError, getLogger

import asyncio
import aiohttp
import os

logger = getLogger(__name__)


Expand Down Expand Up @@ -434,6 +438,8 @@ async def get_user_info(self) -> Optional[dict]:


class MongoDBClient(ApiClient):
background_tasks = set()

def __init__(self, bot):
mongo_uri = bot.config["connection_uri"]
if mongo_uri is None:
Expand Down Expand Up @@ -643,6 +649,11 @@ async def edit_message(self, message_id: Union[int, str], new_content: str) -> N
{"$set": {"messages.$.content": new_content, "messages.$.edited": True}},
)

async def warm_permacache(self, url):
async with asyncio.timeout(10):
async with aiohttp.ClientSession() as session:
await session.head(url)

async def append_log(
self,
message: Message,
Expand All @@ -660,6 +671,12 @@ async def append_log(
else:
avatar_url = message.author.display_avatar.url

if 'PERMACACHE_LOCATION' in os.environ:
for a in message.attachments:
task = asyncio.create_task(self.warm_permacache(a.url.replace('cdn.discordapp.com/', os.environ['PERMACACHE_LOCATION'])))
self.background_tasks.add(task)
task.add_done_callback(self.background_tasks.discard)

data = {
"timestamp": str(message.created_at),
"message_id": message_id,
Expand Down

0 comments on commit 8e1e29c

Please sign in to comment.