Files
mtusi2026lab6/app.py
T
2026-11-19 16:39:32 +03:00

1183 lines
42 KiB
Python

import os
import time
import base64
import secrets
import uuid
import json
import re
import struct
import hashlib
import hmac
import logging
from datetime import datetime, timezone, timedelta
from typing import List, Optional
from uuid import UUID
import psycopg2
import psycopg2.extras
psycopg2.extras.register_uuid()
import bcrypt
from fastapi import FastAPI, Header, HTTPException, Query, Response, Request
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
from pydantic import BaseModel, Field, field_validator, model_validator, ConfigDict
import redis
import boto3
from botocore.client import Config
import threading
from opentelemetry import metrics, trace
from opentelemetry._logs import set_logger_provider
from opentelemetry.exporter.otlp.proto.http._log_exporter import OTLPLogExporter
from opentelemetry.exporter.otlp.proto.http.metric_exporter import OTLPMetricExporter
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler
from opentelemetry.sdk._logs.export import BatchLogRecordProcessor
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
class JsonLogFormatter(logging.Formatter):
def format(self, record: logging.LogRecord) -> str:
span = trace.get_current_span()
ctx = span.get_span_context() if span else None
trace_id = format(ctx.trace_id, "032x") if ctx and ctx.is_valid else None
span_id = format(ctx.span_id, "016x") if ctx and ctx.is_valid else None
payload = {
"ts": datetime.now(timezone.utc).isoformat().replace("+00:00", "Z"),
"level": record.levelname,
"logger": record.name,
"service": os.getenv("OTEL_SERVICE_NAME", "messenger-api"),
"msg": record.getMessage(),
}
if trace_id and span_id:
payload["trace_id"] = trace_id
payload["span_id"] = span_id
for attr in ("method", "path", "status_code", "job_id", "attachment_id", "job_status", "attempt", "error_code"):
if hasattr(record, attr):
payload[attr] = getattr(record, attr)
if record.exc_info:
payload["exception"] = record.exc_info[0].__name__ if record.exc_info[0] else "Exception"
return json.dumps(payload, ensure_ascii=False)
def setup_api_telemetry():
service_name = os.getenv("OTEL_SERVICE_NAME", "messenger-api")
endpoint = os.getenv("OTEL_EXPORTER_OTLP_ENDPOINT", "http://otel-collector:4318").rstrip("/")
env_attrs = os.getenv("OTEL_RESOURCE_ATTRIBUTES", "deployment.environment.name=lab")
attrs = {"service.name": service_name}
for part in env_attrs.split(","):
if "=" in part:
k, v = part.split("=", 1)
attrs[k.strip()] = v.strip()
resource = Resource.create(attrs)
tracer_provider = TracerProvider(resource=resource)
tracer_provider.add_span_processor(
BatchSpanProcessor(OTLPSpanExporter(endpoint=f"{endpoint}/v1/traces"))
)
trace.set_tracer_provider(tracer_provider)
metric_reader = PeriodicExportingMetricReader(
OTLPMetricExporter(endpoint=f"{endpoint}/v1/metrics"),
export_interval_millis=5000,
)
meter_provider = MeterProvider(resource=resource, metric_readers=[metric_reader])
metrics.set_meter_provider(meter_provider)
logger_provider = LoggerProvider(resource=resource)
logger_provider.add_log_record_processor(
BatchLogRecordProcessor(OTLPLogExporter(endpoint=f"{endpoint}/v1/logs"))
)
set_logger_provider(logger_provider)
root_logger = logging.getLogger()
root_logger.setLevel(logging.INFO)
root_logger.handlers.clear()
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(JsonLogFormatter())
root_logger.addHandler(stream_handler)
otel_handler = LoggingHandler(level=logging.INFO, logger_provider=logger_provider)
otel_handler.setFormatter(JsonLogFormatter())
root_logger.addHandler(otel_handler)
return tracer_provider, trace.get_tracer(service_name), metrics.get_meter(service_name)
app = FastAPI(title="MTUSI Messenger — lab6", version="1.6.0")
_tracer_provider, tracer, meter = setup_api_telemetry()
FastAPIInstrumentor.instrument_app(app, tracer_provider=_tracer_provider)
api_logger = logging.getLogger("messenger-api")
api_requests_counter = meter.create_counter(
name="http_server_requests_total",
description="Total HTTP requests served by API",
unit="1",
)
api_errors_counter = meter.create_counter(
name="http_server_errors_total",
description="Total HTTP 4xx/5xx responses served by API",
unit="1",
)
api_duration_histogram = meter.create_histogram(
name="http_server_request_duration_seconds",
description="HTTP request duration in seconds",
unit="s",
)
DATABASE_URL = os.getenv("DATABASE_URL", "postgresql://postgres:postgres@postgres:5432/messenger")
SESSION_TTL_SECONDS = int(os.getenv("SESSION_TTL_SECONDS", "3600"))
INSTANCE_ID = os.getenv("INSTANCE_ID", "instance-1")
REDIS_URL = os.getenv("REDIS_URL", "redis://redis:6379/0")
redis_client = redis.Redis.from_url(REDIS_URL, decode_responses=True)
SECRET_HMAC_KEY = os.environ["SECRET_HMAC_KEY"]
S3_ENDPOINT = os.getenv("S3_ENDPOINT", "http://s3:8333")
S3_BUCKET = os.getenv("S3_BUCKET", "messenger-attachments")
S3_ACCESS_KEY = os.getenv("S3_ACCESS_KEY_ID", "appuser")
S3_SECRET_KEY = os.getenv("S3_SECRET_ACCESS_KEY", "appsecretpassword")
STREAM_NAME = os.getenv("BROKER_STREAM", "attachment_jobs")
s3_client = boto3.client(
"s3",
endpoint_url=S3_ENDPOINT,
aws_access_key_id=S3_ACCESS_KEY,
aws_secret_access_key=S3_SECRET_KEY,
config=Config(signature_version="s3v4"),
region_name="us-east-1"
)
def get_db_connection():
return psycopg2.connect(DATABASE_URL, cursor_factory=psycopg2.extras.RealDictCursor)
def outbox_publisher():
while True:
conn = None
try:
conn = get_db_connection()
with conn.cursor() as cur:
cur.execute("""
SELECT id, stream, payload FROM outbox
WHERE published_at IS NULL
ORDER BY created_at
LIMIT 100
FOR UPDATE SKIP LOCKED
""")
for row in cur.fetchall():
redis_client.xadd(row["stream"], {"payload": json.dumps(row["payload"])})
cur.execute("UPDATE outbox SET published_at=NOW() WHERE id=%s", (row["id"],))
conn.commit()
except Exception as e:
api_logger.error("Outbox publisher error: %s", type(e).__name__)
finally:
if conn is not None and not conn.closed:
conn.close()
time.sleep(1)
@app.on_event("startup")
def _start_outbox_publisher():
threading.Thread(target=outbox_publisher, daemon=True).start()
@app.middleware("http")
async def telemetry_middleware(request: Request, call_next):
start = time.monotonic()
route_obj = request.scope.get("route")
route_path = getattr(route_obj, "path", request.url.path)
method = request.method
status_code = 500
try:
response = await call_next(request)
status_code = response.status_code
return response
finally:
elapsed = time.monotonic() - start
low_card_attrs = {
"http.method": method,
"http.route": route_path,
"http.status_code": status_code,
}
api_requests_counter.add(1, low_card_attrs)
api_duration_histogram.record(elapsed, low_card_attrs)
if status_code >= 400:
api_errors_counter.add(1, low_card_attrs)
api_logger.info(
"HTTP request completed",
extra={
"method": method,
"path": route_path,
"status_code": status_code,
},
)
def generate_request_id() -> str:
return uuid.uuid4().hex
def make_error_response(
status_code: int,
code: str,
message: str,
req_id: str,
extra_headers: Optional[dict] = None,
) -> JSONResponse:
headers = {"X-Request-Id": req_id, "X-Instance-Id": INSTANCE_ID}
if extra_headers:
headers.update(extra_headers)
return JSONResponse(
status_code=status_code,
headers=headers,
content={
"error": {
"code": code,
"message": message,
"request_id": req_id,
}
},
)
def encode_cursor(chat_id: str, created_at: str, msg_id: str) -> str:
payload = f"{chat_id}|{created_at}|{msg_id}"
signature = hmac.new(SECRET_HMAC_KEY.encode(), payload.encode(), hashlib.sha256).hexdigest()[:16]
raw = f"{payload}|{signature}"
return base64.urlsafe_b64encode(raw.encode()).decode()
def decode_cursor(cursor: str, expected_chat_id: str) -> tuple:
try:
decoded = base64.urlsafe_b64decode(cursor.encode()).decode()
parts = decoded.split("|")
if len(parts) != 4:
raise ValueError()
chat_id, created_at, msg_id, signature = parts
if chat_id != expected_chat_id:
raise HTTPException(status_code=400, detail="Cursor belongs to another chat")
expected_sig = hmac.new(
SECRET_HMAC_KEY.encode(),
f"{chat_id}|{created_at}|{msg_id}".encode(),
hashlib.sha256
).hexdigest()[:16]
if not hmac.compare_digest(signature, expected_sig):
raise ValueError()
return created_at, msg_id
except HTTPException:
raise
except Exception:
raise HTTPException(status_code=400, detail="Invalid cursor format")
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
req_id = request.headers.get("X-Request-Id") or generate_request_id()
return make_error_response(
status_code=400,
code="invalid_request",
message="Validation error in request body or parameters",
req_id=req_id
)
@app.exception_handler(HTTPException)
async def http_exception_handler(request: Request, exc: HTTPException):
req_id = request.headers.get("X-Request-Id") or generate_request_id()
headers = {"X-Request-Id": req_id, "X-Instance-Id": INSTANCE_ID}
if exc.headers:
headers.update(exc.headers)
detail = exc.detail
if isinstance(detail, dict) and "error" in detail:
detail["error"]["request_id"] = req_id
return JSONResponse(status_code=exc.status_code, headers=headers, content=detail)
code_map = {
400: "invalid_request",
401: "unauthorized",
404: "not_found",
409: "conflict",
413: "payload_too_large",
415: "unsupported_media_type",
429: "rate_limited",
503: "unavailable",
500: "internal_error",
}
return JSONResponse(
status_code=exc.status_code,
headers=headers,
content={
"error": {
"code": code_map.get(exc.status_code, "internal_error"),
"message": str(detail),
"request_id": req_id,
}
},
)
@app.exception_handler(Exception)
async def unhandled_exception_handler(request: Request, exc: Exception):
req_id = request.headers.get("X-Request-Id") or generate_request_id()
return make_error_response(
status_code=500,
code="internal_error",
message="Internal server error",
req_id=req_id,
)
class CreateUser(BaseModel):
model_config = ConfigDict(extra='forbid')
username: str = Field(..., pattern=r"^[a-z0-9_]{3,32}$")
display_name: str = Field(..., min_length=1, max_length=100)
password: str = Field(..., min_length=12, max_length=128)
@field_validator("display_name", "password")
def not_blank(cls, v):
if not v.strip():
raise ValueError("Must not be blank")
return v
class CreateChat(BaseModel):
model_config = ConfigDict(extra='forbid')
title: str = Field(..., min_length=1, max_length=100)
member_ids: List[uuid.UUID] = Field(..., min_length=1, max_length=100)
@field_validator("title")
def not_blank(cls, v):
if not v.strip():
raise ValueError("Must not be blank")
return v
class CreateMessage(BaseModel):
model_config = ConfigDict(extra='forbid')
text: str = Field(default="", max_length=4000)
attachment_ids: List[uuid.UUID] = Field(default=[], max_length=10)
@model_validator(mode="after")
def check_content(self) -> 'CreateMessage':
if not self.text.strip() and not self.attachment_ids:
raise ValueError("Message must contain either text or attachments")
return self
@app.get("/health/live")
def health_live(response: Response):
req_id = generate_request_id()
response.headers["X-Request-Id"] = req_id
response.headers["X-Instance-Id"] = INSTANCE_ID
return {"status": "ok"}
@app.get("/health/ready")
def health_ready(response: Response):
req_id = generate_request_id()
response.headers["X-Request-Id"] = req_id
response.headers["X-Instance-Id"] = INSTANCE_ID
try:
conn = get_db_connection()
conn.close()
redis_client.ping()
s3_client.head_bucket(Bucket=S3_BUCKET)
except Exception:
raise HTTPException(status_code=503, detail="Database, Redis or S3 unavailable")
return {"status": "ok"}
@app.post("/api/v1/users", status_code=201)
def register_user(body: CreateUser, response: Response):
req_id = generate_request_id()
response.headers["X-Request-Id"] = req_id
response.headers["X-Instance-Id"] = INSTANCE_ID
password_hash = bcrypt.hashpw(body.password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
user_id = str(uuid.uuid4())
created_at = datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")
conn = get_db_connection()
try:
with conn.cursor() as cur:
cur.execute(
"INSERT INTO users (id, username, display_name, password_hash, created_at) VALUES (%s, %s, %s, %s, %s)",
(user_id, body.username, body.display_name, password_hash, created_at)
)
conn.commit()
except psycopg2.errors.UniqueViolation:
conn.rollback()
raise HTTPException(
status_code=409,
detail={"error": {"code": "conflict", "message": "Username already taken", "request_id": req_id}}
)
finally:
conn.close()
return {
"id": user_id,
"username": body.username,
"display_name": body.display_name,
"created_at": created_at
}
@app.post("/api/v1/auth/sessions", status_code=201)
def create_session(authorization: Optional[str] = Header(None), response: Response = None):
req_id = generate_request_id()
response.headers["X-Request-Id"] = req_id
response.headers["X-Instance-Id"] = INSTANCE_ID
if not authorization or not authorization.startswith("Basic "):
raise HTTPException(
status_code=401,
detail="Missing or invalid basic auth",
headers={"WWW-Authenticate": 'Basic realm="messenger", charset="UTF-8"'}
)
try:
encoded = authorization.split(" ")[1]
decoded = base64.b64decode(encoded).decode("utf-8")
username, password = decoded.split(":", 1)
except Exception:
raise HTTPException(
status_code=401,
detail="Invalid credentials format",
headers={"WWW-Authenticate": 'Basic realm="messenger", charset="UTF-8"'}
)
conn = get_db_connection()
with conn.cursor() as cur:
cur.execute("SELECT * FROM users WHERE username = %s", (username,))
user = cur.fetchone()
conn.close()
if not user or not bcrypt.checkpw(password.encode('utf-8'), user['password_hash'].encode('utf-8')):
raise HTTPException(
status_code=401,
detail="Invalid credentials",
headers={"WWW-Authenticate": 'Basic realm="messenger", charset="UTF-8"'}
)
token = secrets.token_urlsafe(48)
redis_client.setex(f"session:{token}", SESSION_TTL_SECONDS, str(user['id']))
expires_at = datetime.now(timezone.utc) + timedelta(seconds=SESSION_TTL_SECONDS)
expires_at_str = expires_at.isoformat().replace("+00:00", "Z")
return {
"token": token,
"token_type": "Bearer",
"expires_at": expires_at_str,
"user": {
"id": str(user['id']),
"username": user['username'],
"display_name": user['display_name'],
"created_at": user['created_at'].isoformat().replace("+00:00", "Z") if isinstance(user['created_at'], datetime) else user['created_at']
}
}
def get_current_user_from_token(authorization: Optional[str] = Header(None)) -> dict:
if not authorization or not authorization.startswith("Bearer "):
raise HTTPException(
status_code=401,
detail="Missing or invalid bearer token",
headers={"WWW-Authenticate": 'Bearer realm="messenger"'}
)
token = authorization.split(" ")[1]
try:
user_id = redis_client.get(f"session:{token}")
except Exception:
raise HTTPException(status_code=503, detail="Session store unavailable")
if not user_id:
raise HTTPException(
status_code=401,
detail="Invalid or expired session token",
headers={"WWW-Authenticate": 'Bearer realm="messenger"'}
)
conn = get_db_connection()
with conn.cursor() as cur:
cur.execute("SELECT * FROM users WHERE id = %s", (user_id,))
row = cur.fetchone()
conn.close()
if not row:
raise HTTPException(
status_code=401,
detail="User not found",
headers={"WWW-Authenticate": 'Bearer realm="messenger"'}
)
return {
"id": str(row['id']),
"username": row['username'],
"display_name": row['display_name'],
"created_at": row['created_at'].isoformat().replace("+00:00", "Z") if isinstance(row['created_at'], datetime) else row['created_at']
}
@app.delete("/api/v1/auth/sessions/current", status_code=204)
def revoke_session(authorization: Optional[str] = Header(None)):
req_id = generate_request_id()
if not authorization or not authorization.startswith("Bearer "):
raise HTTPException(
status_code=401,
detail="Unauthorized",
headers={"WWW-Authenticate": 'Bearer realm="messenger"'}
)
token = authorization.split(" ")[1]
deleted = redis_client.delete(f"session:{token}")
if not deleted:
raise HTTPException(
status_code=401,
detail="Invalid session token",
headers={"WWW-Authenticate": 'Bearer realm="messenger"'}
)
return Response(
status_code=204,
headers={
"X-Request-Id": req_id,
"X-Instance-Id": INSTANCE_ID
},
)
@app.get("/api/v1/users/me")
def current_user(response: Response, authorization: Optional[str] = Header(None)):
req_id = generate_request_id()
response.headers["X-Request-Id"] = req_id
response.headers["X-Instance-Id"] = INSTANCE_ID
return get_current_user_from_token(authorization)
@app.post("/api/v1/chats", status_code=201)
def create_chat(body: CreateChat, response: Response, authorization: Optional[str] = Header(None)):
req_id = generate_request_id()
response.headers["X-Request-Id"] = req_id
response.headers["X-Instance-Id"] = INSTANCE_ID
user = get_current_user_from_token(authorization)
member_ids_str = [str(mid) for mid in body.member_ids]
if user["id"] not in member_ids_str:
raise HTTPException(status_code=400, detail="Caller must be in member_ids")
if len(member_ids_str) != len(set(member_ids_str)):
raise HTTPException(status_code=400, detail="Duplicate member IDs")
conn = get_db_connection()
with conn.cursor() as cur:
for mid_str in member_ids_str:
cur.execute("SELECT id FROM users WHERE id = %s", (mid_str,))
if not cur.fetchone():
conn.close()
raise HTTPException(status_code=400, detail=f"User {mid_str} not found")
chat_id = str(uuid.uuid4())
created_at = datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")
try:
with conn.cursor() as cur:
cur.execute(
"INSERT INTO chats (id, title, created_at) VALUES (%s, %s, %s)",
(chat_id, body.title, created_at)
)
for mid_str in member_ids_str:
cur.execute(
"INSERT INTO chat_members (chat_id, user_id) VALUES (%s, %s)",
(chat_id, mid_str)
)
conn.commit()
except Exception:
conn.rollback()
conn.close()
raise HTTPException(status_code=500, detail="Internal error creating chat")
conn.close()
return {
"id": chat_id,
"title": body.title,
"member_ids": member_ids_str,
"created_at": created_at
}
@app.get("/api/v1/chats")
def list_chats(limit: int = 50, cursor: Optional[str] = None, response: Response = None, authorization: Optional[str] = Header(None)):
req_id = generate_request_id()
response.headers["X-Request-Id"] = req_id
response.headers["X-Instance-Id"] = INSTANCE_ID
user = get_current_user_from_token(authorization)
if limit < 1 or limit > 100:
raise HTTPException(status_code=400, detail="Invalid limit")
conn = get_db_connection()
with conn.cursor() as cur:
cur.execute("""
SELECT c.id, c.title, c.created_at FROM chats c
JOIN chat_members cm ON c.id = cm.chat_id
WHERE cm.user_id = %s
ORDER BY c.created_at ASC, c.id ASC
""", (user["id"],))
rows = cur.fetchall()
user_chats = []
for r in rows:
cur.execute("SELECT user_id FROM chat_members WHERE chat_id = %s", (r['id'],))
m_rows = cur.fetchall()
user_chats.append({
"id": str(r['id']),
"title": r['title'],
"member_ids": [str(m['user_id']) for m in m_rows],
"created_at": r['created_at'].isoformat().replace("+00:00", "Z") if isinstance(r['created_at'], datetime) else r['created_at']
})
conn.close()
start_idx = 0
if cursor:
try:
parts = cursor.split("_")
c_time, c_id = parts[0], parts[1]
found = False
for idx, c in enumerate(user_chats):
if (c["created_at"], c["id"]) >= (c_time, c_id):
start_idx = idx if (c["created_at"], c["id"]) > (c_time, c_id) else idx + 1
found = True
break
if not found:
start_idx = len(user_chats)
except Exception:
raise HTTPException(status_code=400, detail="Invalid cursor format")
page_items = user_chats[start_idx:start_idx + limit]
next_cursor = None
if start_idx + limit < len(user_chats):
last = page_items[-1]
next_cursor = f"{last['created_at']}_{last['id']}"
return {"items": page_items, "next_cursor": next_cursor}
@app.get("/api/v1/chats/{chat_id}")
def get_chat(chat_id: UUID, response: Response, authorization: Optional[str] = Header(None)):
req_id = generate_request_id()
response.headers["X-Request-Id"] = req_id
response.headers["X-Instance-Id"] = INSTANCE_ID
user = get_current_user_from_token(authorization)
chat_id_str = str(chat_id)
conn = get_db_connection()
with conn.cursor() as cur:
cur.execute("""
SELECT c.* FROM chats c
JOIN chat_members cm ON c.id = cm.chat_id
WHERE c.id = %s AND cm.user_id = %s
""", (chat_id_str, user["id"]))
chat = cur.fetchone()
if chat:
cur.execute("SELECT user_id FROM chat_members WHERE chat_id = %s", (chat_id_str,))
members = cur.fetchall()
conn.close()
if not chat:
raise HTTPException(status_code=404, detail="Chat not found")
return {
"id": str(chat['id']),
"title": chat['title'],
"member_ids": [str(m['user_id']) for m in members],
"created_at": chat['created_at'].isoformat().replace("+00:00", "Z") if isinstance(chat['created_at'], datetime) else chat['created_at']
}
def validate_image_and_get_dimensions(data: bytes, content_type: str):
if len(data) > 10 * 1024 * 1024:
raise HTTPException(status_code=413, detail="Payload too large (>10 MiB)")
width, height = 0, 0
if content_type == "image/png":
if not data.startswith(b'\x89PNG\r\n\x1a\n'):
raise HTTPException(status_code=415, detail="Invalid PNG signature")
if len(data) < 33:
raise HTTPException(status_code=415, detail="Truncated PNG")
if data[12:16] != b'IHDR':
raise HTTPException(status_code=415, detail="PNG missing IHDR chunk")
ihdr_len = struct.unpack('>I', data[8:12])[0]
if ihdr_len != 13:
raise HTTPException(status_code=415, detail="Invalid IHDR length")
width, height = struct.unpack('>II', data[16:24])
elif content_type == "image/jpeg":
if not data.startswith(b'\xff\xd8'):
raise HTTPException(status_code=415, detail="Invalid JPEG signature")
i = 2
while i < len(data) - 9:
marker = data[i]
if marker == 0xff:
marker_type = data[i + 1]
if 0xc0 <= marker_type <= 0xc3:
height, width = struct.unpack('>HH', data[i + 5:i + 9])
break
if marker_type in {0xd8, 0xd9} or (0xd0 <= marker_type <= 0xd7):
i += 2
continue
seg_len = struct.unpack('>H', data[i + 2:i + 4])[0]
if seg_len < 2:
raise HTTPException(status_code=415, detail="Invalid JPEG segment")
i += 2 + seg_len
else:
i += 1
else:
raise HTTPException(status_code=415, detail="Unsupported media type")
if width == 0 or height == 0:
raise HTTPException(status_code=415, detail="Could not read image dimensions")
if width * height > 40_000_000:
raise HTTPException(status_code=413, detail="Image exceeds 40 million pixels limit")
@app.post("/api/v1/chats/{chat_id}/attachments", status_code=202)
async def upload_attachment(
chat_id: UUID,
filename: str = Query(..., min_length=1, max_length=255),
request: Request = None,
response: Response = None,
authorization: Optional[str] = Header(None)
):
req_id = generate_request_id()
response.headers["X-Request-Id"] = req_id
response.headers["X-Instance-Id"] = INSTANCE_ID
user = get_current_user_from_token(authorization)
chat_id_str = str(chat_id)
conn = get_db_connection()
with conn.cursor() as cur:
cur.execute(
"SELECT 1 FROM chat_members WHERE chat_id = %s AND user_id = %s",
(chat_id_str, user["id"])
)
if not cur.fetchone():
conn.close()
raise HTTPException(status_code=404, detail="Chat not found or inaccessible")
conn.close()
if any(ord(c) < 0x20 or ord(c) == 0x7f for c in filename) or '/' in filename or '\\' in filename:
raise HTTPException(status_code=400, detail="Invalid filename")
content_type = request.headers.get("content-type")
if content_type not in ["image/png", "image/jpeg"]:
raise HTTPException(status_code=415, detail="Unsupported media type")
MAX_UPLOAD = 10 * 1024 * 1024
cl = request.headers.get("content-length")
if cl is not None:
try:
if int(cl) > MAX_UPLOAD:
raise HTTPException(status_code=413, detail="Payload too large (>10 MiB)")
except ValueError:
raise HTTPException(status_code=400, detail="Invalid Content-Length")
chunks = []
total = 0
async for chunk in request.stream():
total += len(chunk)
if total > MAX_UPLOAD:
raise HTTPException(status_code=413, detail="Payload too large (>10 MiB)")
chunks.append(chunk)
body_bytes = b"".join(chunks)
validate_image_and_get_dimensions(body_bytes, content_type)
size_bytes = len(body_bytes)
sha256_hash = hashlib.sha256(body_bytes).hexdigest()
attachment_id = str(uuid.uuid4())
job_id = str(uuid.uuid4())
now = datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")
staging_key = f"staging/{chat_id_str}/{attachment_id}/original"
try:
s3_client.put_object(
Bucket=S3_BUCKET,
Key=staging_key,
Body=body_bytes,
ContentType=content_type
)
except Exception:
raise HTTPException(status_code=503, detail="S3 storage unavailable")
job_payload = {
"schema_version": 1,
"job_id": job_id,
"type": "attachment.process.v1",
"attachment_id": attachment_id,
"pipeline_version": 1,
"attempt": 1,
"created_at": now
}
conn = get_db_connection()
try:
with conn.cursor() as cur:
cur.execute("""
INSERT INTO attachments (
id, chat_id, owner_id, filename, content_type,
size_bytes, sha256, status, variants, error_code,
pipeline_version, attempt, created_at, updated_at
)
VALUES (%s, %s, %s, %s, %s, %s, %s, 'queued', '[]', NULL, 1, 0, %s, %s)
""", (attachment_id, chat_id_str, user["id"], filename,
content_type, size_bytes, sha256_hash, now, now))
cur.execute(
"INSERT INTO outbox (id, stream, payload, created_at) "
"VALUES (%s, %s, %s::jsonb, %s)",
(str(uuid.uuid4()), STREAM_NAME, json.dumps(job_payload), now)
)
conn.commit()
except Exception:
conn.rollback()
raise HTTPException(status_code=500, detail="Internal error saving attachment")
finally:
if not conn.closed:
conn.close()
return {
"id": attachment_id,
"chat_id": chat_id_str,
"owner_id": user["id"],
"filename": filename,
"content_type": content_type,
"size_bytes": size_bytes,
"sha256": sha256_hash,
"status": "queued",
"variants": [],
"error_code": None,
"created_at": now,
"updated_at": now
}
@app.get("/api/v1/attachments/{attachment_id}")
def get_attachment(attachment_id: UUID, response: Response, authorization: Optional[str] = Header(None)):
req_id = generate_request_id()
response.headers["X-Request-Id"] = req_id
response.headers["X-Instance-Id"] = INSTANCE_ID
user = get_current_user_from_token(authorization)
att_id_str = str(attachment_id)
conn = get_db_connection()
with conn.cursor() as cur:
cur.execute("""
SELECT a.* FROM attachments a
JOIN chat_members cm ON a.chat_id = cm.chat_id
WHERE a.id = %s AND cm.user_id = %s
""", (att_id_str, user["id"]))
row = cur.fetchone()
conn.close()
if not row:
raise HTTPException(status_code=404, detail="Attachment not found or inaccessible")
return {
"id": str(row['id']),
"chat_id": str(row['chat_id']),
"owner_id": str(row['owner_id']),
"filename": row['filename'],
"content_type": row['content_type'],
"size_bytes": row['size_bytes'],
"sha256": row['sha256'],
"status": row['status'],
"variants": row['variants'],
"error_code": row['error_code'],
"created_at": row['created_at'].isoformat().replace("+00:00", "Z") if isinstance(row['created_at'], datetime) else row['created_at'],
"updated_at": row['updated_at'].isoformat().replace("+00:00", "Z") if isinstance(row['updated_at'], datetime) else row['updated_at']
}
@app.get("/api/v1/attachments/{attachment_id}/content")
def download_attachment(
attachment_id: UUID,
variant: str = Query("original", pattern="^(original|thumbnail|crop|optimized)$"),
response: Response = None,
authorization: Optional[str] = Header(None)
):
req_id = generate_request_id()
response.headers["X-Request-Id"] = req_id
response.headers["X-Instance-Id"] = INSTANCE_ID
user = get_current_user_from_token(authorization)
att_id_str = str(attachment_id)
conn = get_db_connection()
with conn.cursor() as cur:
cur.execute("""
SELECT a.* FROM attachments a
JOIN chat_members cm ON a.chat_id = cm.chat_id
WHERE a.id = %s AND cm.user_id = %s
""", (att_id_str, user["id"]))
row = cur.fetchone()
conn.close()
if not row:
raise HTTPException(status_code=404, detail="Attachment not found or inaccessible")
if row['status'] != 'ready':
raise HTTPException(status_code=409, detail=f"Attachment is in status '{row['status']}'")
if variant == "original":
s3_key = f"attachments/{row['chat_id']}/{row['id']}/original"
media_type = row['content_type']
else:
variants_list = row['variants'] or []
matched = [v for v in variants_list if v.get("name") == variant]
if not matched:
raise HTTPException(status_code=404, detail=f"Variant '{variant}' not found")
s3_key = f"attachments/{row['chat_id']}/{row['id']}/{variant}"
media_type = matched[0]["content_type"]
try:
obj = s3_client.get_object(Bucket=S3_BUCKET, Key=s3_key)
data = obj['Body'].read()
except Exception:
raise HTTPException(status_code=503, detail="S3 storage unavailable or object missing")
return Response(
content=data,
media_type=media_type,
headers={
"X-Request-Id": req_id,
"X-Instance-Id": INSTANCE_ID,
},
)
@app.post("/api/v1/chats/{chat_id}/messages", status_code=201)
def create_message(
chat_id: UUID,
body: CreateMessage,
response: Response,
authorization: Optional[str] = Header(None),
idempotency_key: Optional[str] = Header(None, alias="Idempotency-Key")
):
req_id = generate_request_id()
response.headers["X-Request-Id"] = req_id
response.headers["X-Instance-Id"] = INSTANCE_ID
user = get_current_user_from_token(authorization)
if not idempotency_key or not re.match(r"^[A-Za-z0-9._:-]{1,128}$", idempotency_key):
raise HTTPException(status_code=400, detail="Missing or invalid Idempotency-Key")
chat_id_str = str(chat_id)
method = "POST"
path = f"/api/v1/chats/{chat_id_str}/messages"
body_json_str = json.dumps(body.model_dump(mode='json'), sort_keys=True)
request_hash = hashlib.sha256(body_json_str.encode("utf-8")).hexdigest()
conn = get_db_connection()
try:
with conn.cursor() as cur:
cur.execute(
"SELECT 1 FROM chat_members WHERE chat_id = %s AND user_id = %s",
(chat_id_str, user["id"])
)
if not cur.fetchone():
conn.close()
raise HTTPException(status_code=404, detail="Chat not found")
attachment_ids_str = [str(aid) for aid in body.attachment_ids]
if attachment_ids_str:
cur.execute("""
SELECT id, status FROM attachments
WHERE id = ANY(%s::uuid[]) AND chat_id = %s
""", (attachment_ids_str, chat_id_str))
found_atts = cur.fetchall()
if len(found_atts) != len(attachment_ids_str):
conn.close()
raise HTTPException(status_code=404, detail="One or more attachments not found or in another chat")
for att in found_atts:
if att['status'] == 'failed':
conn.close()
raise HTTPException(status_code=409, detail="Cannot attach failed attachment")
msg_id = str(uuid.uuid4())
created_at = datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")
msg_response = {
"id": msg_id,
"chat_id": chat_id_str,
"sender_id": user["id"],
"text": body.text,
"created_at": created_at,
"attachment_ids": attachment_ids_str
}
cur.execute("""
INSERT INTO idempotency_keys (user_id, method, path, key, request_hash, response_status, response_body)
VALUES (%s, %s, %s, %s, %s, %s, %s)
ON CONFLICT (user_id, method, path, key) DO NOTHING
""", (user["id"], method, path, idempotency_key, request_hash, 201, json.dumps(msg_response)))
if cur.rowcount == 0:
cur.execute("""
SELECT request_hash, response_status, response_body
FROM idempotency_keys
WHERE user_id = %s AND method = %s AND path = %s AND key = %s
""", (user["id"], method, path, idempotency_key))
row = cur.fetchone()
conn.commit()
conn.close()
if row["request_hash"] != request_hash:
raise HTTPException(status_code=409, detail="Idempotency key conflict with different payload")
return row["response_body"]
cur.execute(
"INSERT INTO messages (id, chat_id, sender_id, text, created_at, attachment_ids) VALUES (%s, %s, %s, %s, %s, %s::uuid[])",
(msg_id, chat_id_str, user["id"], body.text, created_at, attachment_ids_str)
)
conn.commit()
except HTTPException:
raise
except Exception:
conn.rollback()
conn.close()
raise HTTPException(status_code=500, detail="Internal error processing message")
finally:
if not conn.closed:
conn.close()
return msg_response
@app.get("/api/v1/chats/{chat_id}/messages")
def list_messages(chat_id: UUID, limit: int = 50, cursor: Optional[str] = None,
response: Response = None, authorization: Optional[str] = Header(None)):
req_id = generate_request_id()
response.headers["X-Request-Id"] = req_id
response.headers["X-Instance-Id"] = INSTANCE_ID
user = get_current_user_from_token(authorization)
chat_id_str = str(chat_id)
conn = get_db_connection()
with conn.cursor() as cur:
cur.execute("SELECT 1 FROM chat_members WHERE chat_id=%s AND user_id=%s",
(chat_id_str, user["id"]))
if not cur.fetchone():
conn.close()
raise HTTPException(status_code=404, detail="Chat not found")
if limit < 1 or limit > 100:
conn.close()
raise HTTPException(status_code=400, detail="Invalid limit")
query = "SELECT id, chat_id, sender_id, text, created_at, attachment_ids FROM messages WHERE chat_id=%s"
params = [chat_id_str]
if cursor:
c_time, c_id = decode_cursor(cursor, chat_id_str)
query += " AND (created_at, id) > (%s, %s)"
params.extend([c_time, c_id])
query += " ORDER BY created_at ASC, id ASC LIMIT %s"
params.append(limit + 1)
with conn.cursor() as cur:
cur.execute(query, tuple(params))
rows = cur.fetchall()
conn.close()
has_next = len(rows) > limit
page_items = rows[:limit]
formatted = []
for m in page_items:
raw = m.get('attachment_ids')
atts = [str(a) for a in raw if a] if raw else []
formatted.append({
"id": str(m['id']), "chat_id": str(m['chat_id']),
"sender_id": str(m['sender_id']), "text": m['text'],
"created_at": m['created_at'].isoformat().replace("+00:00", "Z")
if isinstance(m['created_at'], datetime) else m['created_at'],
"attachment_ids": atts
})
next_cursor = None
if has_next and formatted:
last = formatted[-1]
next_cursor = encode_cursor(chat_id_str, last['created_at'], last['id'])
return {"items": formatted, "next_cursor": next_cursor}
@app.post("/api/v1/attachments/{attachment_id}/replay", status_code=202)
def replay_attachment(attachment_id: UUID, response: Response,
authorization: Optional[str] = Header(None)):
req_id = generate_request_id()
response.headers["X-Request-Id"] = req_id
response.headers["X-Instance-Id"] = INSTANCE_ID
user = get_current_user_from_token(authorization)
att_id = str(attachment_id)
conn = get_db_connection()
try:
with conn.cursor() as cur:
cur.execute("""
SELECT a.* FROM attachments a
JOIN chat_members cm ON a.chat_id = cm.chat_id
WHERE a.id=%s AND cm.user_id=%s
""", (att_id, user["id"]))
row = cur.fetchone()
if not row:
conn.close()
raise HTTPException(status_code=404, detail="Attachment not found")
if row["status"] != "failed":
conn.close()
raise HTTPException(status_code=409, detail="Only failed attachments can be replayed")
now = datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")
cur.execute("""
UPDATE attachments SET status='queued', attempt=0, error_code=NULL, updated_at=%s
WHERE id=%s
""", (now, att_id))
job = {"schema_version": 1, "job_id": str(uuid.uuid4()),
"type": "attachment.process.v1", "attachment_id": att_id,
"pipeline_version": 1, "attempt": 1, "created_at": now}
cur.execute("INSERT INTO outbox (id, stream, payload, created_at) "
"VALUES (%s, %s, %s::jsonb, %s)",
(str(uuid.uuid4()), STREAM_NAME, json.dumps(job), now))
conn.commit()
finally:
if not conn.closed:
conn.close()
return {"id": att_id, "status": "queued"}