Skip to content

Commit dffb410

Browse files
author
mominur-helios
committed
fix path issues for cross platform
1 parent 6b5cd68 commit dffb410

File tree

4 files changed

+30
-14
lines changed

4 files changed

+30
-14
lines changed

home/templatetags/file_extension.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,8 @@ def file_extension(value):
1212

1313
@register.filter
1414
def encoded_file_path(path):
15-
return path.replace(os.sep, '%slash%')
15+
return path.replace('/', '%slash%')
16+
17+
@register.filter
18+
def encoded_path(path):
19+
return path.replace('\\', '/')

home/views.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ def get_files_from_directory(directory_path):
3434

3535

3636
def get_breadcrumbs(request):
37-
path_components = [component for component in request.path.split(os.sep) if component]
37+
path_components = [component for component in request.path.split("/") if component]
3838
breadcrumbs = []
3939
url = ''
4040

4141
for component in path_components:
42-
url += f'{os.sep}{component}'
42+
url += f'/{component}'
4343
if component == "file-manager":
4444
component = "media"
4545
breadcrumbs.append({'name': component, 'url': url})

templates/includes/subdirectories.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
{% load static file_extension %}
2+
13
{% for subdirectory in directory.directories %}
24
{% if subdirectory.directories %}
35
<li
@@ -11,7 +13,7 @@
1113
role="button"
1214
aria-expanded="false"
1315
aria-controls="collapse{{subdirectory.id}}"
14-
onclick="window.location.href = '{% url 'file_manager' subdirectory.path|urlencode %}';"
16+
onclick="window.location.href = '{% url 'file_manager' subdirectory.path|encoded_path %}';"
1517
>
1618
{{ subdirectory.name }}
1719
</a>
@@ -23,7 +25,7 @@
2325
class="ms-{{depth}} collapse show"
2426
>
2527
<i class="fa-solid fa-folder"></i>
26-
<a href="{% url 'file_manager' subdirectory.path|urlencode %}">{{ subdirectory.name }}</a>
28+
<a href="{% url 'file_manager' subdirectory.path|encoded_path %}">{{ subdirectory.name }}</a>
2729
</li>
2830
{% endif %}
2931
{% endfor %}

templates/pages/file-manager.html

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
role="button"
4949
aria-expanded="false"
5050
aria-controls="collapse{{directory.id}}"
51-
onclick="window.location.href = '{% url 'file_manager' directory.path|urlencode %}';"
51+
onclick="window.location.href = '{% url 'file_manager' directory.path|encoded_path %}';"
5252
>
5353
{{ directory.name }}
5454
</a>
@@ -57,7 +57,7 @@
5757
{% else %}
5858
<li>
5959
<i class="fa-solid fa-folder"></i>
60-
<a href="{% url 'file_manager' directory.path|urlencode %}">{{ directory.name }}</a>
60+
<a href="{% url 'file_manager' directory.path|encoded_path %}">{{ directory.name }}</a>
6161
</li>
6262
{% endif %}
6363
{% endfor %}
@@ -75,6 +75,7 @@
7575
</form>
7676
</div>
7777
{% if files %}
78+
{{files|length|json_script:"files-count"}}
7879
<div class="table-responsive">
7980
<table class="table">
8081
<tr>
@@ -92,12 +93,6 @@
9293
<i title="View" class="fa-solid fa-eye text-primary"></i>
9394
</span>
9495
<div class="dot-separator mx-2"></div>
95-
<span>
96-
<a href="{% url 'download_file' file.file|encoded_file_path %}">
97-
<i title="Download" class="fa-solid fa-download text-success"></i>
98-
</a>
99-
</span>
100-
<div class="dot-separator mx-2"></div>
10196
<span data-bs-toggle="modal" data-bs-target="#delete-{{forloop.counter}}">
10297
<i title="Delete" class="fa-solid fa-trash text-danger"></i>
10398
</span>
@@ -111,7 +106,12 @@
111106
<div class="modal-content">
112107
<div class="modal-header">
113108
<h1 class="modal-title fs-5" id="staticBackdropLabel">{{ file.filename }}</h1>
114-
<div class="" data-bs-dismiss="modal" aria-label="Close">
109+
<span>
110+
<a href="{% url 'download_file' file.file|encoded_file_path %}">
111+
<i title="Download" class="fa-solid fa-download text-success fs-4"></i>
112+
</a>
113+
</span>
114+
<div class="" id="modal-close-btn-{{forloop.counter}}" data-bs-dismiss="modal" aria-label="Close">
115115
<i class="fa-solid fa-circle-xmark fs-5"></i>
116116
</div>
117117
</div>
@@ -164,6 +164,16 @@ <h1 class="modal-title fs-5" id="exampleModalLabel">Delete File</h1>
164164
function submitForm() {
165165
document.getElementById("upload-file").submit();
166166
}
167+
168+
document.addEventListener('keydown', (event) => {
169+
if (event.key === 'Escape' || event.key === 'Esc' || event.key === 27) {
170+
let files = document.getElementById('files-count').textContent;
171+
for (let i = 1; i <= files; i++) {
172+
let closeButtonElements = document.getElementById(`modal-close-btn-${i}`);
173+
closeButtonElements.click();
174+
}
175+
}
176+
})
167177
</script>
168178

169179
{% endblock extrascripts %}

0 commit comments

Comments
 (0)