added hash to filelist
This commit is contained in:
parent
2c30dd9903
commit
cdb2eddea9
@ -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}")
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user