This commit is contained in:
2026-11-06 18:45:08 +03:00
parent 545d083049
commit 482e7ff477
17 changed files with 958 additions and 42 deletions
+11 -5
View File
@@ -20,7 +20,7 @@ router = APIRouter(prefix='/api/v1', tags=['attachments'])
decode_slot = asyncio.Semaphore(1)
@router.post('/chats/{chat_id}/attachments', status_code=201)
@router.post('/chats/{chat_id}/attachments', status_code=202)
async def upload(chat_id: str, request: Request, filename: str = Query(...),
user: dict = Depends(current_user)):
chat_id = ensure_uuid(chat_id)
@@ -67,9 +67,15 @@ def attachment_info(attachment_id: str, user: dict = Depends(current_user)):
@router.get('/attachments/{attachment_id}/content')
def download(attachment_id: str, variant: str = 'original', user: dict = Depends(current_user)):
attachment = get_attachment(attachment_id, user['id'])
if attachment.status != 'ready':
raise APIError(409, 'conflict', 'Вложение ещё не готово')
key, content_type, size = attachment.object_key, attachment.content_type, attachment.size_bytes
if variant != 'original':
raise APIError(404, 'not_found', 'Вариант не найден')
body = s3_client().get_object(Bucket=BUCKET, Key=attachment.object_key)['Body']
item = next((v for v in attachment.variants if v['name'] == variant), None)
if item is None:
raise APIError(404, 'not_found', 'Вариант не найден')
key, content_type, size = item['object_key'], item['content_type'], item['size_bytes']
body = s3_client().get_object(Bucket=BUCKET, Key=key)['Body']
def chunks():
try:
@@ -77,8 +83,8 @@ def download(attachment_id: str, variant: str = 'original', user: dict = Depends
finally:
body.close()
return StreamingResponse(chunks(), media_type=attachment.content_type,
headers={'Content-Length': str(attachment.size_bytes),
return StreamingResponse(chunks(), media_type=content_type,
headers={'Content-Length': str(size),
'Content-Disposition': "attachment; filename*=UTF-8''" + quote(attachment.filename, safe=''),
'X-Content-Type-Options': 'nosniff'},
background=BackgroundTask(body.close))