forked from mtusi2026/mtusi2026lab4
Реализовать lab4 на оценку 4: SeaweedFS, вложения и согласованность БД/S3.
Перенесён стенд lab3 (API, Redis-сессии, идемпотентность, HAProxy, реплики API). Добавлены приватный bucket SeaweedFS с отдельным ключом API, загрузка PNG/JPEG с лимитами и полной проверкой, компенсация и reconciler, плавная остановка API, тесты и evidence.
This commit is contained in:
@@ -0,0 +1,823 @@
|
||||
"""HTTP API: multi-replica messenger behind LB with attachments in private S3 (lab4)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import base64
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
import urllib.parse
|
||||
import uuid
|
||||
from contextlib import asynccontextmanager
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any, List, Optional, Tuple
|
||||
|
||||
from fastapi import FastAPI, Header, Query, Request
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from fastapi.responses import JSONResponse, Response, StreamingResponse
|
||||
from starlette.concurrency import run_in_threadpool
|
||||
from starlette.exceptions import HTTPException as StarletteHTTPException
|
||||
from starlette.requests import ClientDisconnect
|
||||
|
||||
from messenger import attachments, config, db, lifecycle, reconcile, s3, sessions
|
||||
from messenger.sessions import SessionStoreError
|
||||
from messenger.store import Attachment, AttachmentNotFound, IntentLost, Store, StoreUnavailable, format_ts
|
||||
|
||||
USERNAME_RE = re.compile(r"^[a-z0-9_]{3,32}\Z")
|
||||
UUID_RE = re.compile(
|
||||
r"^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\Z"
|
||||
)
|
||||
IDEMPOTENCY_KEY_RE = re.compile(r"^[A-Za-z0-9._:-]{1,128}\Z")
|
||||
BODY_READ_TIMEOUT_S = 5.0
|
||||
PASSWORD_MIN = 12
|
||||
PASSWORD_MAX = 128
|
||||
|
||||
store = Store()
|
||||
ready = False
|
||||
upload_slots: Optional[asyncio.Semaphore] = None
|
||||
INSTANCE_ID = config.instance_id()
|
||||
VARIANT_NAMES = ("thumbnail", "crop", "optimized")
|
||||
UPLOAD_SLOT_WAIT_S = 5.0
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format="%(asctime)s %(levelname)s %(message)s",
|
||||
)
|
||||
log = logging.getLogger("messenger")
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(_app: FastAPI):
|
||||
# SIGTERM/SIGINT are handled by lifecycle.DrainingServer: readiness goes 503 first,
|
||||
# requests are served through the drain window and this shutdown runs last.
|
||||
global ready, upload_slots
|
||||
db.init_pool()
|
||||
# Migrations run once via `python -m messenger.migrate` (compose migrate service).
|
||||
sessions.init_client()
|
||||
s3.init_client()
|
||||
attachments.prepare_spool_dir()
|
||||
upload_slots = asyncio.Semaphore(config.max_concurrent_uploads())
|
||||
reconcile.start_background()
|
||||
ready = True
|
||||
log.info("messenger started instance_id=%s", INSTANCE_ID)
|
||||
yield
|
||||
ready = False
|
||||
reconcile.stop_background()
|
||||
sessions.close_client()
|
||||
db.close_pool()
|
||||
log.info("messenger stopped")
|
||||
|
||||
|
||||
def request_id_from(request: Request) -> str:
|
||||
incoming = request.headers.get("x-request-id")
|
||||
if incoming and incoming.strip():
|
||||
return incoming.strip()[:128]
|
||||
return str(uuid.uuid4())
|
||||
|
||||
|
||||
def error_body(code: str, message: str, request_id: str) -> dict:
|
||||
return {"error": {"code": code, "message": message, "request_id": request_id}}
|
||||
|
||||
|
||||
def common_headers(request_id: str, extra: Optional[dict] = None) -> dict:
|
||||
headers = {"X-Request-Id": request_id, "X-Instance-Id": INSTANCE_ID}
|
||||
if extra:
|
||||
headers.update(extra)
|
||||
return headers
|
||||
|
||||
|
||||
def error_response(
|
||||
status: int,
|
||||
code: str,
|
||||
message: str,
|
||||
request_id: str,
|
||||
www_authenticate: Optional[str] = None,
|
||||
extra_headers: Optional[dict] = None,
|
||||
) -> JSONResponse:
|
||||
headers = common_headers(request_id, extra_headers)
|
||||
if www_authenticate:
|
||||
headers["WWW-Authenticate"] = www_authenticate
|
||||
return JSONResponse(
|
||||
status_code=status,
|
||||
content=error_body(code, message, request_id),
|
||||
headers=headers,
|
||||
)
|
||||
|
||||
|
||||
def is_blank(value: str) -> bool:
|
||||
return value.strip() == ""
|
||||
|
||||
|
||||
def has_nul(value: str) -> bool:
|
||||
# Names/titles/passwords live in PostgreSQL TEXT, which cannot store U+0000.
|
||||
return "\x00" in value
|
||||
|
||||
|
||||
def has_lone_surrogate(value: str) -> bool:
|
||||
return any(0xD800 <= ord(ch) <= 0xDFFF for ch in value)
|
||||
|
||||
|
||||
def codepoint_len(value: str) -> int:
|
||||
return len(value)
|
||||
|
||||
|
||||
def parse_uuid(value: str) -> Optional[str]:
|
||||
if not isinstance(value, str) or not UUID_RE.match(value):
|
||||
return None
|
||||
try:
|
||||
return str(uuid.UUID(value))
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
|
||||
def encode_cursor(resource: str, created_at: datetime, item_id: str) -> str:
|
||||
raw = f"{resource}|{created_at.strftime('%Y-%m-%dT%H:%M:%SZ')}|{item_id}"
|
||||
return base64.urlsafe_b64encode(raw.encode("utf-8")).decode("ascii")
|
||||
|
||||
|
||||
def decode_cursor(cursor: str, expected_resource: str) -> Tuple[datetime, uuid.UUID]:
|
||||
try:
|
||||
raw = base64.urlsafe_b64decode(cursor.encode("ascii")).decode("utf-8")
|
||||
resource, ts, item_id = raw.split("|", 2)
|
||||
if resource != expected_resource:
|
||||
raise ValueError("wrong resource")
|
||||
created_at = datetime.strptime(ts, "%Y-%m-%dT%H:%M:%SZ").replace(
|
||||
tzinfo=timezone.utc
|
||||
)
|
||||
return created_at, uuid.UUID(item_id)
|
||||
except Exception as exc:
|
||||
raise ValueError("invalid cursor") from exc
|
||||
|
||||
|
||||
app = FastAPI(
|
||||
title="MTUSI Messenger lab4",
|
||||
docs_url=None,
|
||||
redoc_url=None,
|
||||
lifespan=lifespan,
|
||||
)
|
||||
|
||||
|
||||
@app.middleware("http")
|
||||
async def add_request_id_and_gate(request: Request, call_next):
|
||||
rid = request_id_from(request)
|
||||
request.state.request_id = rid
|
||||
|
||||
path = request.url.path
|
||||
if not ready and path not in ("/health/live",):
|
||||
if path == "/health/ready":
|
||||
return error_response(503, "unavailable", "Shutting down", rid)
|
||||
return error_response(503, "unavailable", "Service unavailable", rid)
|
||||
|
||||
response = await call_next(request)
|
||||
if "X-Request-Id" not in response.headers:
|
||||
response.headers["X-Request-Id"] = rid
|
||||
response.headers["X-Instance-Id"] = INSTANCE_ID
|
||||
log.info(
|
||||
"%s %s -> %s request_id=%s instance_id=%s",
|
||||
request.method,
|
||||
path,
|
||||
response.status_code,
|
||||
rid,
|
||||
INSTANCE_ID,
|
||||
)
|
||||
return response
|
||||
|
||||
|
||||
class ApiError(Exception):
|
||||
def __init__(
|
||||
self,
|
||||
status: int,
|
||||
code: str,
|
||||
message: str,
|
||||
www_authenticate: Optional[str] = None,
|
||||
headers: Optional[dict] = None,
|
||||
):
|
||||
self.status = status
|
||||
self.code = code
|
||||
self.message = message
|
||||
self.www_authenticate = www_authenticate
|
||||
self.headers = headers
|
||||
|
||||
|
||||
@app.exception_handler(ApiError)
|
||||
async def api_error_handler(request: Request, exc: ApiError):
|
||||
rid = getattr(request.state, "request_id", None) or request_id_from(request)
|
||||
return error_response(
|
||||
exc.status,
|
||||
exc.code,
|
||||
exc.message,
|
||||
rid,
|
||||
www_authenticate=exc.www_authenticate,
|
||||
extra_headers=exc.headers,
|
||||
)
|
||||
|
||||
|
||||
@app.exception_handler(RequestValidationError)
|
||||
async def validation_handler(request: Request, exc: RequestValidationError):
|
||||
rid = getattr(request.state, "request_id", None) or request_id_from(request)
|
||||
return error_response(400, "invalid_request", "Invalid request", rid)
|
||||
|
||||
|
||||
@app.exception_handler(StarletteHTTPException)
|
||||
async def http_exception_handler(request: Request, exc: StarletteHTTPException):
|
||||
rid = getattr(request.state, "request_id", None) or request_id_from(request)
|
||||
if exc.status_code == 404:
|
||||
return error_response(404, "not_found", "Not found", rid)
|
||||
if exc.status_code == 405:
|
||||
return error_response(400, "invalid_request", "Method not allowed", rid)
|
||||
return error_response(exc.status_code, "invalid_request", "Request failed", rid)
|
||||
|
||||
|
||||
@app.exception_handler(StoreUnavailable)
|
||||
async def store_unavailable_handler(request: Request, exc: StoreUnavailable):
|
||||
rid = getattr(request.state, "request_id", None) or request_id_from(request)
|
||||
return error_response(503, "unavailable", "Database unavailable", rid)
|
||||
|
||||
|
||||
@app.exception_handler(SessionStoreError)
|
||||
async def session_store_unavailable_handler(request: Request, exc: SessionStoreError):
|
||||
rid = getattr(request.state, "request_id", None) or request_id_from(request)
|
||||
return error_response(503, "unavailable", "Session store unavailable", rid)
|
||||
|
||||
|
||||
@app.exception_handler(s3.S3Unavailable)
|
||||
async def s3_unavailable_handler(request: Request, exc: s3.S3Unavailable):
|
||||
rid = getattr(request.state, "request_id", None) or request_id_from(request)
|
||||
return error_response(503, "unavailable", "Attachment storage unavailable", rid)
|
||||
|
||||
|
||||
@app.exception_handler(Exception)
|
||||
async def unhandled_handler(request: Request, exc: Exception):
|
||||
rid = getattr(request.state, "request_id", None) or request_id_from(request)
|
||||
log.exception("unhandled error request_id=%s", rid)
|
||||
return error_response(500, "internal_error", "Internal error", rid)
|
||||
|
||||
|
||||
async def read_json_object(request: Request) -> Any:
|
||||
content_type = request.headers.get("content-type", "")
|
||||
media = content_type.split(";")[0].strip().lower()
|
||||
if media and media != "application/json":
|
||||
raise ApiError(415, "unsupported_media_type", "Unsupported media type")
|
||||
try:
|
||||
body = await asyncio.wait_for(request.body(), timeout=BODY_READ_TIMEOUT_S)
|
||||
except asyncio.TimeoutError:
|
||||
raise ApiError(400, "invalid_request", "Request body timeout")
|
||||
if not body:
|
||||
raise ApiError(400, "invalid_request", "Empty body")
|
||||
try:
|
||||
text = body.decode("utf-8")
|
||||
# Reject trailing garbage after one JSON value (read to EOF).
|
||||
data, end = json.JSONDecoder().raw_decode(text)
|
||||
if text[end:].strip():
|
||||
raise ApiError(400, "invalid_request", "Malformed JSON")
|
||||
except ApiError:
|
||||
raise
|
||||
except (UnicodeDecodeError, json.JSONDecodeError, ValueError):
|
||||
raise ApiError(400, "invalid_request", "Malformed JSON")
|
||||
if not isinstance(data, dict):
|
||||
raise ApiError(400, "invalid_request", "JSON object required")
|
||||
return data
|
||||
|
||||
def require_bearer(authorization: Optional[str]) -> Tuple[str, str]:
|
||||
"""Return (user_id, token). X-User-Id is ignored."""
|
||||
bearer_challenge = 'Bearer realm="messenger"'
|
||||
if not authorization or not authorization.strip():
|
||||
raise ApiError(
|
||||
401, "unauthorized", "Missing bearer token", www_authenticate=bearer_challenge
|
||||
)
|
||||
parts = authorization.strip().split(None, 1)
|
||||
if len(parts) != 2 or parts[0].lower() != "bearer" or not parts[1].strip():
|
||||
# Basic on business endpoints must not work.
|
||||
raise ApiError(
|
||||
401, "unauthorized", "Invalid authorization", www_authenticate=bearer_challenge
|
||||
)
|
||||
token = parts[1].strip()
|
||||
try:
|
||||
session = sessions.get_session(token)
|
||||
except SessionStoreError:
|
||||
raise ApiError(503, "unavailable", "Session store unavailable")
|
||||
if session is None:
|
||||
raise ApiError(
|
||||
401, "unauthorized", "Invalid or expired session", www_authenticate=bearer_challenge
|
||||
)
|
||||
user_id = session["user_id"]
|
||||
user = store.get_user(user_id)
|
||||
if user is None:
|
||||
raise ApiError(
|
||||
401, "unauthorized", "Invalid or expired session", www_authenticate=bearer_challenge
|
||||
)
|
||||
return user_id, token
|
||||
|
||||
|
||||
def require_chat_member(chat_id_raw: str, user_id: str):
|
||||
chat_id = parse_uuid(chat_id_raw)
|
||||
if chat_id is None:
|
||||
raise ApiError(400, "invalid_request", "Invalid chat_id")
|
||||
chat = store.get_chat(chat_id)
|
||||
if chat is None or user_id not in chat.member_ids:
|
||||
raise ApiError(404, "not_found", "Chat not found")
|
||||
return chat
|
||||
|
||||
|
||||
def parse_limit(limit: Optional[int]) -> int:
|
||||
if limit is None:
|
||||
return 50
|
||||
if not isinstance(limit, int) or isinstance(limit, bool) or limit < 1 or limit > 100:
|
||||
raise ApiError(400, "invalid_request", "Invalid limit")
|
||||
return limit
|
||||
|
||||
|
||||
def validate_display_text(value: Any, field: str, max_len: int) -> str:
|
||||
if not isinstance(value, str) or is_blank(value) or has_nul(value):
|
||||
raise ApiError(400, "invalid_request", f"Invalid {field}")
|
||||
if has_lone_surrogate(value):
|
||||
raise ApiError(400, "invalid_request", f"Invalid {field}")
|
||||
length = codepoint_len(value)
|
||||
if length < 1 or length > max_len:
|
||||
raise ApiError(400, "invalid_request", f"Invalid {field}")
|
||||
return value
|
||||
|
||||
|
||||
def validate_password(value: Any) -> str:
|
||||
# Required strings must not be whitespace-only (contracts/README).
|
||||
if not isinstance(value, str) or is_blank(value) or has_nul(value):
|
||||
raise ApiError(400, "invalid_request", "Invalid password")
|
||||
if has_lone_surrogate(value):
|
||||
raise ApiError(400, "invalid_request", "Invalid password")
|
||||
length = codepoint_len(value)
|
||||
if length < PASSWORD_MIN or length > PASSWORD_MAX:
|
||||
raise ApiError(400, "invalid_request", "Invalid password")
|
||||
return value
|
||||
|
||||
|
||||
def parse_basic_credentials(authorization: Optional[str]) -> Tuple[str, str]:
|
||||
challenge = 'Basic realm="messenger", charset="UTF-8"'
|
||||
if not authorization or not authorization.strip():
|
||||
raise ApiError(
|
||||
401, "unauthorized", "Invalid credentials", www_authenticate=challenge
|
||||
)
|
||||
parts = authorization.strip().split(None, 1)
|
||||
if len(parts) != 2 or parts[0].lower() != "basic":
|
||||
raise ApiError(
|
||||
401, "unauthorized", "Invalid credentials", www_authenticate=challenge
|
||||
)
|
||||
try:
|
||||
decoded = base64.b64decode(parts[1].strip(), validate=True).decode("utf-8")
|
||||
except Exception:
|
||||
raise ApiError(
|
||||
401, "unauthorized", "Invalid credentials", www_authenticate=challenge
|
||||
)
|
||||
if ":" not in decoded:
|
||||
raise ApiError(
|
||||
401, "unauthorized", "Invalid credentials", www_authenticate=challenge
|
||||
)
|
||||
username, password = decoded.split(":", 1)
|
||||
# Username ASCII-only per contract; password may contain ':'.
|
||||
if not username or any(ord(ch) > 127 for ch in username):
|
||||
raise ApiError(
|
||||
401, "unauthorized", "Invalid credentials", www_authenticate=challenge
|
||||
)
|
||||
return username, password
|
||||
|
||||
|
||||
@app.get("/health/live")
|
||||
async def liveness(request: Request):
|
||||
rid = request.state.request_id
|
||||
return JSONResponse(content={"status": "ok"}, headers=common_headers(rid))
|
||||
|
||||
|
||||
@app.get("/health/ready")
|
||||
async def readiness(request: Request):
|
||||
rid = request.state.request_id
|
||||
if not ready:
|
||||
return error_response(503, "unavailable", "Not ready", rid)
|
||||
if lifecycle.draining:
|
||||
# Only readiness reflects the drain: other endpoints keep serving until the
|
||||
# LB has stopped routing here and the listener is closed.
|
||||
return error_response(503, "unavailable", "Shutting down", rid)
|
||||
# Mandatory synchronous path in lab4 = sessions + metadata + attachment bytes,
|
||||
# so PostgreSQL, Redis and S3 are all readiness dependencies.
|
||||
if not await run_in_threadpool(_dependencies_ok):
|
||||
return error_response(503, "unavailable", "Dependency unavailable", rid)
|
||||
return JSONResponse(content={"status": "ok"}, headers=common_headers(rid))
|
||||
|
||||
|
||||
def _dependencies_ok() -> bool:
|
||||
return db.ping() and sessions.ping() and s3.ping()
|
||||
|
||||
|
||||
@app.post("/api/v1/users")
|
||||
async def register_user(request: Request):
|
||||
rid = request.state.request_id
|
||||
data = await read_json_object(request)
|
||||
allowed = {"username", "display_name", "password"}
|
||||
if set(data.keys()) - allowed:
|
||||
raise ApiError(400, "invalid_request", "Unexpected fields")
|
||||
if "username" not in data or "display_name" not in data or "password" not in data:
|
||||
raise ApiError(400, "invalid_request", "Missing required fields")
|
||||
username = data["username"]
|
||||
display_name = data["display_name"]
|
||||
if (
|
||||
not isinstance(username, str)
|
||||
or is_blank(username)
|
||||
or has_lone_surrogate(username)
|
||||
or not USERNAME_RE.match(username)
|
||||
):
|
||||
raise ApiError(400, "invalid_request", "Invalid username")
|
||||
display_name = validate_display_text(display_name, "display_name", 100)
|
||||
password = validate_password(data["password"])
|
||||
try:
|
||||
user = store.create_user(username, display_name, password)
|
||||
except KeyError:
|
||||
raise ApiError(409, "conflict", "Username already taken")
|
||||
return JSONResponse(
|
||||
status_code=201,
|
||||
content=user.to_dict(),
|
||||
headers=common_headers(rid),
|
||||
)
|
||||
|
||||
|
||||
@app.post("/api/v1/auth/sessions")
|
||||
async def create_session(request: Request):
|
||||
rid = request.state.request_id
|
||||
challenge = 'Basic realm="messenger", charset="UTF-8"'
|
||||
# Contract: Basic only, no JSON body (even with valid credentials).
|
||||
try:
|
||||
raw_body = await asyncio.wait_for(request.body(), timeout=BODY_READ_TIMEOUT_S)
|
||||
except asyncio.TimeoutError:
|
||||
raise ApiError(400, "invalid_request", "Request body timeout")
|
||||
if raw_body and raw_body.strip():
|
||||
raise ApiError(400, "invalid_request", "Login must not include a body")
|
||||
username, password = parse_basic_credentials(
|
||||
request.headers.get("authorization")
|
||||
)
|
||||
# Uniform 401 for wrong user / wrong password (no user enumeration).
|
||||
user = store.get_user_by_username(username)
|
||||
if user is None or not store.verify_password(user.password_hash, password):
|
||||
raise ApiError(
|
||||
401, "unauthorized", "Invalid credentials", www_authenticate=challenge
|
||||
)
|
||||
try:
|
||||
session = sessions.create_session(user.id)
|
||||
except SessionStoreError:
|
||||
raise ApiError(503, "unavailable", "Session store unavailable")
|
||||
body = {
|
||||
"token": session["token"],
|
||||
"token_type": "Bearer",
|
||||
"expires_at": session["expires_at"],
|
||||
"user": user.to_dict(),
|
||||
}
|
||||
return JSONResponse(
|
||||
status_code=201, content=body, headers=common_headers(rid)
|
||||
)
|
||||
|
||||
|
||||
@app.delete("/api/v1/auth/sessions/current")
|
||||
async def logout(
|
||||
request: Request,
|
||||
authorization: Optional[str] = Header(default=None, alias="Authorization"),
|
||||
):
|
||||
rid = request.state.request_id
|
||||
_user_id, token = require_bearer(authorization)
|
||||
try:
|
||||
sessions.revoke_session(token)
|
||||
except SessionStoreError:
|
||||
raise ApiError(503, "unavailable", "Session store unavailable")
|
||||
return Response(status_code=204, headers=common_headers(rid))
|
||||
|
||||
|
||||
@app.get("/api/v1/users/me")
|
||||
async def current_user(
|
||||
request: Request,
|
||||
authorization: Optional[str] = Header(default=None, alias="Authorization"),
|
||||
):
|
||||
rid = request.state.request_id
|
||||
user_id, _token = require_bearer(authorization)
|
||||
user = store.get_user(user_id)
|
||||
assert user is not None
|
||||
return JSONResponse(content=user.to_dict(), headers=common_headers(rid))
|
||||
|
||||
|
||||
@app.post("/api/v1/chats")
|
||||
async def create_chat(
|
||||
request: Request,
|
||||
authorization: Optional[str] = Header(default=None, alias="Authorization"),
|
||||
):
|
||||
rid = request.state.request_id
|
||||
user_id, _token = require_bearer(authorization)
|
||||
data = await read_json_object(request)
|
||||
allowed = {"title", "member_ids"}
|
||||
if set(data.keys()) - allowed:
|
||||
raise ApiError(400, "invalid_request", "Unexpected fields")
|
||||
if "title" not in data or "member_ids" not in data:
|
||||
raise ApiError(400, "invalid_request", "Missing required fields")
|
||||
title = validate_display_text(data["title"], "title", 100)
|
||||
member_ids = data["member_ids"]
|
||||
if not isinstance(member_ids, list) or not member_ids or len(member_ids) > 100:
|
||||
raise ApiError(400, "invalid_request", "Invalid member_ids")
|
||||
parsed: List[str] = []
|
||||
seen = set()
|
||||
for mid in member_ids:
|
||||
if not isinstance(mid, str):
|
||||
raise ApiError(400, "invalid_request", "Invalid member_ids")
|
||||
uid = parse_uuid(mid)
|
||||
if uid is None:
|
||||
raise ApiError(400, "invalid_request", "Invalid member_ids")
|
||||
if uid in seen:
|
||||
raise ApiError(400, "invalid_request", "Duplicate member_ids")
|
||||
seen.add(uid)
|
||||
parsed.append(uid)
|
||||
if user_id not in seen:
|
||||
raise ApiError(400, "invalid_request", "Caller must be a member")
|
||||
try:
|
||||
chat = store.create_chat(title, parsed)
|
||||
except KeyError:
|
||||
raise ApiError(400, "invalid_request", "Unknown member")
|
||||
return JSONResponse(
|
||||
status_code=201,
|
||||
content=chat.to_dict(),
|
||||
headers=common_headers(rid),
|
||||
)
|
||||
|
||||
|
||||
@app.get("/api/v1/chats")
|
||||
async def list_chats(
|
||||
request: Request,
|
||||
authorization: Optional[str] = Header(default=None, alias="Authorization"),
|
||||
limit: Optional[int] = Query(default=None),
|
||||
cursor: Optional[str] = Query(default=None),
|
||||
):
|
||||
rid = request.state.request_id
|
||||
user_id, _token = require_bearer(authorization)
|
||||
lim = parse_limit(limit)
|
||||
resource = f"chats:{user_id}"
|
||||
cursor_key = None
|
||||
if cursor is not None:
|
||||
if not cursor:
|
||||
raise ApiError(400, "invalid_request", "Invalid cursor")
|
||||
try:
|
||||
cursor_ts, cursor_id = decode_cursor(cursor, resource)
|
||||
except ValueError:
|
||||
raise ApiError(400, "invalid_request", "Invalid cursor")
|
||||
if not store.chat_exists_for_user(user_id, cursor_ts, cursor_id):
|
||||
raise ApiError(400, "invalid_request", "Invalid cursor")
|
||||
cursor_key = (cursor_ts, cursor_id)
|
||||
chats, more = store.list_chats_for_user(user_id, lim, cursor_key)
|
||||
next_cursor = None
|
||||
if more and chats:
|
||||
last = chats[-1]
|
||||
next_cursor = encode_cursor(resource, last.created_at, last.id)
|
||||
return JSONResponse(
|
||||
content={"items": [c.to_dict() for c in chats], "next_cursor": next_cursor},
|
||||
headers=common_headers(rid),
|
||||
)
|
||||
|
||||
|
||||
@app.get("/api/v1/chats/{chat_id}")
|
||||
async def get_chat(
|
||||
chat_id: str,
|
||||
request: Request,
|
||||
authorization: Optional[str] = Header(default=None, alias="Authorization"),
|
||||
):
|
||||
rid = request.state.request_id
|
||||
user_id, _token = require_bearer(authorization)
|
||||
chat = require_chat_member(chat_id, user_id)
|
||||
return JSONResponse(content=chat.to_dict(), headers=common_headers(rid))
|
||||
|
||||
|
||||
def parse_attachment_ids(raw: Any) -> List[str]:
|
||||
if not isinstance(raw, list) or len(raw) > 10:
|
||||
raise ApiError(400, "invalid_request", "Invalid attachment_ids")
|
||||
parsed: List[str] = []
|
||||
for item in raw:
|
||||
aid = parse_uuid(item) if isinstance(item, str) else None
|
||||
if aid is None or aid in parsed:
|
||||
raise ApiError(400, "invalid_request", "Invalid attachment_ids")
|
||||
parsed.append(aid)
|
||||
return parsed
|
||||
|
||||
|
||||
def parse_idempotency_key(raw: Optional[str]) -> str:
|
||||
if raw is None or not raw.strip() or not IDEMPOTENCY_KEY_RE.match(raw.strip()):
|
||||
raise ApiError(400, "invalid_request", "Invalid or missing Idempotency-Key")
|
||||
return raw.strip()
|
||||
|
||||
|
||||
@app.post("/api/v1/chats/{chat_id}/messages")
|
||||
async def create_message(
|
||||
chat_id: str,
|
||||
request: Request,
|
||||
authorization: Optional[str] = Header(default=None, alias="Authorization"),
|
||||
idempotency_key: Optional[str] = Header(default=None, alias="Idempotency-Key"),
|
||||
):
|
||||
rid = request.state.request_id
|
||||
user_id, _token = require_bearer(authorization)
|
||||
chat = require_chat_member(chat_id, user_id)
|
||||
key = parse_idempotency_key(idempotency_key)
|
||||
data = await read_json_object(request)
|
||||
allowed = {"text", "attachment_ids"}
|
||||
if set(data.keys()) - allowed:
|
||||
raise ApiError(400, "invalid_request", "Unexpected fields")
|
||||
if "text" not in data:
|
||||
raise ApiError(400, "invalid_request", "Missing text")
|
||||
attachment_ids = parse_attachment_ids(data.get("attachment_ids", []))
|
||||
text = data["text"]
|
||||
# U+0000 is a valid code point of message text (stored as UTF-8 bytes, see 004 migration).
|
||||
if (
|
||||
not isinstance(text, str)
|
||||
or has_lone_surrogate(text)
|
||||
or codepoint_len(text) > 4000
|
||||
):
|
||||
raise ApiError(400, "invalid_request", "Invalid text")
|
||||
# Empty/whitespace text is allowed only together with at least one attachment.
|
||||
if not attachment_ids and is_blank(text):
|
||||
raise ApiError(400, "invalid_request", "Invalid text")
|
||||
path = f"/api/v1/chats/{chat.id}/messages"
|
||||
try:
|
||||
message, _replayed = store.create_message_idempotent(
|
||||
chat.id, user_id, text, attachment_ids, "POST", path, key
|
||||
)
|
||||
except KeyError:
|
||||
raise ApiError(409, "conflict", "Idempotency-Key reused with different body")
|
||||
except AttachmentNotFound:
|
||||
raise ApiError(404, "not_found", "Attachment not found")
|
||||
except ValueError:
|
||||
raise ApiError(400, "invalid_request", "Invalid text")
|
||||
return JSONResponse(
|
||||
status_code=201,
|
||||
content=message.to_dict(),
|
||||
headers=common_headers(rid),
|
||||
)
|
||||
|
||||
|
||||
@app.get("/api/v1/chats/{chat_id}/messages")
|
||||
async def list_messages(
|
||||
chat_id: str,
|
||||
request: Request,
|
||||
authorization: Optional[str] = Header(default=None, alias="Authorization"),
|
||||
limit: Optional[int] = Query(default=None),
|
||||
cursor: Optional[str] = Query(default=None),
|
||||
):
|
||||
rid = request.state.request_id
|
||||
user_id, _token = require_bearer(authorization)
|
||||
chat = require_chat_member(chat_id, user_id)
|
||||
lim = parse_limit(limit)
|
||||
resource = f"messages:{chat.id}"
|
||||
cursor_key = None
|
||||
if cursor is not None:
|
||||
if not cursor:
|
||||
raise ApiError(400, "invalid_request", "Invalid cursor")
|
||||
try:
|
||||
cursor_ts, cursor_id = decode_cursor(cursor, resource)
|
||||
except ValueError:
|
||||
raise ApiError(400, "invalid_request", "Invalid cursor")
|
||||
if not store.message_exists(chat.id, cursor_ts, cursor_id):
|
||||
raise ApiError(400, "invalid_request", "Invalid cursor")
|
||||
cursor_key = (cursor_ts, cursor_id)
|
||||
messages, has_more = store.list_messages(chat.id, lim, cursor_key)
|
||||
next_cursor = None
|
||||
if has_more and messages:
|
||||
last = messages[-1]
|
||||
next_cursor = encode_cursor(resource, last.created_at, last.id)
|
||||
return JSONResponse(
|
||||
content={
|
||||
"items": [m.to_dict() for m in messages],
|
||||
"next_cursor": next_cursor,
|
||||
},
|
||||
headers=common_headers(rid),
|
||||
)
|
||||
|
||||
|
||||
def parse_content_length(raw: Optional[str]) -> Optional[int]:
|
||||
if raw is None:
|
||||
return None
|
||||
if not raw.strip().isdigit():
|
||||
raise ApiError(400, "invalid_request", "Invalid Content-Length")
|
||||
return int(raw.strip())
|
||||
|
||||
|
||||
def require_attachment(attachment_id_raw: str, user_id: str) -> Attachment:
|
||||
attachment_id = parse_uuid(attachment_id_raw)
|
||||
if attachment_id is None:
|
||||
raise ApiError(400, "invalid_request", "Invalid attachment_id")
|
||||
att = store.get_attachment_for_user(attachment_id, user_id)
|
||||
if att is None:
|
||||
# Unknown and foreign attachments are indistinguishable.
|
||||
raise ApiError(404, "not_found", "Attachment not found")
|
||||
return att
|
||||
|
||||
|
||||
@app.post("/api/v1/chats/{chat_id}/attachments")
|
||||
async def upload_attachment(
|
||||
chat_id: str,
|
||||
request: Request,
|
||||
authorization: Optional[str] = Header(default=None, alias="Authorization"),
|
||||
):
|
||||
rid = request.state.request_id
|
||||
user_id, _token = require_bearer(authorization)
|
||||
# Input is validated before the chat lookup: a bad filename never reaches S3/DB queries.
|
||||
try:
|
||||
filename = attachments.validate_filename(attachments.query_filename(request.scope.get("query_string", b"")))
|
||||
content_type = attachments.declared_type(request.headers.get("content-type"))
|
||||
except attachments.UploadRejected as exc:
|
||||
raise ApiError(exc.status, exc.code, exc.message)
|
||||
chat = require_chat_member(chat_id, user_id)
|
||||
length = parse_content_length(request.headers.get("content-length"))
|
||||
if length is not None and length > config.max_upload_bytes():
|
||||
# Rejected before taking an upload slot; the body is drained (bounded), not stored.
|
||||
await attachments.drain_oversized(request.stream(), length)
|
||||
raise ApiError(413, "payload_too_large", "File exceeds 10 MiB")
|
||||
|
||||
assert upload_slots is not None
|
||||
try:
|
||||
await asyncio.wait_for(upload_slots.acquire(), timeout=UPLOAD_SLOT_WAIT_S)
|
||||
except asyncio.TimeoutError:
|
||||
raise ApiError(503, "unavailable", "Too many concurrent uploads", headers={"Retry-After": "1"})
|
||||
spooled = None
|
||||
try:
|
||||
try:
|
||||
spooled = await attachments.spool_upload(request.stream(), content_type, length)
|
||||
except attachments.UploadRejected as exc:
|
||||
raise ApiError(exc.status, exc.code, exc.message)
|
||||
except (attachments.ClientGone, ClientDisconnect):
|
||||
raise ApiError(400, "invalid_request", "Upload interrupted or timed out")
|
||||
try:
|
||||
att = await run_in_threadpool(
|
||||
attachments.persist,
|
||||
store,
|
||||
spooled,
|
||||
chat_id=chat.id,
|
||||
owner_id=user_id,
|
||||
filename=filename,
|
||||
instance=INSTANCE_ID,
|
||||
)
|
||||
except IntentLost:
|
||||
raise ApiError(503, "unavailable", "Upload expired, retry")
|
||||
finally:
|
||||
attachments.discard(spooled)
|
||||
upload_slots.release()
|
||||
return JSONResponse(
|
||||
status_code=201,
|
||||
content=att.to_dict(),
|
||||
headers=common_headers(rid, {"Location": f"/api/v1/attachments/{att.id}"}),
|
||||
)
|
||||
|
||||
|
||||
@app.get("/api/v1/attachments/{attachment_id}")
|
||||
async def get_attachment(
|
||||
attachment_id: str,
|
||||
request: Request,
|
||||
authorization: Optional[str] = Header(default=None, alias="Authorization"),
|
||||
):
|
||||
rid = request.state.request_id
|
||||
user_id, _token = require_bearer(authorization)
|
||||
att = require_attachment(attachment_id, user_id)
|
||||
return JSONResponse(content=att.to_dict(), headers=common_headers(rid))
|
||||
|
||||
|
||||
@app.get("/api/v1/attachments/{attachment_id}/content")
|
||||
async def download_attachment(
|
||||
attachment_id: str,
|
||||
request: Request,
|
||||
authorization: Optional[str] = Header(default=None, alias="Authorization"),
|
||||
variant: Optional[str] = Query(default=None),
|
||||
):
|
||||
rid = request.state.request_id
|
||||
user_id, _token = require_bearer(authorization)
|
||||
att = require_attachment(attachment_id, user_id)
|
||||
if variant is not None and variant != "original":
|
||||
if variant in VARIANT_NAMES:
|
||||
raise ApiError(404, "not_found", "Variant not found")
|
||||
raise ApiError(400, "invalid_request", "Invalid variant")
|
||||
try:
|
||||
length, chunks, body = await run_in_threadpool(s3.open_object, att.object_key)
|
||||
except s3.S3ObjectMissing:
|
||||
log.error("original missing in S3 attachment_id=%s request_id=%s", att.id, rid)
|
||||
raise ApiError(503, "unavailable", "Attachment content unavailable")
|
||||
if length != att.size_bytes:
|
||||
body.close()
|
||||
log.error("original size mismatch attachment_id=%s request_id=%s", att.id, rid)
|
||||
raise ApiError(503, "unavailable", "Attachment content unavailable")
|
||||
|
||||
def stream():
|
||||
try:
|
||||
yield from chunks
|
||||
finally:
|
||||
body.close()
|
||||
|
||||
headers = common_headers(
|
||||
rid,
|
||||
{
|
||||
"Content-Length": str(att.size_bytes),
|
||||
"ETag": f'"{att.sha256}"',
|
||||
"Cache-Control": "private, no-store",
|
||||
"X-Content-Type-Options": "nosniff",
|
||||
"Content-Disposition": "inline; filename*=UTF-8''" + urllib.parse.quote(att.filename, safe=""),
|
||||
},
|
||||
)
|
||||
return StreamingResponse(stream(), media_type=att.content_type, headers=headers)
|
||||
|
||||
|
||||
def create_app() -> FastAPI:
|
||||
return app
|
||||
Reference in New Issue
Block a user