Реализовать асинхронную обработку вложений
This commit is contained in:
@@ -14,7 +14,7 @@ router = APIRouter(tags=['attachments'])
|
||||
|
||||
@router.post(
|
||||
'/api/v1/chats/{chat_id}/attachments',
|
||||
status_code=201,
|
||||
status_code=202,
|
||||
response_model=AttachmentOut,
|
||||
)
|
||||
async def upload(
|
||||
@@ -51,16 +51,14 @@ async def content(
|
||||
actor: User = Depends(current_user),
|
||||
attachments: AttachmentService = Depends(attachment_service),
|
||||
) -> StreamingResponse:
|
||||
if variant != 'original':
|
||||
from ...domain.errors import NotFound
|
||||
|
||||
raise NotFound('вариант вложения не найден')
|
||||
attachment, stream = await attachments.content(actor, attachment_id)
|
||||
attachment, stream, content_type, size_bytes = await attachments.content(
|
||||
actor, attachment_id, variant
|
||||
)
|
||||
return StreamingResponse(
|
||||
stream.chunks(),
|
||||
media_type=attachment.content_type,
|
||||
media_type=content_type,
|
||||
headers={
|
||||
'Content-Length': str(attachment.size_bytes),
|
||||
'Content-Length': str(size_bytes),
|
||||
'Content-Disposition': f'inline; filename="attachment-{attachment.id}"',
|
||||
},
|
||||
)
|
||||
|
||||
+21
-2
@@ -134,6 +134,15 @@ class MessageOut(Strict):
|
||||
)
|
||||
|
||||
|
||||
class VariantOut(Strict):
|
||||
name: str
|
||||
content_type: str
|
||||
size_bytes: int
|
||||
width: int
|
||||
height: int
|
||||
sha256: str
|
||||
|
||||
|
||||
class AttachmentOut(Strict):
|
||||
id: UUID
|
||||
chat_id: UUID
|
||||
@@ -143,7 +152,7 @@ class AttachmentOut(Strict):
|
||||
size_bytes: int
|
||||
sha256: str
|
||||
status: str
|
||||
variants: list = Field(default_factory=list)
|
||||
variants: list[VariantOut] = Field(default_factory=list)
|
||||
error_code: str | None
|
||||
created_at: str
|
||||
updated_at: str
|
||||
@@ -159,7 +168,17 @@ class AttachmentOut(Strict):
|
||||
size_bytes=attachment.size_bytes,
|
||||
sha256=attachment.sha256,
|
||||
status=attachment.status,
|
||||
variants=[],
|
||||
variants=[
|
||||
VariantOut(
|
||||
name=item.name,
|
||||
content_type=item.content_type,
|
||||
size_bytes=item.size_bytes,
|
||||
width=item.width,
|
||||
height=item.height,
|
||||
sha256=item.sha256,
|
||||
)
|
||||
for item in attachment.variants
|
||||
],
|
||||
error_code=attachment.error_code,
|
||||
created_at=to_rfc3339(attachment.created_at),
|
||||
updated_at=to_rfc3339(attachment.updated_at),
|
||||
|
||||
Reference in New Issue
Block a user