fix(uploads): bound direct upload reads

* Stabilize full test collection

* Add bounded reads for direct uploads
This commit is contained in:
Vykos
2026-06-04 01:32:50 +02:00
committed by GitHub
parent 48f5182286
commit 193dc2f085
7 changed files with 105 additions and 16 deletions

22
src/upload_limits.py Normal file
View File

@@ -0,0 +1,22 @@
"""Small helpers for route-local upload size caps."""
from fastapi import HTTPException, UploadFile
def format_byte_limit(limit: int) -> str:
if limit % (1024 * 1024) == 0:
return f"{limit // (1024 * 1024)} MB"
if limit % 1024 == 0:
return f"{limit // 1024} KB"
return f"{limit} 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)
if len(data) > limit:
raise HTTPException(
status_code=413,
detail=f"{label} exceeds {format_byte_limit(limit)} limit",
)
return data