update colorsceheme, allow anonymous 50mb uploads

This commit is contained in:
subodh committed 2026-03-13 09:56:05 +05:30
1 parent 95391d404b
commit f7c0310c91
4 files changed
+210 -114

No files matched your search

+17 -8
View File
@@ -7,7 +7,8 @@ from fastapi.middleware.cors import CORSMiddleware
from app.config.settings import settings
from app.services.r2 import upload_file
MAX_FILE_SIZE = 250 * 1024 * 1024 # 250 MB
UNAUTH_LIMIT = 50 * 1024 * 1024 # 50 MB — unauthenticated
AUTH_LIMIT = 250 * 1024 * 1024 # 250 MB — authenticated
app = FastAPI(title="File Host API")
@@ -46,19 +47,27 @@ async def auth(password: str = Form(...)):
async def upload(
request: Request,
file: UploadFile = File(...),
password: str = Form(...),
password: str = Form(default=""),
):
"""Validate password, enforce size limit, and stream the file to R2."""
if password != settings.UPLOAD_PASSWORD:
"""
Upload a file to R2.
- No password: allowed up to 50 MB.
- Correct password: allowed up to 250 MB.
- Wrong password (non-empty): rejected with 401.
"""
if password and password != settings.UPLOAD_PASSWORD:
raise HTTPException(status_code=401, detail="Invalid password.")
# Reject oversized requests early, before reading the body.
# Content-Length covers the whole multipart body; form overhead is negligible.
is_authenticated = password == settings.UPLOAD_PASSWORD
max_size = AUTH_LIMIT if is_authenticated else UNAUTH_LIMIT
limit_str = "250 MB" if is_authenticated else "50 MB"
content_length = request.headers.get("content-length")
if content_length and int(content_length) > MAX_FILE_SIZE:
if content_length and int(content_length) > max_size:
raise HTTPException(
status_code=413,
detail="File too large. Maximum allowed size is 250 MB.",
detail=f"File too large. Maximum allowed size is {limit_str}.",
)
content_type = (