"""Synchronous binary uploads with bounded staging and durable database intents.""" import asyncio import hashlib import tempfile import unicodedata import uuid import warnings from urllib.parse import quote import anyio from fastapi import APIRouter, Depends, Request from fastapi.responses import StreamingResponse from PIL import Image, UnidentifiedImageError from sqlalchemy import select, func from sqlalchemy.ext.asyncio import AsyncSession from starlette.requests import ClientDisconnect from .auth import current_user, get_session from .errors import ApiError from .http_utils import bad_request, parse_uuid from .models import Attachment, ChatMember, User from .routes import open_chat from .schemas import AttachmentOut from .storage import MAX_BYTES, StorageUnavailable, unavailable from .timeutil import format_ts router = APIRouter(prefix="/api/v1") MAX_PIXELS = 40_000_000 Image.MAX_IMAGE_PIXELS = MAX_PIXELS def attachment_out(row): return { "id": str(row.id), "chat_id": str(row.chat_id), "owner_id": str(row.owner_id), "filename": row.filename, "content_type": row.content_type, "size_bytes": row.size_bytes, "sha256": row.sha256, "status": row.status, "variants": [], "error_code": None, "created_at": format_ts(row.created_at), "updated_at": format_ts(row.updated_at), } def validate_filename(value): if value is None or not 1 <= len(value) <= 255 or not value.strip(): raise bad_request("filename: от 1 до 255 символов") if any(char in "/\\" or unicodedata.category(char) in ("Cc", "Cs") for char in value): raise bad_request("filename содержит путь или управляющие символы") return value def validate_image(stream, declared): """Check dimensions before decode; verify CRC and actually decode pixel data.""" try: stream.seek(0) with warnings.catch_warnings(): warnings.simplefilter("error", Image.DecompressionBombWarning) with Image.open(stream, formats=["PNG", "JPEG"]) as image: width, height = image.size if width * height > MAX_PIXELS: raise ApiError(413, "payload_too_large", "Изображение больше 40 млн пикселей") actual = {"PNG": "image/png", "JPEG": "image/jpeg"}[image.format] if actual != declared: raise ApiError(415, "unsupported_media_type", "MIME не соответствует содержимому") # Animated images are not part of the single-image upload contract. if getattr(image, "n_frames", 1) != 1: raise ApiError(415, "unsupported_media_type", "Нужно одно изображение") image.verify() stream.seek(0) with Image.open(stream, formats=["PNG", "JPEG"]) as image: image.load() return width, height except (Image.DecompressionBombWarning, Image.DecompressionBombError): raise ApiError(413, "payload_too_large", "Изображение больше 40 млн пикселей") from None except (UnidentifiedImageError, OSError, SyntaxError, ValueError, EOFError): raise ApiError(415, "unsupported_media_type", "Повреждённый PNG/JPEG") from None finally: stream.seek(0) async def save_attachment(session, storage, row, stream): """Commit intent BEFORE PUT; lock it until final ready commit. On failure keep the intent. A cleanup worker rechecks its state after grace; never delete blindly after an ambiguous COMMIT acknowledgement. """ session.add(row) await session.commit() try: locked = (await session.execute( select(Attachment.id).where(Attachment.id == row.id).with_for_update() )).scalar_one_or_none() if locked is None: raise ApiError(409, "conflict", "Срок незавершённой загрузки истёк") await anyio.to_thread.run_sync(storage.put, row.object_key, stream, row.size_bytes, row.content_type) row.status = "ready" row.updated_at = (await session.execute(select(func.clock_timestamp()))).scalar_one() await session.commit() except BaseException: await session.rollback() raise return row @router.post("/chats/{chat_id}/attachments", status_code=201, response_model=AttachmentOut) async def upload(chat_id: str, request: Request, user: User = Depends(current_user), session: AsyncSession = Depends(get_session)): chat = await open_chat(session, user, chat_id) filename = validate_filename(request.query_params.get("filename")) declared = request.headers.get("content-type", "").split(";", 1)[0].strip().lower() if declared not in ("image/png", "image/jpeg"): raise ApiError(415, "unsupported_media_type", "Нужен Content-Type image/png или image/jpeg") length = request.headers.get("content-length") if length is not None and (not length.isascii() or not length.isdigit()): raise bad_request("Некорректный Content-Length") if length is not None and (len(length) > 20 or int(length) > MAX_BYTES): raise ApiError(413, "payload_too_large", "Файл больше 10 MiB") chat_uuid, owner_uuid = chat.id, user.id # Do not hold a DB connection during the client's network upload or slot wait. await session.rollback() storage = request.app.state.storage if storage is None: raise unavailable() try: await asyncio.wait_for(request.app.state.upload_slots.acquire(), timeout=30) except TimeoutError: raise ApiError(503, "unavailable", "Все слоты загрузки заняты") from None try: with tempfile.TemporaryFile(mode="w+b") as stream: size, checksum = 0, hashlib.sha256() try: async with asyncio.timeout(30): async for chunk in request.stream(): size += len(chunk) if size > MAX_BYTES: raise ApiError(413, "payload_too_large", "Файл больше 10 MiB") checksum.update(chunk) stream.write(chunk) except TimeoutError: raise ApiError(408, "invalid_request", "Истекло время приёма файла") from None except ClientDisconnect: raise bad_request("Передача файла прервана") from None if length is not None and int(length) != size: raise bad_request("Неполное тело запроса") async with request.app.state.decode_slot: width, height = await anyio.to_thread.run_sync(validate_image, stream, declared) row = Attachment( id=uuid.uuid4(), chat_id=chat_uuid, owner_id=owner_uuid, filename=filename, object_key="originals/" + uuid.uuid4().hex, content_type=declared, size_bytes=size, sha256=checksum.hexdigest(), width=width, height=height, status="uploading", ) await save_attachment(session, storage, row, stream) return attachment_out(row) except StorageUnavailable: raise unavailable() from None finally: request.app.state.upload_slots.release() async def accessible_attachment(session, user, attachment_id): ident = uuid.UUID(parse_uuid(attachment_id, "attachment_id")) row = (await session.execute( select(Attachment).join(ChatMember, ChatMember.chat_id == Attachment.chat_id) .where(Attachment.id == ident, ChatMember.user_id == user.id) )).scalar_one_or_none() if row is None: raise ApiError(404, "not_found", "Объект не найден") if row.status != "ready": raise ApiError(409, "conflict", "Вложение ещё не готово") return row @router.get("/attachments/{attachment_id}", response_model=AttachmentOut) async def metadata(attachment_id: str, user: User = Depends(current_user), session: AsyncSession = Depends(get_session)): return attachment_out(await accessible_attachment(session, user, attachment_id)) @router.get("/attachments/{attachment_id}/content") async def content(attachment_id: str, request: Request, user: User = Depends(current_user), session: AsyncSession = Depends(get_session)): row = await accessible_attachment(session, user, attachment_id) if request.query_params.get("variant", "original") != "original": raise ApiError(404, "not_found", "Вариант не найден") key, mime, size, name = row.object_key, row.content_type, row.size_bytes, row.filename await session.rollback() storage = request.app.state.storage if storage is None: raise unavailable() try: response = await anyio.to_thread.run_sync(storage.get, key) except StorageUnavailable: raise unavailable() from None async def chunks(): try: while True: chunk = await anyio.to_thread.run_sync(response.read, 64 * 1024) if not chunk: break yield chunk finally: response.close() response.release_conn() return StreamingResponse(chunks(), media_type=mime, headers={ "Content-Length": str(size), "X-Content-Type-Options": "nosniff", "Cache-Control": "private, no-store", "Content-Disposition": "attachment; filename*=UTF-8''" + quote(name, safe=""), })