add directory mediatype functionality via helpers, begin looking into static jquery (nothing yet)

This commit is contained in:
Xevion
2020-10-31 17:14:20 -05:00
parent 8cbf8396fd
commit c53e8ecf9c
4 changed files with 35 additions and 8 deletions

View File

@@ -1,4 +1,31 @@
def get_mediatype(mimetype: str) -> str:
import os
from typing import Tuple, List
def extra_listdir(path: str) -> List[Tuple[str, str]]:
"""
Helper function used for identifying file media type for every file in a given directory, extending os.listdir
:param path: The path to the directory.
:return: A list of tuples, each containing two strings, the file or directory name, and the media type.
"""
return [(file, get_all_mediatype(file, path)) for file in os.listdir(path)]
def get_all_mediatype(head: str, tail: str) -> str:
"""
A extra media type function supporting directories on top of files.
:param head: The head of the path, usually the directory name or filename at the very end.
:param tail: The rest of the path, everything that comes before the head.
:return: A media type in string form.
"""
if os.path.isfile(os.path.join(tail, head)):
return get_file_mediatype(head)
return "folder"
def get_file_mediatype(mimetype: str) -> str:
"""Simple media type categorization based on the given mimetype"""
if mimetype is not None:
if mimetype.startswith('image'):

1
viewer/static/hover.js Normal file
View File

@@ -0,0 +1 @@
asdas

View File

@@ -17,10 +17,13 @@
<span class="panel-icon pr-4">
<i class="fas fa-{{ file.1 }} fa-lg" aria-hidden="true"></i>
</span>
<a href="{% url 'file' directory.id file.0 %}">
<a href="{% if file.1 != 'folder' %}{% url 'file' directory.id file.0 %}{% endif %}">
{{ file.0 }}
</a>
</div>
{% endfor %}
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" integrity="sha512-bLT0Qm9VnAYZDflyKcBaQ2gg0hSYNQrJ8RilYldYQ1FxQYoCLtUjuuRuZo+fjqhx/qtq/1itJ0C2ejDxltZVFg==" crossorigin="anonymous"></script>
{% load static %}
<script src="{% static "hover.js" %}"></script>
{% endblock content %}

View File

@@ -1,10 +1,9 @@
import mimetypes
import os
from django.http import FileResponse
from django.shortcuts import render, get_object_or_404
from viewer.helpers import get_mediatype
from viewer.helpers import extra_listdir
from viewer.models import ServedDirectory
@@ -20,12 +19,9 @@ def browse(request, directory_id):
dir = get_object_or_404(ServedDirectory, id=directory_id)
if os.path.isdir(dir.path):
files = [
(file, get_mediatype(mimetypes.guess_type(file)[0])) for file in os.listdir(dir.path)
]
context = {
'title': f'Browse - {os.path.dirname(dir.path)}',
'files': files,
'files': extra_listdir(dir.path),
'directory': dir
}
return render(request, 'browse.html', context)