From cdb2eddea96a1611aed61ca71767e3c1e2da67fd Mon Sep 17 00:00:00 2001 From: Vasily Date: Mon, 20 Oct 2025 12:24:08 +0800 Subject: [PATCH] added hash to filelist --- simple_server.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/simple_server.py b/simple_server.py index 22fa4c7..437099b 100644 --- a/simple_server.py +++ b/simple_server.py @@ -5,6 +5,7 @@ from pydantic import BaseModel from typing import List import uvicorn import os +import hashlib PORT = 8051 DIRECTORY = "./data/db" @@ -18,14 +19,26 @@ app = FastAPI() class DeleteFileRequest(BaseModel): name: str -@app.post("/api/files/list", response_model=List[str]) +class FileInfo(BaseModel): + name: str + hash: str + +@app.post("/api/files/list", response_model=List[FileInfo]) async def api_list_files(): """ - Lists all files in the data/db directory. + Lists all files and their SHA256 hashes in the data/db directory. """ try: - files = [f for f in os.listdir(DIRECTORY) if os.path.isfile(os.path.join(DIRECTORY, f)) and f.endswith(".db")] - return files + files_with_hashes = [] + for f in os.listdir(DIRECTORY): + file_path = os.path.join(DIRECTORY, f) + if os.path.isfile(file_path) and f.endswith(".db"): + hasher = hashlib.sha256() + with open(file_path, "rb") as afile: + buf = afile.read() + hasher.update(buf) + files_with_hashes.append({"name": f, "hash": hasher.hexdigest()}) + return files_with_hashes except Exception as e: raise HTTPException(status_code=500, detail=f"An error occurred while listing files: {e}")