Files
mtusi2026lab5/app/routers/attachments.py
T

245 lines
9.4 KiB
Python

import os
import uuid
import hashlib
import magic
import tempfile
from datetime import datetime, timezone
from fastapi import APIRouter, Depends, HTTPException, status, Query, Request
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.database import get_db
from app.models import Attachment, ChatMember, AttachmentJob, JobOutbox
from app.schemas import Attachment as AttachmentSchema, Variant, ErrorResponse
from app.dependencies import get_current_user
from app.s3_client import get_s3_client, upload_file_to_s3, delete_file_from_s3
from minio import Minio
from PIL import Image
import io
from fastapi.responses import Response
router = APIRouter()
@router.post("/api/v1/chats/{chat_id}/attachments", status_code=status.HTTP_202_ACCEPTED, responses={202: {"model": AttachmentSchema}, 400: {"model": ErrorResponse}, 401: {"model": ErrorResponse}, 404: {"model": ErrorResponse}, 413: {"model": ErrorResponse}, 415: {"model": ErrorResponse}, 503: {"model": ErrorResponse}})
async def upload_attachment(
chat_id: uuid.UUID,
filename: str = Query(..., min_length=1, max_length=255),
request: Request = None,
current_user = Depends(get_current_user),
db: AsyncSession = Depends(get_db),
s3: Minio = Depends(get_s3_client)
):
# 1. Проверка доступа к чату
is_member = await db.execute(
select(ChatMember).where(ChatMember.chat_id == chat_id, ChatMember.user_id == current_user.id)
)
if not is_member.scalar_one_or_none():
raise HTTPException(status_code=404, detail={"code": "not_found", "message": "Chat not found or access denied"})
# 2. Чтение сырых байтов с ограничением (защита от DoS)
MAX_SIZE = 10 * 1024 * 1024 # 10 MiB
body = await request.body()
if len(body) > MAX_SIZE:
raise HTTPException(status_code=413, detail={"code": "payload_too_large", "message": "File size exceeds 10 MiB limit"})
if len(body) == 0:
raise HTTPException(status_code=400, detail={"code": "invalid_request", "message": "Empty file"})
# 3. Определение MIME-типа по содержимому
mime = magic.from_buffer(body, mime=True)
if mime not in ("image/png", "image/jpeg"):
raise HTTPException(status_code=415, detail={"code": "unsupported_media_type", "message": "Only image/png and image/jpeg are supported"})
# 4. Быстрая проверка размеров изображения (без полного декодирования)
try:
img = Image.open(io.BytesIO(body))
width, height = img.size
if width * height > 40_000_000:
raise HTTPException(status_code=413, detail={"code": "payload_too_large", "message": "Image resolution exceeds 40 million pixels"})
except Exception:
raise HTTPException(status_code=400, detail={"code": "invalid_request", "message": "Invalid or corrupted image file"})
# 5. Вычисление SHA256
sha256_hash = hashlib.sha256(body).hexdigest()
# 6. Генерация ключей
att_id = uuid.uuid4()
ext = "png" if mime == "image/png" else "jpeg"
staging_key = f"staging/{att_id}.{ext}"
# 7. Загрузка в S3 staging
with tempfile.NamedTemporaryFile(delete=False, suffix=f".{ext}") as tmp:
tmp.write(body)
tmp_path = tmp.name
try:
await upload_file_to_s3(staging_key, tmp_path, mime)
except Exception:
raise HTTPException(status_code=503, detail={"code": "unavailable", "message": "Failed to upload to S3 staging"})
finally:
os.unlink(tmp_path)
# 8. Транзакция: Создание Attachment, Job и Outbox сообщения
job_id = uuid.uuid4()
now = datetime.now(timezone.utc)
new_attachment = Attachment(
id=att_id,
owner_id=current_user.id,
chat_id=chat_id,
staging_key=staging_key,
original_filename=filename,
content_type=mime,
size_bytes=len(body),
sha256=sha256_hash,
status="queued"
)
new_job = AttachmentJob(
job_id=job_id,
attachment_id=att_id,
attempt=1,
status="queued"
)
outbox_payload = {
"job_id": str(job_id),
"attachment_id": str(att_id),
"attempt": 1
}
new_outbox = JobOutbox(
job_id=job_id,
payload=outbox_payload,
published_at=None
)
db.add(new_attachment)
db.add(new_job)
db.add(new_outbox)
try:
await db.commit()
except Exception:
await db.rollback()
try:
await delete_file_from_s3(staging_key)
except Exception:
pass
raise HTTPException(status_code=503, detail={"code": "unavailable", "message": "Database error during job creation"})
now_str = now.isoformat().replace('+00:00', 'Z')
# Возвращаем ПОЛНЫЙ объект AttachmentSchema, как требует OpenAPI контракт
return AttachmentSchema(
id=str(att_id),
chat_id=str(chat_id),
owner_id=str(current_user.id),
filename=filename,
content_type=mime,
size_bytes=len(body),
sha256=sha256_hash,
status="queued",
variants=[],
error_code=None,
created_at=now_str,
updated_at=now_str
)
@router.get("/api/v1/attachments/{attachment_id}", response_model=AttachmentSchema, responses={401: {"model": ErrorResponse}, 404: {"model": ErrorResponse}})
async def get_attachment_metadata(
attachment_id: uuid.UUID,
current_user = Depends(get_current_user),
db: AsyncSession = Depends(get_db)
):
stmt = select(Attachment).where(Attachment.id == attachment_id)
result = await db.execute(stmt)
attachment = result.scalar_one_or_none()
if not attachment:
raise HTTPException(status_code=404, detail={"code": "not_found", "message": "Attachment not found"})
is_member = await db.execute(
select(ChatMember).where(ChatMember.chat_id == attachment.chat_id, ChatMember.user_id == current_user.id)
)
if not is_member.scalar_one_or_none():
raise HTTPException(status_code=404, detail={"code": "not_found", "message": "Access denied"})
variants_list = []
if attachment.status == "ready":
from app.models import AttachmentVariant
var_stmt = select(AttachmentVariant).where(AttachmentVariant.attachment_id == attachment_id)
var_result = await db.execute(var_stmt)
for v in var_result.scalars().all():
variants_list.append(Variant(
name=v.variant_name,
content_type=v.content_type,
size_bytes=v.size_bytes,
width=v.width,
height=v.height,
sha256=v.sha256
))
now_str = attachment.created_at.isoformat().replace('+00:00', 'Z')
return AttachmentSchema(
id=str(attachment.id),
chat_id=str(attachment.chat_id),
owner_id=str(attachment.owner_id),
filename=attachment.original_filename,
content_type=attachment.content_type,
size_bytes=attachment.size_bytes,
sha256=attachment.sha256,
status=attachment.status,
variants=variants_list,
error_code=attachment.error_code,
created_at=now_str,
updated_at=now_str
)
@router.get("/api/v1/attachments/{attachment_id}/content", responses={401: {"model": ErrorResponse}, 404: {"model": ErrorResponse}, 409: {"model": ErrorResponse}})
async def download_attachment(
attachment_id: uuid.UUID,
variant: str = Query(default="original"),
current_user = Depends(get_current_user),
db: AsyncSession = Depends(get_db),
s3: Minio = Depends(get_s3_client)
):
stmt = select(Attachment).where(Attachment.id == attachment_id)
result = await db.execute(stmt)
attachment = result.scalar_one_or_none()
if not attachment:
raise HTTPException(status_code=404, detail={"code": "not_found", "message": "Attachment not found"})
is_member = await db.execute(
select(ChatMember).where(ChatMember.chat_id == attachment.chat_id, ChatMember.user_id == current_user.id)
)
if not is_member.scalar_one_or_none():
raise HTTPException(status_code=404, detail={"code": "not_found", "message": "Access denied"})
if attachment.status != "ready":
raise HTTPException(status_code=409, detail={"code": "conflict", "message": f"Attachment is not ready yet (status: {attachment.status})"})
if variant == "original":
s3_key = attachment.s3_key
content_type = attachment.content_type
else:
from app.models import AttachmentVariant
var_stmt = select(AttachmentVariant).where(
AttachmentVariant.attachment_id == attachment_id,
AttachmentVariant.variant_name == variant
)
var_result = await db.execute(var_stmt)
var_obj = var_result.scalar_one_or_none()
if not var_obj:
raise HTTPException(status_code=404, detail={"code": "not_found", "message": f"Variant {variant} not found"})
s3_key = var_obj.s3_key
content_type = var_obj.content_type
try:
response = s3.get_object("messenger-attachments", s3_key)
data = response.read()
response.close()
response.release_conn()
except Exception:
raise HTTPException(status_code=503, detail={"code": "unavailable", "message": "Failed to download from S3"})
return Response(content=data, media_type=content_type)