Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -41,3 +41,25 @@ async def list_files():
|
|
41 |
return JSONResponse({"files_data": files_data})
|
42 |
except FileNotFoundError:
|
43 |
raise HTTPException(status_code=404, detail="Diret贸rio de dados n茫o encontrado")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
return JSONResponse({"files_data": files_data})
|
42 |
except FileNotFoundError:
|
43 |
raise HTTPException(status_code=404, detail="Diret贸rio de dados n茫o encontrado")
|
44 |
+
|
45 |
+
|
46 |
+
@app.get("/files/{origin_id}")
|
47 |
+
async def get_file_by_origin_id(origin_id: int):
|
48 |
+
try:
|
49 |
+
for filename in os.listdir(BASE_DIR):
|
50 |
+
if filename.startswith(f"{origin_id}_") and filename.endswith(".json"):
|
51 |
+
filepath = os.path.join(BASE_DIR, filename)
|
52 |
+
if os.path.isfile(filepath):
|
53 |
+
try:
|
54 |
+
with open(filepath, "r") as f:
|
55 |
+
file_content = f.read()
|
56 |
+
try:
|
57 |
+
file_content_json = json.loads(file_content)
|
58 |
+
return JSONResponse({"filename": filename, "content": file_content_json})
|
59 |
+
except json.JSONDecodeError:
|
60 |
+
return JSONResponse({"filename": filename, "content": file_content})
|
61 |
+
except (IOError, OSError) as e:
|
62 |
+
raise HTTPException(status_code=500, detail=f"Erro ao ler o ficheiro {filename}: {e}")
|
63 |
+
raise HTTPException(status_code=404, detail=f"Ficheiro com originId '{origin_id}' n茫o encontrado")
|
64 |
+
except FileNotFoundError:
|
65 |
+
raise HTTPException(status_code=404, detail="Diret贸rio de dados n茫o encontrado")
|