feat: add image attachments to messages
- upload, metadata and download endpoints for PNG/JPEG attachments:
limits before reading and decompression, type by content,
structural integrity check without decoding pixels, safe filename
- write order: pending row with object key -> PUT to S3 -> ready;
per-step DB sessions so no pool connection is held during transfers
- attachment_ids in messages (validation, idempotency hash, history);
whitespace-only text allowed with an attachment (teacher, 2026-10-20)
- migration: attachments and message_attachments tables, text length
CHECK relaxed with NOT VALID
- student tests, fault checks and driver commands for attachments;
rollout profile starts from e1161f7 and checks the commit first
- sync attachments deltas into main specs; rules, AGENTS.md, PLAN.md
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
from .attachment import AttachmentContentDTO, AttachmentFullDTO
|
||||
from .chat import ChatFullDTO, ChatPageDTO
|
||||
from .idempotency import IdempotencyRecordDTO
|
||||
from .message import MessageFullDTO, MessagePageDTO
|
||||
@@ -5,6 +6,8 @@ from .session import CurrentSessionDTO, SessionFullDTO, SessionTokenDTO
|
||||
from .user import UserCredentialsDTO, UserFullDTO
|
||||
|
||||
__all__ = [
|
||||
"AttachmentContentDTO",
|
||||
"AttachmentFullDTO",
|
||||
"ChatFullDTO",
|
||||
"ChatPageDTO",
|
||||
"CurrentSessionDTO",
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
from collections.abc import AsyncIterator, Awaitable, Callable
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from uuid import UUID
|
||||
|
||||
from src.schemas.enums import AttachmentStatus, ImageType
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class AttachmentFullDTO:
|
||||
"""Строка вложения: метаданные и ключ объекта в S3.
|
||||
|
||||
`object_key` и статус `pending` в API не попадают; `sha256` — байты
|
||||
SHA-256 оригинала, hex собирает модель ответа.
|
||||
"""
|
||||
|
||||
id: UUID
|
||||
chat_id: UUID
|
||||
owner_id: UUID
|
||||
filename: str
|
||||
content_type: ImageType
|
||||
size_bytes: int
|
||||
sha256: bytes
|
||||
object_key: str
|
||||
status: AttachmentStatus
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class AttachmentContentDTO:
|
||||
"""Оригинал вложения на отдачу потоком.
|
||||
|
||||
`chunks` — блоки объекта из S3; `close` освобождает соединение с S3,
|
||||
вызов повторно безопасен. Генератор вызывает его сам, когда поток
|
||||
дочитан, оборван или упал; ответ — ещё раз фоновой задачей, если
|
||||
генератор так и не начался.
|
||||
"""
|
||||
|
||||
content_type: ImageType
|
||||
size_bytes: int
|
||||
chunks: AsyncIterator[bytes]
|
||||
close: Callable[[], Awaitable[None]]
|
||||
@@ -10,6 +10,8 @@ class MessageFullDTO:
|
||||
sender_id: UUID
|
||||
text: str
|
||||
created_at: datetime
|
||||
# вложения в порядке отправки; в БД — таблица message_attachments
|
||||
attachment_ids: tuple[UUID, ...] = ()
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
|
||||
Reference in New Issue
Block a user