|
import gradio as gr |
|
import logging |
|
from pathlib import Path |
|
|
|
|
|
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s") |
|
|
|
|
|
DEFAULT_DATA_PATH = Path.home() |
|
|
|
|
|
def get_storage(data_path: str): |
|
|
|
data_path = Path(data_path) |
|
|
|
|
|
if not data_path.exists() or not data_path.is_dir(): |
|
logging.error(f"Directory not found: {data_path}") |
|
return [], f"Error: Directory not found or inaccessible: {data_path}" |
|
|
|
|
|
files = [] |
|
total_size = 0 |
|
for file in data_path.glob("**/*"): |
|
if file.is_file(): |
|
try: |
|
stats = file.stat() |
|
files.append({ |
|
"Original Name": file.name, |
|
"Path": str(file.resolve()), |
|
"Size (MB)": f"{stats.st_size / (1024.0 ** 2):.2f} MB", |
|
}) |
|
total_size += stats.st_size |
|
except Exception as e: |
|
logging.warning(f"Failed to process file: {file}. Error: {e}") |
|
|
|
if not files: |
|
logging.info(f"No files found in directory: {data_path}") |
|
return [], "No files found in the specified directory." |
|
|
|
|
|
usage = f"{total_size / (1024.0 ** 3):.3f} GB" |
|
logging.info(f"Scanned {len(files)} files. Total size: {usage}") |
|
return files, usage |
|
|
|
|
|
with gr.Blocks() as app: |
|
|
|
gr.Markdown("# π File Storage Viewer\n" |
|
"Easily view files and calculate storage usage in a specified directory.") |
|
|
|
with gr.Row(): |
|
|
|
dir_input = gr.Textbox( |
|
value=str(DEFAULT_DATA_PATH), |
|
label="Directory Path", |
|
placeholder="Enter the directory path to scan.", |
|
) |
|
|
|
fetch_btn = gr.Button("Fetch Files") |
|
|
|
|
|
with gr.Row(): |
|
file_table = gr.Dataframe( |
|
headers=["Original Name", "Path", "Size (MB)"], |
|
interactive=False, |
|
label="Files", |
|
) |
|
storage_usage = gr.Textbox(label="Total Storage Usage", interactive=False) |
|
|
|
|
|
fetch_btn.click( |
|
get_storage, |
|
inputs=[dir_input], |
|
outputs=[file_table, storage_usage], |
|
show_progress=True |
|
) |
|
|
|
|
|
app.launch(allowed_paths=[str(DEFAULT_DATA_PATH)], enable_queue=True) |
|
|
|
|