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:
@@ -0,0 +1,293 @@
|
||||
import hashlib
|
||||
import time
|
||||
from collections.abc import AsyncIterator, Callable
|
||||
from contextlib import AbstractAsyncContextManager, AsyncExitStack
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
import anyio.to_thread
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from src.core import exceptions
|
||||
from src.core.repos import attachment as attachment_repo
|
||||
from src.core.repos import attachment_object as attachment_object_repo
|
||||
from src.core.services.chat import ChatService
|
||||
from src.infra.cache import Cache
|
||||
from src.infra.logger import AbstractLogger, get_logger
|
||||
from src.infra.s3 import ObjectStorage
|
||||
from src.schemas import dataclasses
|
||||
from src.schemas.enums import AttachmentStatus, ImageType
|
||||
from src.utils.constants import ATTACHMENT_MAX_BYTES, ATTACHMENT_MAX_PIXELS
|
||||
from src.utils.image import (
|
||||
DamagedImage,
|
||||
ImageInfo,
|
||||
TooManyPixels,
|
||||
UnsupportedImage,
|
||||
inspect_image,
|
||||
)
|
||||
from src.utils.utc import utcnow
|
||||
|
||||
logger: AbstractLogger = get_logger().bind(module="attachment_service")
|
||||
|
||||
# Content-Type загрузки: тип без параметров, в нижнем регистре
|
||||
_ACCEPTED_MEDIA_TYPES = frozenset(image_type.value for image_type in ImageType)
|
||||
|
||||
# короткая сессия БД на один шаг: `db_session_scope` слоя API
|
||||
type DbScope = Callable[[], AbstractAsyncContextManager[AsyncSession]]
|
||||
|
||||
|
||||
def _inspect(data: bytes) -> tuple[ImageInfo, bytes]:
|
||||
"""Структура изображения и SHA-256 — одним вызовом в потоке."""
|
||||
info = inspect_image(data, max_pixels=ATTACHMENT_MAX_PIXELS)
|
||||
return info, hashlib.sha256(data).digest()
|
||||
|
||||
|
||||
class AttachmentService:
|
||||
"""Вложения: загрузка в S3 с метаданными в PostgreSQL, чтение, отдача.
|
||||
|
||||
Сессии на весь запрос нет: каждый шаг с БД — свой короткий
|
||||
`async with self.db_scope()`, без вложенных, поэтому ни одно
|
||||
соединение пула не занято передачей тела (design change
|
||||
`attachments`, решение 5). `filename` в журнал не пишется.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
db_scope: DbScope,
|
||||
cache: Cache,
|
||||
storage: ObjectStorage,
|
||||
) -> None:
|
||||
self.db_scope = db_scope
|
||||
self.cache = cache
|
||||
self.storage = storage
|
||||
|
||||
async def _require_member(
|
||||
self,
|
||||
*,
|
||||
db_session: AsyncSession,
|
||||
chat_id: UUID,
|
||||
user_id: UUID,
|
||||
) -> None:
|
||||
await ChatService(
|
||||
db_session=db_session,
|
||||
cache=self.cache,
|
||||
).get_chat_for_member(chat_id=chat_id, user_id=user_id)
|
||||
|
||||
async def upload(
|
||||
self,
|
||||
*,
|
||||
chat_id: UUID,
|
||||
owner_id: UUID,
|
||||
filename: str,
|
||||
media_type: str | None,
|
||||
content_length: int | None,
|
||||
body: AsyncIterator[bytes],
|
||||
) -> dataclasses.AttachmentFullDTO:
|
||||
"""Загрузка оригинала; 201 только после `ready` в БД.
|
||||
|
||||
Порядок проверок — спек «Порядок проверок загрузки»: членство
|
||||
(404), `Content-Type` (415), `Content-Length` (413), затем тело со
|
||||
счётчиком (413) и содержимое (415 или 413). Порядок записи
|
||||
(решение 1): строка `pending` с ключом объекта — commit, PUT,
|
||||
`ready` — commit. Отказ S3 или БД после строки `pending` — 503,
|
||||
строка остаётся до уборщика (`upload-consistency`).
|
||||
"""
|
||||
started = time.monotonic()
|
||||
log = logger.bind(
|
||||
method="upload",
|
||||
chat_id=chat_id,
|
||||
filename_length=len(filename),
|
||||
)
|
||||
async with self.db_scope() as db_session:
|
||||
await self._require_member(
|
||||
db_session=db_session,
|
||||
chat_id=chat_id,
|
||||
user_id=owner_id,
|
||||
)
|
||||
if media_type not in _ACCEPTED_MEDIA_TYPES:
|
||||
log.info("Rejected attachment", reason="type")
|
||||
raise exceptions.UnsupportedMediaType
|
||||
if (
|
||||
content_length is not None
|
||||
and content_length > ATTACHMENT_MAX_BYTES
|
||||
):
|
||||
log.info(
|
||||
"Rejected attachment",
|
||||
reason="declared_size",
|
||||
declared_size=content_length,
|
||||
)
|
||||
raise exceptions.PayloadTooLarge
|
||||
data = await self._read_body(body=body, log=log)
|
||||
try:
|
||||
info, sha256 = await anyio.to_thread.run_sync(_inspect, data)
|
||||
except TooManyPixels as e:
|
||||
log.info("Rejected attachment", reason="pixels")
|
||||
raise exceptions.PayloadTooLarge from e
|
||||
except DamagedImage as e:
|
||||
log.info("Rejected attachment", reason="damaged")
|
||||
raise exceptions.UnsupportedMediaType from e
|
||||
except UnsupportedImage as e:
|
||||
log.info("Rejected attachment", reason="content")
|
||||
raise exceptions.UnsupportedMediaType from e
|
||||
if info.content_type != media_type:
|
||||
log.info("Rejected attachment", reason="content_mismatch")
|
||||
raise exceptions.UnsupportedMediaType
|
||||
|
||||
attachment_id = uuid4()
|
||||
now = utcnow()
|
||||
pending = dataclasses.AttachmentFullDTO(
|
||||
id=attachment_id,
|
||||
chat_id=chat_id,
|
||||
owner_id=owner_id,
|
||||
filename=filename,
|
||||
content_type=info.content_type,
|
||||
size_bytes=len(data),
|
||||
sha256=sha256,
|
||||
object_key=attachment_object_repo.original_key(
|
||||
self.storage,
|
||||
attachment_id=attachment_id,
|
||||
),
|
||||
status=AttachmentStatus.PENDING,
|
||||
created_at=now,
|
||||
updated_at=now,
|
||||
)
|
||||
async with self.db_scope() as db_session:
|
||||
await attachment_repo.insert_attachment(
|
||||
db_session=db_session,
|
||||
attachment=pending,
|
||||
)
|
||||
await db_session.commit()
|
||||
await attachment_object_repo.put_original(
|
||||
self.storage,
|
||||
key=pending.object_key,
|
||||
data=data,
|
||||
content_type=pending.content_type.value,
|
||||
)
|
||||
async with self.db_scope() as db_session:
|
||||
ready = await attachment_repo.mark_ready(
|
||||
db_session=db_session,
|
||||
attachment_id=attachment_id,
|
||||
updated_at=utcnow(),
|
||||
)
|
||||
await db_session.commit()
|
||||
if ready is None:
|
||||
# строку pending удалить некому, пока нет уборщика: ветку
|
||||
# переопределит change upload-consistency
|
||||
raise RuntimeError("Pending attachment disappeared before ready")
|
||||
log.info(
|
||||
"Uploaded attachment",
|
||||
attachment_id=attachment_id,
|
||||
size_bytes=ready.size_bytes,
|
||||
content_type=ready.content_type,
|
||||
width=info.width,
|
||||
height=info.height,
|
||||
duration_ms=round((time.monotonic() - started) * 1000),
|
||||
)
|
||||
return ready
|
||||
|
||||
async def _read_body(
|
||||
self,
|
||||
*,
|
||||
body: AsyncIterator[bytes],
|
||||
log: AbstractLogger,
|
||||
) -> bytes:
|
||||
"""Тело целиком; больше лимита — 413 сразу, без дочитывания."""
|
||||
buffer = bytearray()
|
||||
try:
|
||||
async for chunk in body:
|
||||
if len(buffer) + len(chunk) > ATTACHMENT_MAX_BYTES:
|
||||
log.info(
|
||||
"Rejected attachment",
|
||||
reason="size",
|
||||
size=len(buffer) + len(chunk),
|
||||
)
|
||||
raise exceptions.PayloadTooLarge
|
||||
buffer += chunk
|
||||
except exceptions.UploadInterrupted:
|
||||
log.info("Rejected attachment", reason="interrupted")
|
||||
raise
|
||||
return bytes(buffer)
|
||||
|
||||
async def get_attachment(
|
||||
self,
|
||||
*,
|
||||
attachment_id: UUID,
|
||||
user_id: UUID,
|
||||
) -> dataclasses.AttachmentFullDTO:
|
||||
"""Готовое вложение для участника его чата; иначе 404.
|
||||
|
||||
Нет строки, она `pending` или пользователь не участник —
|
||||
одинаковый `AttachmentNotFound`.
|
||||
"""
|
||||
async with self.db_scope() as db_session:
|
||||
attachment = await attachment_repo.get_attachment(
|
||||
db_session=db_session,
|
||||
attachment_id=attachment_id,
|
||||
)
|
||||
if (
|
||||
attachment is None
|
||||
or attachment.status != AttachmentStatus.READY
|
||||
):
|
||||
raise exceptions.AttachmentNotFound
|
||||
try:
|
||||
await self._require_member(
|
||||
db_session=db_session,
|
||||
chat_id=attachment.chat_id,
|
||||
user_id=user_id,
|
||||
)
|
||||
except exceptions.ChatNotFound as e:
|
||||
raise exceptions.AttachmentNotFound from e
|
||||
return attachment
|
||||
|
||||
async def open_content(
|
||||
self,
|
||||
*,
|
||||
attachment_id: UUID,
|
||||
user_id: UUID,
|
||||
) -> dataclasses.AttachmentContentDTO:
|
||||
"""Оригинал потоком из S3 (решение 7).
|
||||
|
||||
Отказ S3 — 503 до заголовков ответа. Нет объекта или размер в S3
|
||||
другой — `error` в журнале и 500; строку сервис не меняет.
|
||||
"""
|
||||
attachment = await self.get_attachment(
|
||||
attachment_id=attachment_id,
|
||||
user_id=user_id,
|
||||
)
|
||||
log = logger.bind(
|
||||
method="open_content",
|
||||
attachment_id=attachment_id,
|
||||
object_key=attachment.object_key,
|
||||
)
|
||||
stack = AsyncExitStack()
|
||||
try:
|
||||
stored = await stack.enter_async_context(
|
||||
attachment_object_repo.open_original(
|
||||
self.storage,
|
||||
key=attachment.object_key,
|
||||
),
|
||||
)
|
||||
except exceptions.StoredObjectMissing as e:
|
||||
log.error("Attachment content is missing")
|
||||
raise exceptions.AttachmentContentMissing from e
|
||||
if stored.size != attachment.size_bytes:
|
||||
await stack.aclose()
|
||||
log.error(
|
||||
"Attachment content size differs",
|
||||
size_bytes=attachment.size_bytes,
|
||||
stored_size=stored.size,
|
||||
)
|
||||
raise exceptions.AttachmentContentMissing
|
||||
|
||||
async def chunks() -> AsyncIterator[bytes]:
|
||||
try:
|
||||
async for chunk in stored.iter_chunks():
|
||||
yield chunk
|
||||
finally:
|
||||
await stack.aclose()
|
||||
|
||||
return dataclasses.AttachmentContentDTO(
|
||||
content_type=attachment.content_type,
|
||||
size_bytes=attachment.size_bytes,
|
||||
chunks=chunks(),
|
||||
close=stack.aclose,
|
||||
)
|
||||
Reference in New Issue
Block a user