forked from mtusi2026/mtusi2026lab5
242 lines
8.5 KiB
Python
242 lines
8.5 KiB
Python
import hashlib
|
|
import re
|
|
import uuid
|
|
from datetime import datetime, timezone
|
|
from io import BytesIO
|
|
|
|
from fastapi import APIRouter, Header, Request
|
|
from fastapi.responses import JSONResponse, StreamingResponse
|
|
from PIL import Image, UnidentifiedImageError
|
|
|
|
from app.security import get_current_user
|
|
|
|
router = APIRouter(tags=["attachments"])
|
|
|
|
MAX_BYTES = 10 * 1024 * 1024
|
|
MAX_PIXELS = 40_000_000
|
|
ALLOWED = {"PNG": "image/png", "JPEG": "image/jpeg"}
|
|
BAD_FILENAME = re.compile(r'[/\\\x00-\x1f]')
|
|
BEARER_CHALLENGE = 'Bearer realm="messenger"'
|
|
CHUNK = 64 * 1024
|
|
|
|
|
|
def error_response(status, code, message, request_id, headers=None):
|
|
return JSONResponse(
|
|
status_code=status,
|
|
content={"error": {"code": code, "message": message, "request_id": request_id}},
|
|
headers=headers or {},
|
|
)
|
|
|
|
|
|
def validate_image(data: bytes):
|
|
if len(data) == 0:
|
|
raise ValueError("unsupported_media_type")
|
|
if len(data) > MAX_BYTES:
|
|
raise ValueError("payload_too_large")
|
|
try:
|
|
img = Image.open(BytesIO(data))
|
|
img.verify()
|
|
img = Image.open(BytesIO(data))
|
|
fmt = img.format
|
|
w, h = img.size
|
|
except (UnidentifiedImageError, OSError, ValueError):
|
|
raise ValueError("unsupported_media_type")
|
|
if fmt not in ALLOWED:
|
|
raise ValueError("unsupported_media_type")
|
|
if w * h > MAX_PIXELS:
|
|
raise ValueError("payload_too_large")
|
|
return ALLOWED[fmt], w, h, hashlib.sha256(data).hexdigest()
|
|
|
|
|
|
def safe_filename(name: str) -> str:
|
|
if not name or len(name) > 255:
|
|
raise ValueError("invalid_request")
|
|
if BAD_FILENAME.search(name):
|
|
raise ValueError("invalid_request")
|
|
return name
|
|
|
|
|
|
async def _auth(request, authorization):
|
|
try:
|
|
user, _ = await get_current_user(request, authorization)
|
|
return user, None
|
|
except Exception:
|
|
return None, error_response(
|
|
401, "unauthorized", "unauthorized", request.state.request_id,
|
|
headers={"WWW-Authenticate": BEARER_CHALLENGE},
|
|
)
|
|
|
|
|
|
async def _read_limited(request, limit):
|
|
buf = bytearray()
|
|
async for chunk in request.stream():
|
|
buf.extend(chunk)
|
|
if len(buf) > limit:
|
|
raise ValueError("payload_too_large")
|
|
return bytes(buf)
|
|
|
|
|
|
@router.post("/chats/{chat_id}/attachments", status_code=202)
|
|
async def upload_attachment(chat_id: str, filename: str, request: Request,
|
|
authorization: str | None = Header(default=None)):
|
|
rid = request.state.request_id
|
|
try:
|
|
uuid.UUID(chat_id)
|
|
except (ValueError, TypeError):
|
|
return error_response(400, "invalid_request", "invalid uuid", rid)
|
|
|
|
user, err = await _auth(request, authorization)
|
|
if err is not None:
|
|
return err
|
|
|
|
if not await request.app.state.storage.is_chat_member(chat_id, user["id"]):
|
|
return error_response(404, "not_found", "chat not found", rid)
|
|
|
|
try:
|
|
safe_filename(filename)
|
|
except ValueError:
|
|
return error_response(400, "invalid_request", "invalid filename", rid)
|
|
|
|
try:
|
|
data = await _read_limited(request, MAX_BYTES)
|
|
except ValueError:
|
|
return error_response(413, "payload_too_large", "payload_too_large", rid)
|
|
|
|
try:
|
|
content_type, w, h, sha = validate_image(data)
|
|
except ValueError as e:
|
|
code = str(e)
|
|
status = 413 if code == "payload_too_large" else 415
|
|
return error_response(status, code, code, rid)
|
|
|
|
att_id = str(uuid.uuid4())
|
|
staging_key = f"staging/{chat_id}/{att_id}"
|
|
|
|
try:
|
|
request.app.state.s3.put(staging_key, data, content_type)
|
|
except Exception:
|
|
return error_response(503, "unavailable", "storage unavailable", rid)
|
|
|
|
job = {
|
|
"schema_version": 1,
|
|
"job_id": str(uuid.uuid4()),
|
|
"type": "attachment.process.v1",
|
|
"attachment_id": att_id,
|
|
"pipeline_version": 1,
|
|
"attempt": 1,
|
|
"created_at": datetime.now(timezone.utc).isoformat().replace("+00:00", "Z"),
|
|
}
|
|
try:
|
|
att = await request.app.state.storage.create_attachment_with_job_and_outbox(
|
|
att_id, chat_id, user["id"], staging_key,
|
|
filename, content_type, len(data), sha, job,
|
|
)
|
|
except Exception:
|
|
try:
|
|
request.app.state.s3.remove(staging_key)
|
|
except Exception:
|
|
pass
|
|
return error_response(503, "unavailable", "queue unavailable", rid)
|
|
|
|
return att
|
|
|
|
|
|
@router.get("/attachments/{attachment_id}")
|
|
async def get_attachment(attachment_id: str, request: Request,
|
|
authorization: str | None = Header(default=None)):
|
|
rid = request.state.request_id
|
|
try:
|
|
uuid.UUID(attachment_id)
|
|
except (ValueError, TypeError):
|
|
return error_response(400, "invalid_request", "invalid uuid", rid)
|
|
user, err = await _auth(request, authorization)
|
|
if err is not None:
|
|
return err
|
|
att = await request.app.state.storage.get_attachment(attachment_id)
|
|
if att is None:
|
|
return error_response(404, "not_found", "attachment not found", rid)
|
|
if not await request.app.state.storage.is_chat_member(att["chat_id"], user["id"]):
|
|
return error_response(404, "not_found", "attachment not found", rid)
|
|
att.pop("_object_key", None)
|
|
att.pop("_variant_keys", None)
|
|
return att
|
|
|
|
|
|
@router.post("/attachments/{attachment_id}/replay", status_code=202)
|
|
async def replay_attachment(attachment_id: str, request: Request,
|
|
authorization: str | None = Header(default=None)):
|
|
rid = request.state.request_id
|
|
try:
|
|
uuid.UUID(attachment_id)
|
|
except (ValueError, TypeError):
|
|
return error_response(400, "invalid_request", "invalid uuid", rid)
|
|
user, err = await _auth(request, authorization)
|
|
if err is not None:
|
|
return err
|
|
att = await request.app.state.storage.get_attachment(attachment_id)
|
|
if att is None:
|
|
return error_response(404, "not_found", "attachment not found", rid)
|
|
if not await request.app.state.storage.is_chat_member(att["chat_id"], user["id"]):
|
|
return error_response(404, "not_found", "attachment not found", rid)
|
|
result = await request.app.state.storage.replay_failed_attachment(attachment_id)
|
|
if result == "not_found":
|
|
return error_response(404, "not_found", "attachment not found", rid)
|
|
if result == "already_ready":
|
|
return error_response(409, "conflict", "attachment already ready", rid)
|
|
if result == "not_failed":
|
|
return error_response(409, "conflict", "attachment is not failed", rid)
|
|
att = await request.app.state.storage.get_attachment(attachment_id)
|
|
att.pop("_object_key", None)
|
|
att.pop("_variant_keys", None)
|
|
return att
|
|
|
|
|
|
@router.get("/attachments/{attachment_id}/content")
|
|
async def download_attachment(attachment_id: str, request: Request,
|
|
variant: str = "original",
|
|
authorization: str | None = Header(default=None)):
|
|
rid = request.state.request_id
|
|
try:
|
|
uuid.UUID(attachment_id)
|
|
except (ValueError, TypeError):
|
|
return error_response(400, "invalid_request", "invalid uuid", rid)
|
|
user, err = await _auth(request, authorization)
|
|
if err is not None:
|
|
return err
|
|
att = await request.app.state.storage.get_attachment(attachment_id)
|
|
if att is None:
|
|
return error_response(404, "not_found", "attachment not found", rid)
|
|
if not await request.app.state.storage.is_chat_member(att["chat_id"], user["id"]):
|
|
return error_response(404, "not_found", "attachment not found", rid)
|
|
|
|
if att["status"] in ("queued", "processing"):
|
|
return error_response(409, "conflict", "attachment not ready", rid)
|
|
if att["status"] == "failed":
|
|
return error_response(409, "conflict", "attachment failed", rid)
|
|
|
|
if variant == "original":
|
|
obj_key = att["_object_key"]
|
|
content_type = att["content_type"]
|
|
size_bytes = att["size_bytes"]
|
|
else:
|
|
found = next((v for v in att["variants"] if v["name"] == variant), None)
|
|
if not found:
|
|
return error_response(404, "not_found", "variant not found", rid)
|
|
|
|
obj_key = att["_variant_keys"].get(variant)
|
|
if not obj_key:
|
|
return error_response(404, "not_found", "variant key not found", rid)
|
|
|
|
content_type = found["content_type"]
|
|
size_bytes = found["size_bytes"]
|
|
|
|
try:
|
|
obj = request.app.state.s3.get(obj_key)
|
|
except Exception:
|
|
return error_response(503, "unavailable", "storage unavailable", rid)
|
|
|
|
return StreamingResponse(
|
|
obj.stream(CHUNK),
|
|
media_type=content_type,
|
|
headers={"Content-Length": str(size_bytes)},
|
|
) |