from fastapi import APIRouter, UploadFile, File, Depends, HTTPException from fastapi.responses import FileResponse from typing import List from ..core.dependencies import get_current_active_user from ..utils.file_storage import file_storage from ..utils.logger import logger from pathlib import Path router = APIRouter() @router.post("/upload") async def upload_file( file: UploadFile = File(...), category: str = "documents", current_user = Depends(get_current_active_user) ) -> dict: try: file_path = await file_storage.save_file(file, category) if not file_path: raise HTTPException(status_code=400, detail="Failed to upload file") return { "filename": file.filename, "stored_path": file_path, "url": file_storage.get_file_url(file_path) } except ValueError as e: raise HTTPException(status_code=400, detail=str(e)) except Exception as e: logger.error(f"File upload error: {str(e)}") raise HTTPException(status_code=500, detail="Internal server error") @router.delete("/{file_path:path}") async def delete_file( file_path: str, current_user = Depends(get_current_active_user) ) -> dict: success = await file_storage.delete_file(file_path) if not success: raise HTTPException(status_code=404, detail="File not found") return {"status": "success", "message": "File deleted successfully"} @router.get("/{file_path:path}") async def get_file( file_path: str, current_user = Depends(get_current_active_user) ): full_path = Path("uploads") / file_path if not full_path.exists(): raise HTTPException(status_code=404, detail="File not found") return FileResponse(str(full_path))