69 lines
2.2 KiB
Python
69 lines
2.2 KiB
Python
from fastapi import APIRouter, Depends, UploadFile, File, HTTPException
|
|
from fastapi.responses import FileResponse
|
|
from pathlib import Path
|
|
from app.models.user import User
|
|
from app.api.auth import get_current_user
|
|
from app.core.upload import save_upload_file, delete_file
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.post("/profile-picture")
|
|
async def upload_profile_picture(
|
|
file: UploadFile = File(...), current_user: User = Depends(get_current_user)
|
|
):
|
|
"""Upload a profile picture"""
|
|
try:
|
|
file_path = await save_upload_file(file, "profile_pictures")
|
|
|
|
# Delete old profile picture if exists
|
|
if current_user.profile_picture:
|
|
delete_file(current_user.profile_picture)
|
|
|
|
# Update user's profile picture in database would happen here
|
|
# For now, just return the path
|
|
return {
|
|
"file_path": file_path,
|
|
"message": "Profile picture uploaded successfully",
|
|
}
|
|
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=f"Upload failed: {str(e)}")
|
|
|
|
|
|
@router.post("/event-image")
|
|
async def upload_event_image(
|
|
file: UploadFile = File(...), current_user: User = Depends(get_current_user)
|
|
):
|
|
"""Upload an event image"""
|
|
try:
|
|
file_path = await save_upload_file(file, "event_images")
|
|
return {"file_path": file_path, "message": "Event image uploaded successfully"}
|
|
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=f"Upload failed: {str(e)}")
|
|
|
|
|
|
@router.post("/post-image")
|
|
async def upload_post_image(
|
|
file: UploadFile = File(...), current_user: User = Depends(get_current_user)
|
|
):
|
|
"""Upload a post image"""
|
|
try:
|
|
file_path = await save_upload_file(file, "post_images")
|
|
return {"file_path": file_path, "message": "Post image uploaded successfully"}
|
|
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=f"Upload failed: {str(e)}")
|
|
|
|
|
|
@router.get("/serve/{folder}/{filename}")
|
|
async def serve_file(folder: str, filename: str):
|
|
"""Serve uploaded files"""
|
|
file_path = Path(f"/app/storage/uploads/{folder}/{filename}")
|
|
|
|
if not file_path.exists():
|
|
raise HTTPException(status_code=404, detail="File not found")
|
|
|
|
return FileResponse(file_path)
|