added hash to filelist

This commit is contained in:
Vasily 2025-10-20 12:24:08 +08:00
parent 2c30dd9903
commit cdb2eddea9

View File

@ -5,6 +5,7 @@ from pydantic import BaseModel
from typing import List from typing import List
import uvicorn import uvicorn
import os import os
import hashlib
PORT = 8051 PORT = 8051
DIRECTORY = "./data/db" DIRECTORY = "./data/db"
@ -18,14 +19,26 @@ app = FastAPI()
class DeleteFileRequest(BaseModel): class DeleteFileRequest(BaseModel):
name: str 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(): 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: try:
files = [f for f in os.listdir(DIRECTORY) if os.path.isfile(os.path.join(DIRECTORY, f)) and f.endswith(".db")] files_with_hashes = []
return files 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: except Exception as e:
raise HTTPException(status_code=500, detail=f"An error occurred while listing files: {e}") raise HTTPException(status_code=500, detail=f"An error occurred while listing files: {e}")