mirror of
https://github.com/pewdiepie-archdaemon/odysseus.git
synced 2026-07-11 12:27:13 +00:00
fix(upload): configure chat attachment size limit (#2439)
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user