forked from mtusi2026/mtusi2026lab5
lab5: асинхронный воркер, брокер сообщений
This commit is contained in:
@@ -0,0 +1,286 @@
|
||||
import uuid
|
||||
import filetype
|
||||
import io
|
||||
import hashlib
|
||||
from datetime import datetime, timezone
|
||||
from PIL import Image
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, Request
|
||||
from fastapi.responses import StreamingResponse
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy import select, func, text
|
||||
|
||||
from app.dependencies import get_session
|
||||
from app.security import get_current_user
|
||||
from app.models.user import User
|
||||
from app.models.attachment import Attachment
|
||||
from app.models.outbox import OutboxEvent
|
||||
from app.models.chat import ChatMember
|
||||
from app.storage import minio_client, BUCKET
|
||||
|
||||
|
||||
router_upload = APIRouter(
|
||||
prefix="/api/v1/chats/{chat_id}/attachments",
|
||||
tags=["attachments"]
|
||||
)
|
||||
|
||||
router_download = APIRouter(
|
||||
prefix="/api/v1/attachments",
|
||||
tags=["attachments"]
|
||||
)
|
||||
|
||||
MAX_SIZE = 10 * 1024 * 1024
|
||||
MAX_PIXELS = 40_000_000
|
||||
MAX_ACTIVE_ATTACHMENTS = 100
|
||||
ADMISSION_LOCK_KEY = 823164001
|
||||
|
||||
def format_utc_timestamp(value: datetime | None) -> str:
|
||||
if value is None:
|
||||
value = datetime.now(timezone.utc)
|
||||
|
||||
elif value.tzinfo is None:
|
||||
value = value.replace(tzinfo=timezone.utc)
|
||||
|
||||
return (
|
||||
value.astimezone(timezone.utc)
|
||||
.isoformat()
|
||||
.replace("+00:00", "Z")
|
||||
)
|
||||
|
||||
|
||||
def build_attachment_response(att: Attachment) -> dict:
|
||||
variants = []
|
||||
|
||||
if (
|
||||
att.status == "ready"
|
||||
and att.thumbnail_key
|
||||
and att.thumbnail_sha256
|
||||
and att.thumbnail_size_bytes is not None
|
||||
and att.thumbnail_width is not None
|
||||
and att.thumbnail_height is not None
|
||||
):
|
||||
variants.append({
|
||||
"name": "thumbnail",
|
||||
"content_type": "image/jpeg",
|
||||
"size_bytes": att.thumbnail_size_bytes,
|
||||
"width": att.thumbnail_width,
|
||||
"height": att.thumbnail_height,
|
||||
"sha256": att.thumbnail_sha256,
|
||||
})
|
||||
|
||||
return {
|
||||
"id": str(att.id),
|
||||
"chat_id": str(att.chat_id),
|
||||
"owner_id": str(att.user_id),
|
||||
"filename": att.filename,
|
||||
"content_type": att.mime_type,
|
||||
"size_bytes": att.size_bytes,
|
||||
"sha256": att.sha256,
|
||||
"status": att.status,
|
||||
"variants": variants,
|
||||
"error_code": att.error_code,
|
||||
"created_at": format_utc_timestamp(att.created_at),
|
||||
"updated_at": format_utc_timestamp(att.updated_at),
|
||||
}
|
||||
|
||||
|
||||
@router_upload.post("", status_code=202)
|
||||
async def upload_attachment(
|
||||
chat_id: uuid.UUID,
|
||||
filename: str = Query(
|
||||
...,
|
||||
min_length=1,
|
||||
max_length=255,
|
||||
pattern=r"^[^/\\\x00]+$"
|
||||
),
|
||||
request: Request = None,
|
||||
user: User = Depends(get_current_user),
|
||||
session: AsyncSession = Depends(get_session),
|
||||
):
|
||||
result = await session.execute(
|
||||
select(ChatMember).where(
|
||||
ChatMember.chat_id == chat_id,
|
||||
ChatMember.user_id == user.id,
|
||||
)
|
||||
)
|
||||
|
||||
if not result.scalar_one_or_none():
|
||||
raise HTTPException(404, "Чат не найден")
|
||||
|
||||
sha_hasher = hashlib.sha256()
|
||||
buffer = io.BytesIO()
|
||||
total = 0
|
||||
first_bytes = b""
|
||||
|
||||
async for chunk in request.stream():
|
||||
if not chunk:
|
||||
continue
|
||||
|
||||
total += len(chunk)
|
||||
|
||||
if total > MAX_SIZE:
|
||||
raise HTTPException(413, "File too large")
|
||||
|
||||
sha_hasher.update(chunk)
|
||||
buffer.write(chunk)
|
||||
|
||||
if len(first_bytes) < 262:
|
||||
need = 262 - len(first_bytes)
|
||||
first_bytes += chunk[:need]
|
||||
|
||||
kind = filetype.guess(first_bytes)
|
||||
|
||||
if not kind or kind.mime not in ("image/png", "image/jpeg"):
|
||||
raise HTTPException(400, "Only PNG/JPEG allowed")
|
||||
|
||||
contents = buffer.getvalue()
|
||||
|
||||
try:
|
||||
img = Image.open(io.BytesIO(contents))
|
||||
|
||||
if img.width * img.height > MAX_PIXELS:
|
||||
raise HTTPException(400, "Image too large (pixels)")
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception:
|
||||
raise HTTPException(400, "Corrupted image")
|
||||
|
||||
# Serialize admission checks across all API instances.
|
||||
# This transaction-level PostgreSQL lock is released on commit/rollback.
|
||||
await session.execute(
|
||||
text("SELECT pg_advisory_xact_lock(:lock_key)"),
|
||||
{"lock_key": ADMISSION_LOCK_KEY},
|
||||
)
|
||||
|
||||
active_count = await session.scalar(
|
||||
select(func.count())
|
||||
.select_from(Attachment)
|
||||
.where(Attachment.status.in_(("queued", "processing")))
|
||||
)
|
||||
|
||||
if active_count >= MAX_ACTIVE_ATTACHMENTS:
|
||||
await session.rollback()
|
||||
raise HTTPException(
|
||||
status_code=429,
|
||||
detail="Image processing queue is full; try again later",
|
||||
headers={"Retry-After": "30"},
|
||||
)
|
||||
|
||||
object_key = f"{uuid.uuid4()}.{kind.extension}"
|
||||
|
||||
minio_client.put_object(
|
||||
BUCKET,
|
||||
object_key,
|
||||
io.BytesIO(contents),
|
||||
len(contents),
|
||||
content_type=kind.mime,
|
||||
)
|
||||
|
||||
sha_hash = sha_hasher.hexdigest()
|
||||
|
||||
att = Attachment(
|
||||
object_key=object_key,
|
||||
filename=filename,
|
||||
mime_type=kind.mime,
|
||||
size_bytes=len(contents),
|
||||
chat_id=chat_id,
|
||||
user_id=user.id,
|
||||
sha256=sha_hash,
|
||||
status="queued"
|
||||
)
|
||||
|
||||
session.add(att)
|
||||
await session.flush()
|
||||
|
||||
event = OutboxEvent(attachment_id=att.id)
|
||||
session.add(event)
|
||||
|
||||
await session.commit()
|
||||
await session.refresh(att)
|
||||
|
||||
return build_attachment_response(att)
|
||||
|
||||
|
||||
@router_download.get("/{attachment_id}/content")
|
||||
async def download_attachment_content(
|
||||
attachment_id: uuid.UUID,
|
||||
variant: str = "original",
|
||||
user: User = Depends(get_current_user),
|
||||
session: AsyncSession = Depends(get_session),
|
||||
):
|
||||
att = await session.get(Attachment, attachment_id)
|
||||
|
||||
if not att:
|
||||
raise HTTPException(404, "Not found")
|
||||
|
||||
result = await session.execute(
|
||||
select(ChatMember).where(
|
||||
ChatMember.chat_id == att.chat_id,
|
||||
ChatMember.user_id == user.id,
|
||||
)
|
||||
)
|
||||
|
||||
if not result.scalar_one_or_none():
|
||||
raise HTTPException(404, "Not found")
|
||||
|
||||
if att.status != "ready":
|
||||
raise HTTPException(409, "File is not ready yet")
|
||||
|
||||
if variant == "thumbnail":
|
||||
if not att.thumbnail_key:
|
||||
raise HTTPException(404, "Thumbnail not found")
|
||||
|
||||
key_to_download = att.thumbnail_key
|
||||
mime = "image/jpeg"
|
||||
|
||||
elif variant == "original":
|
||||
key_to_download = att.object_key
|
||||
mime = att.mime_type
|
||||
|
||||
else:
|
||||
raise HTTPException(404, "Variant not found")
|
||||
|
||||
try:
|
||||
response = minio_client.get_object(
|
||||
BUCKET,
|
||||
key_to_download
|
||||
)
|
||||
|
||||
return StreamingResponse(
|
||||
response,
|
||||
media_type=mime,
|
||||
headers={
|
||||
"Content-Disposition":
|
||||
f'attachment; filename="{att.filename}"'
|
||||
},
|
||||
)
|
||||
|
||||
except Exception:
|
||||
raise HTTPException(
|
||||
404,
|
||||
"File not found in storage"
|
||||
)
|
||||
|
||||
|
||||
@router_download.get("/{attachment_id}")
|
||||
async def get_attachment_metadata(
|
||||
attachment_id: uuid.UUID,
|
||||
user: User = Depends(get_current_user),
|
||||
session: AsyncSession = Depends(get_session),
|
||||
):
|
||||
att = await session.get(Attachment, attachment_id)
|
||||
|
||||
if not att:
|
||||
raise HTTPException(404, "Not found")
|
||||
|
||||
result = await session.execute(
|
||||
select(ChatMember).where(
|
||||
ChatMember.chat_id == att.chat_id,
|
||||
ChatMember.user_id == user.id,
|
||||
)
|
||||
)
|
||||
|
||||
if not result.scalar_one_or_none():
|
||||
raise HTTPException(404, "Not found")
|
||||
|
||||
return build_attachment_response(att)
|
||||
Reference in New Issue
Block a user