Files
mtusi2026lab5/src/app/api/attachments.py
T

228 lines
7.6 KiB
Python

"""Вложения (lab5): upload → 202 queued, async worker, download вариантов."""
import hashlib
import io
import os
import unicodedata
from datetime import datetime, timezone
from typing import Optional
from uuid import UUID, uuid4
from fastapi import APIRouter, Depends, HTTPException, Query, Request
from fastapi.responses import Response
from PIL import Image, UnidentifiedImageError
from starlette.concurrency import run_in_threadpool
from botocore.exceptions import ClientError
from ..models.schemas import Attachment, User
from ..storage import s3
from ..storage.postgres import storage
from .security import current_user
router = APIRouter(prefix="/api/v1", tags=["attachments"])
MAX_UPLOAD_BYTES = int(os.environ.get("MAX_UPLOAD_BYTES", "10485760"))
MAX_IMAGE_PIXELS = int(os.environ.get("MAX_IMAGE_PIXELS", "40000000"))
PIPELINE_VERSION = 1
def _require_member(chat_id: UUID, user_id: UUID):
chat = storage.get_chat(chat_id)
if chat is None or user_id not in chat.member_ids:
raise HTTPException(status_code=404, detail="not_found")
return chat
def _validate_filename(name: str) -> str:
if not name or len(name) > 255:
raise HTTPException(status_code=400, detail="invalid_request")
if name.strip() != name or name.strip() == "":
raise HTTPException(status_code=400, detail="invalid_request")
for ch in name:
if ch in ("/", "\\", "\x00") or unicodedata.category(ch).startswith("C"):
raise HTTPException(status_code=400, detail="invalid_request")
return name
def _detect_content_type(data: bytes) -> Optional[str]:
"""MIME по содержимому, не по заголовку."""
if data.startswith(b"\x89PNG\r\n\x1a\n"):
return "image/png"
if data.startswith(b"\xff\xd8\xff"):
return "image/jpeg"
return None
def _validate_image(data: bytes) -> str:
"""MIME по содержимому + лимит пикселей + полная проверка декодирования."""
ctype = _detect_content_type(data)
if ctype is None:
raise HTTPException(status_code=415, detail="unsupported_media_type")
try:
img = Image.open(io.BytesIO(data))
width, height = img.size
if width <= 0 or height <= 0:
raise HTTPException(status_code=415, detail="unsupported_media_type")
if width * height > MAX_IMAGE_PIXELS:
raise HTTPException(status_code=413, detail="payload_too_large")
img.load()
except HTTPException:
raise
except Image.DecompressionBombError:
raise HTTPException(status_code=413, detail="payload_too_large")
except Exception:
raise HTTPException(status_code=415, detail="unsupported_media_type")
return ctype
@router.post(
path="/chats/{chat_id}/attachments",
summary="Принять один PNG/JPEG, до 10 MiB (lab5: 202 queued)",
response_model=Attachment,
status_code=202,
)
async def upload_attachment(
chat_id: UUID,
request: Request,
user: User = Depends(current_user),
filename: str = Query(...),
) -> Attachment:
_require_member(chat_id, user.id)
safe_name = _validate_filename(filename)
cl = request.headers.get("content-length")
if cl is not None:
try:
if int(cl) > MAX_UPLOAD_BYTES:
raise HTTPException(status_code=413, detail="payload_too_large")
except ValueError:
raise HTTPException(status_code=400, detail="invalid_request")
chunks: list[bytes] = []
total = 0
async for chunk in request.stream():
if not chunk:
continue
total += len(chunk)
if total > MAX_UPLOAD_BYTES:
raise HTTPException(status_code=413, detail="payload_too_large")
chunks.append(chunk)
body = b"".join(chunks)
if not body:
raise HTTPException(status_code=400, detail="invalid_request")
content_type = await run_in_threadpool(_validate_image, body)
sha256 = hashlib.sha256(body).hexdigest()
attachment_id = uuid4()
staging_key = s3.staging_key(chat_id, attachment_id)
# 1) Durable staging в S3 (приватный prefix).
try:
await run_in_threadpool(s3.put_bytes, staging_key, body, content_type)
except Exception as exc: # noqa: BLE001
raise HTTPException(status_code=503, detail="unavailable") from exc
now = datetime.now(timezone.utc)
att = Attachment(
id=attachment_id,
chat_id=chat_id,
owner_id=user.id,
filename=safe_name,
content_type=content_type,
size_bytes=len(body),
sha256=sha256,
status="queued",
variants=[],
error_code=None,
created_at=now,
updated_at=now,
)
job_id = uuid4()
outbox_payload = {
"job_id": str(job_id),
"type": "attachment.process.v1",
"attachment_id": str(attachment_id),
"pipeline_version": PIPELINE_VERSION,
"created_at": now.isoformat().replace("+00:00", "Z"),
}
# 2) INSERT attachment + outbox в ОДНОЙ транзакции.
# При сбое — компенсация: удаляем staging.
try:
await run_in_threadpool(
storage.create_attachment_queued, att, staging_key, outbox_payload
)
except Exception:
try:
await run_in_threadpool(s3.delete_object, staging_key)
except Exception:
pass
raise HTTPException(status_code=503, detail="unavailable")
return att
@router.get(
path="/attachments/{attachment_id}",
summary="Метаданные вложения, доступ участникам чата",
response_model=Attachment,
)
def get_attachment(
attachment_id: UUID,
user: User = Depends(current_user),
) -> Attachment:
pair = storage.get_attachment_with_key(attachment_id)
if pair is None:
raise HTTPException(status_code=404, detail="not_found")
att, _key = pair
_require_member(att.chat_id, user.id)
return att
@router.get(
path="/attachments/{attachment_id}/content",
summary="Скачать байты (original или variant) через авторизованный API",
)
def download_attachment(
attachment_id: UUID,
user: User = Depends(current_user),
variant: str = Query("original"),
):
if variant not in ("original", "thumbnail", "crop", "optimized"):
raise HTTPException(status_code=400, detail="invalid_request")
pair = storage.get_attachment_with_key(attachment_id)
if pair is None:
raise HTTPException(status_code=404, detail="not_found")
att, object_key = pair
_require_member(att.chat_id, user.id)
if att.status != "ready":
raise HTTPException(status_code=409, detail="conflict")
if variant == "original":
key = object_key
ctype_expected = att.content_type
else:
vs = [v for v in att.variants if v.name == variant]
if not vs:
raise HTTPException(status_code=404, detail="not_found")
v = vs[0]
key = s3.variant_key(att.chat_id, att.id, variant)
ctype_expected = v.content_type
try:
data, ctype = s3.get_object(key)
except ClientError as exc:
code = exc.response.get("Error", {}).get("Code", "")
if code in ("404", "NoSuchKey", "NotFound"):
raise HTTPException(status_code=404, detail="not_found")
raise HTTPException(status_code=503, detail="unavailable") from exc
except Exception as exc: # noqa: BLE001
raise HTTPException(status_code=503, detail="unavailable") from exc
return Response(
content=data,
media_type=ctype or ctype_expected,
)