mirror of
http://88.130.71.182:3000/BlitTech/contexta_be.git
synced 2026-06-13 08:30:07 +00:00
57 lines
1.9 KiB
Python
57 lines
1.9 KiB
Python
from fastapi import APIRouter, HTTPException, Depends, UploadFile, File
|
|
from app.database import get_supabase
|
|
from app.dependencies import get_current_user
|
|
from app.config import settings
|
|
import uuid
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
router = APIRouter(prefix="/upload", tags=["Upload"])
|
|
|
|
ALLOWED_IMAGE_TYPES = {"image/png", "image/jpeg", "image/jpg", "image/gif", "image/svg+xml", "image/webp"}
|
|
MAX_LOGO_SIZE = 2 * 1024 * 1024 # 2MB
|
|
|
|
|
|
@router.post("/logo")
|
|
async def upload_logo(
|
|
file: UploadFile = File(...),
|
|
user=Depends(get_current_user),
|
|
):
|
|
"""Upload a chatbot logo to Supabase Storage. Returns public URL."""
|
|
supabase = get_supabase()
|
|
|
|
# Validate content type
|
|
if file.content_type not in ALLOWED_IMAGE_TYPES:
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail=f"Invalid file type. Allowed: PNG, JPG, GIF, SVG, WebP"
|
|
)
|
|
|
|
# Read file
|
|
file_bytes = await file.read()
|
|
if len(file_bytes) > MAX_LOGO_SIZE:
|
|
raise HTTPException(status_code=413, detail="Image must be under 2MB")
|
|
|
|
# Determine extension
|
|
content_type = file.content_type or "image/png"
|
|
ext_map = {
|
|
"image/png": "png", "image/jpeg": "jpg", "image/jpg": "jpg",
|
|
"image/gif": "gif", "image/svg+xml": "svg", "image/webp": "webp"
|
|
}
|
|
ext = ext_map.get(content_type, "png")
|
|
|
|
# Upload to Supabase Storage
|
|
path = f"{user.id}/{uuid.uuid4().hex}.{ext}"
|
|
try:
|
|
result = supabase.storage.from_("logos").upload(
|
|
path=path,
|
|
file=file_bytes,
|
|
file_options={"content-type": content_type, "upsert": "true"},
|
|
)
|
|
# Get public URL
|
|
public_url = supabase.storage.from_("logos").get_public_url(path)
|
|
return {"url": public_url}
|
|
except Exception as e:
|
|
logger.error(f"Logo upload failed: {e}")
|
|
raise HTTPException(status_code=500, detail=f"Upload failed: {str(e)[:200]}")
|