update ui

This commit is contained in:
subodh committed 2026-03-08 21:11:43 +05:30
1 parent 0e35933ee3
commit 29280d6490
5 files changed
+207 -454

No files matched your search

+10 -12
View File
@@ -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,
)
+11 -10
View File
@@ -1,5 +1,6 @@
import asyncio
from functools import lru_cache
from typing import BinaryIO
import boto3
from botocore.client import Config
@@ -21,25 +22,25 @@ def _get_r2_client():
)
def _put_object(object_name: str, file_bytes: bytes, content_type: str) -> str:
"""Blocking S3 put"""
def _upload_fileobj(file_obj: BinaryIO, object_name: str, content_type: str) -> str:
"""Stream file to R2. boto3 automatically uses multipart for large files."""
key = f"files/{object_name}"
_get_r2_client().put_object(
Bucket=settings.R2_BUCKET,
Key=key,
Body=file_bytes,
ContentType=content_type,
_get_r2_client().upload_fileobj(
file_obj,
settings.R2_BUCKET,
key,
ExtraArgs={"ContentType": content_type},
)
return f"{settings.PUBLIC_URL}/files/{object_name}"
async def upload_file(file_bytes: bytes, object_name: str, content_type: str) -> str:
"""Upload a file to R2 storage"""
async def upload_file(file_obj: BinaryIO, object_name: str, content_type: str) -> str:
"""Upload a file to R2 storage."""
try:
url = await asyncio.to_thread(
_put_object, object_name, file_bytes, content_type
_upload_fileobj, file_obj, object_name, content_type
)
except (BotoCoreError, ClientError) as exc:
raise RuntimeError(f"R2 upload failed: {exc}") from exc