Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -1,13 +1,42 @@
|
|
1 |
from typing import List
|
2 |
-
from fastapi import FastAPI
|
3 |
-
|
4 |
-
|
5 |
-
|
|
|
6 |
|
7 |
app = FastAPI()
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
return similarities
|
|
|
1 |
from typing import List
|
2 |
+
from fastapi import FastAPI, HTTPException
|
3 |
+
from fastapi.responses import JSONResponse
|
4 |
+
import os
|
5 |
+
import json
|
6 |
+
from models import RequestModel
|
7 |
|
8 |
app = FastAPI()
|
9 |
|
10 |
+
BASE_DIR = "/tmp/data"
|
11 |
+
|
12 |
+
@app.post("/save")
|
13 |
+
async def save(images: List[RequestModel]):
|
14 |
+
os.makedirs(BASE_DIR, exist_ok=True)
|
15 |
+
for image_data in images:
|
16 |
+
filename = os.path.join(BASE_DIR, f"{image_data.originId}_{image_data.assetCode}.json")
|
17 |
+
with open(filename, "w") as f:
|
18 |
+
json.dump(image_data.dict(), f, indent=4)
|
19 |
+
return True
|
20 |
+
|
21 |
+
@app.get("/files")
|
22 |
+
async def list_files():
|
23 |
+
try:
|
24 |
+
files_data = []
|
25 |
+
for filename in os.listdir(BASE_DIR):
|
26 |
+
filepath = os.path.join(BASE_DIR, filename)
|
27 |
+
if os.path.isfile(filepath):
|
28 |
+
try:
|
29 |
+
with open(filepath, "r") as f:
|
30 |
+
file_content = f.read() # L锚 o conte煤do do ficheiro
|
31 |
+
# Tenta decodificar o conte煤do como JSON, se poss铆vel
|
32 |
+
try:
|
33 |
+
file_content_json = json.loads(file_content)
|
34 |
+
files_data.append({"filename": filename, "content": file_content_json})
|
35 |
+
except json.JSONDecodeError:
|
36 |
+
files_data.append({"filename": filename, "content": file_content}) # Se n茫o for JSON, retorna o texto
|
37 |
+
except (IOError, OSError) as e:
|
38 |
+
raise HTTPException(status_code=500, detail=f"Erro ao ler o ficheiro {filename}: {e}")
|
39 |
|
40 |
+
return JSONResponse({"files_data": files_data})
|
41 |
+
except FileNotFoundError:
|
42 |
+
raise HTTPException(status_code=404, detail="Diret贸rio de dados n茫o encontrado")
|
|