update ui
This commit is contained in:
1 parent
0e35933ee3
commit
29280d6490
5 files changed
+207
-454
No files matched your search
+10
-12
@@ -1,13 +1,13 @@
|
||||
import mimetypes
|
||||
import secrets
|
||||
|
||||
from fastapi import FastAPI, File, Form, HTTPException, UploadFile
|
||||
from fastapi import FastAPI, File, Form, HTTPException, Request, UploadFile
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
from app.config.settings import settings
|
||||
from app.services.r2 import upload_file
|
||||
|
||||
MAX_FILE_SIZE = 50 * 1024 * 1024 # 50 MB
|
||||
MAX_FILE_SIZE = 250 * 1024 * 1024 # 250 MB
|
||||
|
||||
app = FastAPI(title="File Host API")
|
||||
|
||||
@@ -44,23 +44,21 @@ async def auth(password: str = Form(...)):
|
||||
|
||||
@app.post("/upload")
|
||||
async def upload(
|
||||
request: Request,
|
||||
file: UploadFile = File(...),
|
||||
password: str = Form(...),
|
||||
):
|
||||
"""Validate password, enforce size limit, and upload the file to R2."""
|
||||
"""Validate password, enforce size limit, and stream the file to R2."""
|
||||
if password != settings.UPLOAD_PASSWORD:
|
||||
raise HTTPException(status_code=401, detail="Invalid password.")
|
||||
|
||||
content = await file.read()
|
||||
|
||||
if not content:
|
||||
raise HTTPException(status_code=400, detail="Uploaded file is empty.")
|
||||
|
||||
if len(content) > MAX_FILE_SIZE:
|
||||
mb = len(content) / 1024 / 1024
|
||||
# Reject oversized requests early, before reading the body.
|
||||
# Content-Length covers the whole multipart body; form overhead is negligible.
|
||||
content_length = request.headers.get("content-length")
|
||||
if content_length and int(content_length) > MAX_FILE_SIZE:
|
||||
raise HTTPException(
|
||||
status_code=413,
|
||||
detail=f"File is too large ({mb:.1f} MB). Maximum allowed size is 50 MB.",
|
||||
detail="File too large. Maximum allowed size is 250 MB.",
|
||||
)
|
||||
|
||||
content_type = (
|
||||
@@ -70,7 +68,7 @@ async def upload(
|
||||
|
||||
try:
|
||||
url = await upload_file(
|
||||
file_bytes=content,
|
||||
file_obj=file.file,
|
||||
object_name=object_name,
|
||||
content_type=content_type,
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user