diff --git a/updog/__main__.py b/updog/__main__.py index 63d5e06..e20c811 100644 --- a/updog/__main__.py +++ b/updog/__main__.py @@ -106,7 +106,7 @@ def home(path): if os.path.exists(requested_path): # Read the files try: - directory_files = process_files(os.scandir(requested_path), base_directory) + directory_files = process_files(os.scandir(requested_path), base_directory, requested_path) except PermissionError: abort(403, 'Read Permission Denied: ' + requested_path) diff --git a/updog/templates/home.html b/updog/templates/home.html index c53a4a3..d15c2d8 100644 --- a/updog/templates/home.html +++ b/updog/templates/home.html @@ -75,14 +75,20 @@

Directory: {{ directory }}

{% for file in files %} - {% if file.is_dir %} + {% if not file.exists %} + + {% elif file.is_dir %} {% else %} {% endif %} + {% if file.exists %} {{ file.name }}{% if file.is_dir %}/{% endif %} + {% else %} + {{ file.name }}{% if file.is_dir %}/{% endif %} + {% endif %} {{ file.size }} @@ -91,7 +97,9 @@

Directory: {{ directory }}

{{ file.last_modified }} - {% if not file.is_dir %} + {% if file.is_symlink and not file.exists %} + Dead Symbolic Link + {% elif not file.is_dir %} View in browser {% endif %} @@ -115,4 +123,4 @@

Directory: {{ directory }}

- \ No newline at end of file + diff --git a/updog/utils/path.py b/updog/utils/path.py index cde0c3f..986d25e 100644 --- a/updog/utils/path.py +++ b/updog/utils/path.py @@ -28,23 +28,42 @@ def human_readable_file_size(size): return '{:.4g} {}'.format(size / (1 << (order * 10)), _suffixes[order]) -def process_files(directory_files, base_directory): +def process_files(directory_files, base_directory, requested_path): files = [] - for file in directory_files: - if file.is_dir(): - size = '--' - size_sort = -1 - else: - size = human_readable_file_size(file.stat().st_size) - size_sort = file.stat().st_size + + for file_entry in directory_files: + + path = os.path.join(requested_path, file_entry.name) + + # Default values for display/sort fields + + # Size + size = '--' + size_sort = -1 + + # Last-Modified date + last_modified = '???' + last_modified_sort = -1 + + if os.path.exists(path): + last_modified_sort = file_entry.stat().st_mtime + last_modified = ctime(last_modified_sort) + + if os.path.isfile(path): + # Existing item is a file or a symlink points to a real file + size_sort = file_entry.stat().st_size + size = human_readable_file_size(size_sort) + files.append({ - 'name': file.name, - 'is_dir': file.is_dir(), - 'rel_path': get_relative_path(file.path, base_directory), + 'name': file_entry.name, + 'is_dir': os.path.isdir(path), + 'rel_path': get_relative_path(file_entry.path, base_directory), 'size': size, 'size_sort': size_sort, - 'last_modified': ctime(file.stat().st_mtime), - 'last_modified_sort': file.stat().st_mtime + 'last_modified': last_modified, + 'last_modified_sort': last_modified_sort, + 'is_symlink': file_entry.is_symlink(), + 'exists': os.path.exists(path) }) return files