from typing import Any, Dict from fastapi import APIRouter, Depends, File, HTTPException, UploadFile, status from app import models from app.api import deps from app.utils import files router = APIRouter() @router.post("/images/", response_model=Dict[str, str]) async def upload_image( *, file: UploadFile = File(...), current_user: models.User = Depends(deps.get_current_active_superuser), ) -> Any: """ Upload an image file. """ try: file_path = await files.save_image(file) return {"file_path": file_path} except HTTPException: raise except Exception as e: raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Error uploading file: {str(e)}", ) @router.post("/documents/", response_model=Dict[str, str]) async def upload_document( *, file: UploadFile = File(...), current_user: models.User = Depends(deps.get_current_active_superuser), ) -> Any: """ Upload a document file. """ try: file_path = await files.save_document(file) return {"file_path": file_path} except HTTPException: raise except Exception as e: raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Error uploading file: {str(e)}", ) @router.delete("/{file_path:path}", response_model=Dict[str, bool]) async def delete_uploaded_file( *, file_path: str, current_user: models.User = Depends(deps.get_current_active_superuser), ) -> Any: """ Delete an uploaded file. """ try: success = files.delete_file(file_path) if not success: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail="File not found", ) return {"success": True} except HTTPException: raise except Exception as e: raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Error deleting file: {str(e)}", )