feat: перенос 5 лабы логики

This commit is contained in:
Enterxx2
2026-11-10 13:48:00 +03:00
parent 470d9d6f1e
commit 60a71dc839
182 changed files with 14717 additions and 8 deletions
@@ -0,0 +1,99 @@
from typing import Annotated
from uuid import UUID
from fastapi import APIRouter, Depends, Header, Query, Request, status
from fastapi.responses import StreamingResponse
from starlette.background import BackgroundTask
from application.api.dependencies import get_current_user
from application.api.utils import content_disposition, request_chunks
from domain import (
AttachmentContentType,
AttachmentFilename,
AttachmentSchema,
AttachmentVariant,
UserSchema,
)
from infrastructure.dependencies import ServiceCreator
from infrastructure.services import AttachmentService
attachments_router = APIRouter(tags=["Attachments"])
IMAGE_BODY = {
content_type: {"schema": {"type": "string", "format": "binary"}}
for content_type in AttachmentContentType
}
@attachments_router.post(
"/chats/{chat_id}/attachments",
status_code=status.HTTP_202_ACCEPTED,
response_model=AttachmentSchema,
openapi_extra={"requestBody": {"required": True, "content": IMAGE_BODY}},
)
async def upload_attachment(
chat_id: UUID,
filename: Annotated[AttachmentFilename, Query()],
request: Request,
content_type: Annotated[str | None, Header()] = None,
content_length: Annotated[int | None, Header(ge=0)] = None,
current_user: UserSchema = Depends(get_current_user),
service: AttachmentService = Depends(ServiceCreator.get_attachment_service),
) -> AttachmentSchema:
"""
Сырые байты одного PNG/JPEG, не multipart. 202 queued — после staging и транзакции job/outbox; resize выполняет worker.
"""
return await service.upload(
current_user,
chat_id,
filename,
content_type,
content_length,
request_chunks(request),
)
@attachments_router.get(
"/attachments/{attachment_id}",
status_code=status.HTTP_200_OK,
response_model=AttachmentSchema,
)
async def get_attachment(
attachment_id: UUID,
current_user: UserSchema = Depends(get_current_user),
service: AttachmentService = Depends(ServiceCreator.get_attachment_service),
) -> AttachmentSchema:
"""Метаданные доступны только участникам чата вложения; остальным — 404"""
return await service.get_attachment(current_user, attachment_id)
@attachments_router.get(
"/attachments/{attachment_id}/content",
status_code=status.HTTP_200_OK,
response_class=StreamingResponse,
responses={200: {"content": IMAGE_BODY}},
)
async def download_attachment(
attachment_id: UUID,
variant: Annotated[AttachmentVariant, Query()] = AttachmentVariant.ORIGINAL,
current_user: UserSchema = Depends(get_current_user),
service: AttachmentService = Depends(ServiceCreator.get_attachment_service),
) -> StreamingResponse:
"""
Байты через API, без redirect на S3: доступ проверяет приложение.
Ответ идёт потоком из S3, файл целиком в памяти реплики не собирается.
"""
attachment, content = await service.open_content(
current_user, attachment_id, variant
)
return StreamingResponse(
content.iter_chunks(),
media_type=attachment.content_type,
headers={
"Content-Length": str(content.content_length),
"Content-Disposition": content_disposition(attachment.filename),
"X-Content-Type-Options": "nosniff",
"Cache-Control": "private, no-store",
},
background=BackgroundTask(content.close),
)