|
1 | | -from fastapi import APIRouter, Depends, HTTPException, File, UploadFile, Query |
2 | | -from app.services.llm_service import LLM, OpenAI |
| 1 | +from fastapi import APIRouter, HTTPException, UploadFile, File, Form, Depends |
3 | 2 | from typing import List |
4 | 3 | import os |
5 | 4 |
|
6 | | -# from app.services.chroma_services import embedding |
7 | 5 | from app.services.file_manager_service import ( |
8 | 6 | get_file_manager, |
9 | 7 | get_file_manager_by_extension, |
|
18 | 16 | ) |
19 | 17 |
|
20 | 18 |
|
21 | | -@router.post("/upload_file") |
22 | | -async def upload_file(files: List[UploadFile] = File(...), token: str=Query(...)): |
| 19 | +@router.post("") |
| 20 | +async def upload_file(files: List[UploadFile], token: str): |
23 | 21 | """ |
24 | | - Carica il file nel database vettoriale |
| 22 | + Carica il file nel database vettoriale. |
25 | 23 |
|
26 | | - Args: |
27 | | - - files (List[UploadFile]): I file da caricare. Devono essere file di testo o PDF. |
| 24 | + ### Args: |
| 25 | + * **files (List[UploadFile])**: I file da caricare. Devono essere file di testo o PDF. |
28 | 26 |
|
29 | | - Raises: |
30 | | - - HTTPException: Se il file non è valido o se si verifica un errore durante il caricamento. |
31 | | - - HTTPException: Se il file esiste già nel database vettoriale. |
32 | | - - HTTPException: Se si verifica un errore durante il caricamento e l'elaborazione del file. |
| 27 | + ### Raises: |
| 28 | + * **HTTPException.400_BAD_REQUEST**: Se non sono stati forniti file o se i file non sono di tipo testo o PDF. |
| 29 | + * **HTTPException.500_INTERNAL_SERVER_ERROR**: Se si verifica un errore durante il caricamento dei file. |
33 | 30 | """ |
34 | 31 |
|
35 | 32 | if not files: |
@@ -97,59 +94,50 @@ async def upload_file(files: List[UploadFile] = File(...), token: str=Query(...) |
97 | 94 | } |
98 | 95 |
|
99 | 96 |
|
100 | | -@router.delete("/delete_file") |
| 97 | +@router.delete("") |
101 | 98 | async def delete_file(fileDelete: schemas.DocumentDelete): |
| 99 | + """ |
| 100 | + Elimina un file dal database. |
| 101 | +
|
| 102 | + ### Args: |
| 103 | + * **fileDelete (schemas.DocumentDelete)**: Il file da eliminare. Deve contenere il titolo, il token e la password corrente. |
| 104 | +
|
| 105 | + ### Raises: |
| 106 | + * **HTTPException.400_BAD_REQUEST**: Se il file non esiste o se si verifica un errore durante l'eliminazione. |
| 107 | + * **HTTPException.500_INTERNAL_SERVER_ERROR**: Se si verifica un errore durante l'eliminazione del file. |
| 108 | + """ |
102 | 109 | print("delete file title:", fileDelete) |
103 | | - file_manager = get_file_manager_by_extension(fileDelete.title) |
104 | | - if file_manager is None: |
105 | | - raise HTTPException(status_code=400, detail="File manager not found") |
106 | 110 | try: |
| 111 | + file_manager = get_file_manager_by_extension(fileDelete.title) |
| 112 | + if file_manager is None: |
| 113 | + raise HTTPException(status_code=400, detail="File manager not found") |
| 114 | + |
107 | 115 | file_path = file_manager.get_full_path(fileDelete.title) |
108 | 116 | print("file path:", file_path) |
109 | 117 | await file_manager.delete_document( |
110 | 118 | fileDelete.id, file_path, fileDelete.token, fileDelete.current_password |
111 | 119 | ) |
112 | | - except HTTPException as e: |
113 | | - match e.status_code: |
114 | | - case 404: |
115 | | - print("error detail:", e.detail) |
116 | | - raise HTTPException( |
117 | | - status_code=404, |
118 | | - detail="Document not found", |
119 | | - ) |
120 | | - case 500: |
121 | | - print("error detail:", e.detail) |
122 | | - raise HTTPException( |
123 | | - status_code=500, |
124 | | - detail="Error in deleting file", |
125 | | - ) |
126 | | - case _: |
127 | | - print("error detail:", e.detail) |
128 | | - raise HTTPException( |
129 | | - status_code=500, |
130 | | - detail="Error in deleting file", |
131 | | - ) |
132 | 120 |
|
| 121 | + return {"message": "File deleted successfully"} |
| 122 | + except HTTPException as http_exc: |
| 123 | + raise http_exc |
133 | 124 | except Exception as e: |
134 | | - print("error detail:", e) |
135 | | - raise HTTPException( |
136 | | - status_code=500, |
137 | | - detail="Error in deleting file", |
138 | | - ) |
139 | | - |
140 | | - return {"message": "File deleted successfully"} |
| 125 | + raise HTTPException(status_code=500, detail=f"Error in deleting file: {str(e)}") |
141 | 126 |
|
142 | 127 |
|
143 | | -@router.get("/get_documents") |
| 128 | +@router.get("") |
144 | 129 | def get_documents(): |
145 | 130 | """ |
146 | | - Ottiene la lista dei documenti dal database. |
| 131 | + Restituisce il numero di documenti e i loro nomi. |
147 | 132 |
|
148 | | - Args: |
149 | | - - token (str): Il token di autenticazione dell'utente. |
150 | | -
|
151 | | - Raises: |
152 | | - - HTTPException: Se si verifica un errore durante il recupero dei documenti. |
| 133 | + ### Returns: |
| 134 | + * **int**: Il numero di documenti. |
| 135 | + * **List[str]**: I nomi dei documenti. |
| 136 | + * **List[str]**: I nomi dei file nella directory /data/documents. |
153 | 137 | """ |
154 | | - |
155 | | - return os.listdir("/data/documents") |
| 138 | + file_manager = get_file_manager() |
| 139 | + return ( |
| 140 | + file_manager.get_documents_number(), |
| 141 | + file_manager.get_documents(), |
| 142 | + os.listdir("/data/documents"), |
| 143 | + ) |
0 commit comments