from fastapi import APIRouter, Depends, HTTPException, UploadFile, File from app.core.deps import require_role from app.db.models import User from app.services.cos_service import upload_image router = APIRouter(prefix="/api/upload", tags=["upload"]) @router.post("/image") async def upload_image_api( file: UploadFile = File(...), user: User = Depends(require_role("super_admin", "class_admin")), ): """Upload an image to Tencent COS.""" contents = await file.read() if len(contents) > 10 * 1024 * 1024: # 10MB raise HTTPException(status_code=400, detail="File too large (max 10MB)") if file.content_type not in {"image/jpeg", "image/png", "image/gif", "image/webp"}: raise HTTPException(status_code=400, detail="Invalid file type, only JPEG/PNG/GIF/WebP allowed") url = upload_image("images", file.filename or "upload.jpg", contents, file.content_type) return {"url": url}