attachments: add attachment fetch impl

- channel.messages: fix width/height switching
This commit is contained in:
Luna 2018-12-08 23:59:45 -03:00
parent 7e17b6965e
commit a8704f8176
2 changed files with 18 additions and 6 deletions

View File

@ -17,14 +17,26 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
""" """
from quart import Blueprint from quart import Blueprint, send_file, current_app as app
bp = Blueprint('attachments', __name__) bp = Blueprint('attachments', __name__)
@bp.route('/attachments' @bp.route('/attachments'
'/<int:channel_id>/<int:message_id>/<filename>.<ext>', '/<int:channel_id>/<int:message_id>/<filename>',
methods=['GET']) methods=['GET'])
async def _get_attachment(channel_id: int, message_id: int, async def _get_attachment(channel_id: int, message_id: int,
filename: str, ext: str): filename: str):
# TODO: get the attachment id with given metadata attach_id = await app.db.fetchval("""
return '', 204 SELECT id
FROM attachments
WHERE channel_id = $1
AND message_id = $2
AND filename = $3
""", channel_id, message_id, filename)
if attach_id is None:
return '', 404
ext = filename.split('.')[-1]
return await send_file(f'./attachments/{attach_id}.{ext}')

View File

@ -389,7 +389,7 @@ async def _add_attachment(message_id: int, channel_id: int,
INSERT INTO attachments INSERT INTO attachments
(id, channel_id, message_id, (id, channel_id, message_id,
filename, filesize, filename, filesize,
image, height, width) image, width, height)
VALUES VALUES
($1, $2, $3, $4, $5, $6, $7, $8) ($1, $2, $3, $4, $5, $6, $7, $8)
""", """,