Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dead symlink handling #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion updog/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
14 changes: 11 additions & 3 deletions updog/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,20 @@ <h2>Directory: {{ directory }}</h2>
{% 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 }}
Expand All @@ -91,7 +97,9 @@ <h2>Directory: {{ directory }}</h2>
{{ 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>
Expand All @@ -115,4 +123,4 @@ <h2>Directory: {{ directory }}</h2>
<script src="{{ url_for('static', filename='js/main.js') }}"></script>

</body>
</html>
</html>
45 changes: 32 additions & 13 deletions updog/utils/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down