files sorting and filter

This commit is contained in:
Vasily.onl 2025-06-23 12:04:15 +08:00
parent bf6fffd52c
commit 3c7dbf327c

View File

@ -18,24 +18,38 @@ app.mount("/static", StaticFiles(directory="static"), name="static")
@app.get("/files", response_class=HTMLResponse) @app.get("/files", response_class=HTMLResponse)
async def list_files(): async def list_files():
files_html = "" db_files_info = []
for root, _, files in os.walk(DIRECTORY): for root, _, files in os.walk(DIRECTORY):
for file in files: for file in files:
file_path = os.path.join(root, file) if file.endswith(".db"):
relative_path = os.path.relpath(file_path, DIRECTORY) file_path = os.path.join(root, file)
try: relative_path = os.path.relpath(file_path, DIRECTORY)
file_size = os.path.getsize(file_path) try:
# convert to GB file_size = os.path.getsize(file_path)
file_size = file_size / (1024 * 1024 * 1024) creation_time = os.path.getctime(file_path)
files_html += f""" db_files_info.append({
<li> "relative_path": relative_path,
{relative_path} ({file_size:.3f} GB) "file_size": file_size,
<a href="/data/{relative_path}" download="{relative_path}">Download</a> "creation_time": creation_time
<button onclick="deleteFile('{relative_path}')">Delete</button> })
</li> except Exception as e:
""" # Log or handle error for specific file without breaking the list
except Exception as e: print(f"Error accessing {file_path}: {e}")
files_html += f"<li>Error accessing {relative_path}: {e}</li>"
# Sort files by creation time
db_files_info.sort(key=lambda x: x["creation_time"])
files_html = ""
for file_info in db_files_info:
# convert to GB
file_size_gb = file_info["file_size"] / (1024 * 1024 * 1024)
files_html += f"""
<li>
{file_info["relative_path"]} ({file_size_gb:.3f} GB)
<a href="/data/{file_info["relative_path"]}" download="{file_info["relative_path"]}">Download</a>
<button onclick="deleteFile('{file_info["relative_path"]}')">Delete</button>
</li>
"""
return f""" return f"""
<html> <html>