fix(upload): configure chat attachment size limit (#2439)

This commit is contained in:
nubs
2026-06-07 20:42:24 +00:00
committed by GitHub
parent 8746c9c0df
commit 865e61450e
9 changed files with 106 additions and 4 deletions
+22
View File
@@ -1,7 +1,12 @@
"""Small helpers for route-local upload size caps."""
import os
from fastapi import HTTPException, UploadFile
DEFAULT_CHAT_UPLOAD_MAX_BYTES = 10 * 1024 * 1024
CHAT_UPLOAD_MAX_BYTES_ENV = "ODYSSEUS_CHAT_UPLOAD_MAX_BYTES"
def format_byte_limit(limit: int) -> str:
if limit % (1024 * 1024) == 0:
@@ -11,6 +16,23 @@ def format_byte_limit(limit: int) -> str:
return f"{limit} bytes"
def read_byte_limit_env(name: str, default: int) -> int:
raw = os.getenv(name)
if raw is None or not raw.strip():
return default
try:
limit = int(raw)
except ValueError as exc:
raise ValueError(f"{name} must be an integer byte count") from exc
if limit < 1:
raise ValueError(f"{name} must be greater than 0")
return limit
def get_chat_upload_max_bytes() -> int:
return read_byte_limit_env(CHAT_UPLOAD_MAX_BYTES_ENV, DEFAULT_CHAT_UPLOAD_MAX_BYTES)
async def read_upload_limited(upload: UploadFile, limit: int, label: str = "Upload") -> bytes:
"""Read an UploadFile with a hard byte cap."""
data = await upload.read(limit + 1)