File size: 1,832 Bytes
a3b84cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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))