Spaces:
Runtime error

File size: 802 Bytes
5e0d166
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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)}")