This commit is contained in:
Alan Deutscher 2020-02-24 23:25:46 -08:00 committed by GitHub
commit 31872de5d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 17 deletions

View File

@ -106,7 +106,7 @@ def main():
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)

View File

@ -75,14 +75,20 @@
{% for file in files %}
<tr>
<td> <!-- Icon -->
{% if file.is_dir %}
{% if not file.exists %}
<button class="file_ic"><i class="far"></i></button><!-- Dead Symlink -->
{% elif file.is_dir %}
<button class="file_ic"><i class="far fa-folder"></i></button><!-- Directory icon -->
{% else %}
<button class="file_ic"><i class="far fa-file"></i></button><!-- File icon -->
{% endif %}
</td>
<td> <!-- Name -->
{% if file.exists %}
<a href="/{{ file.rel_path }}">{{ file.name }}{% if file.is_dir %}/{% endif %}</a>
{% else %}
{{ file.name }}{% if file.is_dir %}/{% endif %}
{% endif %}
</td>
<td data-order="{{ file.size_sort }}"> <!-- File size -->
{{ file.size }}
@ -91,7 +97,9 @@
{{ file.last_modified }}
</td>
<td> <!-- View file -->
{% if not file.is_dir %}
{% if file.is_symlink and not file.exists %}
Dead Symbolic Link
{% elif not file.is_dir %}
<a href="/{{ file.rel_path }}?view">View in browser</a>
{% endif %}
</td>
@ -115,4 +123,4 @@
<script src="{{ url_for('static', filename='js/main.js') }}"></script>
</body>
</html>
</html>

View File

@ -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