Spaces:
Runtime error

communityapp2 / src /utils.py
low-key-007's picture
Create utils.py
5e0d166 verified
Raw History Blame Contribute Delete
802 Bytes
import uuid
from fastapi import UploadFile, HTTPException
from src.config import supabase
async def upload_image(file: UploadFile, bucket: str) -> str:
"""Uploads an image to Supabase Storage and returns the public URL."""
if file.size > 5 * 1024 * 1024:
raise HTTPException(413, "Image too large (Max 5MB)")
ext = file.filename.split('.')[-1]
filename = f"{uuid.uuid4()}.{ext}"
try:
file_content = await file.read()
supabase.storage.from_(bucket).upload(
path=filename,
file=file_content,
file_options={"content-type": file.content_type}
)
return supabase.storage.from_(bucket).get_public_url(filename)
except Exception as e:
raise HTTPException(500, f"Upload failed: {str(e)}")