diff --git a/litecord/blueprints/attachments.py b/litecord/blueprints/attachments.py index 07a5f7e..220b9b1 100644 --- a/litecord/blueprints/attachments.py +++ b/litecord/blueprints/attachments.py @@ -27,4 +27,4 @@ bp = Blueprint('attachments', __name__) async def _get_attachment(channel_id: int, message_id: int, filename: str, ext: str): # TODO: get the attachment id with given metadata - return + return '', 204 diff --git a/litecord/blueprints/channel/messages.py b/litecord/blueprints/channel/messages.py index acdec46..ccdc90a 100644 --- a/litecord/blueprints/channel/messages.py +++ b/litecord/blueprints/channel/messages.py @@ -377,7 +377,9 @@ async def _add_attachment(message_id: int, channel_id: int, # open with pillow, extract image size image = Image.open(attachment_file.stream) img_width, img_height = image.size - image.close() + + # NOTE: DO NOT close the image, as closing the image will + # also close the stream. # reset it to 0 for later usage attachment_file.stream.seek(0) @@ -397,7 +399,7 @@ async def _add_attachment(message_id: int, channel_id: int, ext = filename.split('.')[-1] - with open(f'attachments/{attachment_id}.{ext}') as attach_file: + with open(f'attachments/{attachment_id}.{ext}', 'wb') as attach_file: attach_file.write(attachment_file.stream.read()) log.debug('written {} bytes for attachment id {}', diff --git a/litecord/storage.py b/litecord/storage.py index a6ecbee..fe4f317 100644 --- a/litecord/storage.py +++ b/litecord/storage.py @@ -645,11 +645,13 @@ class Storage: WHERE message_id = $1 """, message_id) + attachment_ids = [r['id'] for r in attachment_ids] + res = [] for attachment_id in attachment_ids: row = await self.db.fetchrow(""" - SELECT id::text, message_id, channel_id, mime + SELECT id::text, message_id, channel_id, filename, filesize, image, height, width FROM attachments WHERE id = $1 @@ -659,7 +661,6 @@ class Storage: drow.pop('message_id') drow.pop('channel_id') - drow.pop('mime') drow['size'] = drow['filesize'] drow.pop('size')