From 443fcd4bbaaec5a8c2ac87d0e22c17497c27e222 Mon Sep 17 00:00:00 2001 From: Jason <81298350+Deutscher775@users.noreply.github.com> Date: Fri, 11 Oct 2024 23:32:57 +0200 Subject: [PATCH] fix nerimity file upload --- src/astroidapi/sending_handler.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/astroidapi/sending_handler.py b/src/astroidapi/sending_handler.py index e513eb5..992539c 100644 --- a/src/astroidapi/sending_handler.py +++ b/src/astroidapi/sending_handler.py @@ -209,11 +209,25 @@ async def send_to_nerimity(cls, updated_json, endpoint, attachments: list = None "Authorization": f"{config.NERIMITY_TOKEN}", } print(channel_id) - formdata = aiohttp.FormData() - formdata.add_field("content", f"**{message_author_name}**: {message_content}") + payload = { + "content": f"**{message_author_name}**: {message_content}", + } + nerimityCdnFileId = None if attachments is not None: - formdata.add_field("attachment", open(os.path.abspath(attachments[0].name), "rb"), filename=attachments[0].name.split("/")[-1], content_type=f"image/{attachments[0].name.split('.')[-1]}") - r = await session.post(f"https://nerimity.com/api/channels/{int(channel_id)}/messages", headers=headers, data=formdata) + formdata = aiohttp.FormData() + formdata.add_field("f", open(os.path.abspath(attachments[0].name), "rb"), filename=attachments[0].name.split("/")[-1], content_type=f"image/{attachments[0].name.split('.')[-1]}") + async with session.post("https://cdn.nerimity.com/upload", headers=headers, data=formdata) as r: + if r.ok: + nerimityCdnFileId = (await r.json())["fileId"] + async with session.post(f"https://cdn.nerimity.com/attachments/{int(channel_id)}/{nerimityCdnFileId}", headers=headers) as r: + if r.ok: + nerimityCdnFileId = (await r.json())["fileId"] + payload["nerimityCdnFileId"] = nerimityCdnFileId + else: + raise errors.SendingError.SendFromNerimiryError(f"Failed to upload attachment to nerimity. Response: {r.status}, {r.reason}") + else: + raise errors.SendingError.SendFromNerimiryError(f"Failed to upload attachment to nerimity. Response: {r.status}, {r.reason}") + r = await session.post(f"https://nerimity.com/api/channels/{int(channel_id)}/messages", headers=headers, data=payload) print(f"Sent to nerimity. Response: {r.status}, {r.reason} {await r.text()}") await session.close() if attachments is not None: