File size: 2,747 Bytes
cf25fe0
c6c67e3
830055d
cf25fe0
c6c67e3
 
 
 
830055d
cf25fe0
8e0edfc
c6c67e3
 
 
830055d
c6c67e3
 
 
830055d
 
c6c67e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
830055d
c6c67e3
 
 
830055d
c6c67e3
 
 
8e0edfc
cf25fe0
8e0edfc
 
 
c6c67e3
 
830055d
c6c67e3
 
 
 
 
 
 
 
 
830055d
c6c67e3
8e0edfc
c6c67e3
 
830055d
c6c67e3
 
 
830055d
8e0edfc
c6c67e3
 
 
 
 
 
8e0edfc
830055d
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import gradio as gr
import logging
from pathlib import Path

# Configure logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")

# Default directory to scan
DEFAULT_DATA_PATH = Path.home()  # Use home directory as a dynamic default

# Function to get file details and total storage usage
def get_storage(data_path: str):
    # Convert to Path object
    data_path = Path(data_path)
    
    # Validate directory
    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}"
    
    # Collect file details and calculate total size
    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."
    
    # Total size in GB
    usage = f"{total_size / (1024.0 ** 3):.3f} GB"
    logging.info(f"Scanned {len(files)} files. Total size: {usage}")
    return files, usage

# Define the Gradio interface
with gr.Blocks() as app:
    # Title and description
    gr.Markdown("# πŸ“ File Storage Viewer\n"
                "Easily view files and calculate storage usage in a specified directory.")
    
    with gr.Row():
        # Input field for directory path
        dir_input = gr.Textbox(
            value=str(DEFAULT_DATA_PATH),
            label="Directory Path",
            placeholder="Enter the directory path to scan.",
        )
        # Button to trigger file listing
        fetch_btn = gr.Button("Fetch Files")
    
    # Outputs: File list and total storage usage
    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)
    
    # Click action to trigger the `get_storage` function
    fetch_btn.click(
        get_storage,
        inputs=[dir_input],
        outputs=[file_table, storage_usage],
        show_progress=True
    )

# Launch the Gradio app
app.launch(allowed_paths=[str(DEFAULT_DATA_PATH)], enable_queue=True)