diff --git a/viewer/helpers.py b/viewer/helpers.py index 101871b..915ca15 100644 --- a/viewer/helpers.py +++ b/viewer/helpers.py @@ -1,8 +1,40 @@ +import mimetypes import os from typing import List, Tuple +from django.urls import reverse -def extra_listdir(path: str) -> List[Tuple[str, str]]: +from viewer.models import ServedDirectory + + +class File: + def __init__(self, head, tail): + self.filename = head + self.fullpath = os.path.join(tail, head) + self.mediatype = self.get_mediatype() + + def get_url(self, directory: ServedDirectory) -> str: + return reverse('file', args=(directory.id, self.filename)) + + def get_mediatype(self) -> str: + """Simple media type categorization based on the given mimetype""" + if os.path.exists(self.fullpath): + if os.path.isdir(self.fullpath): + return 'folder' + mimetype = mimetypes.guess_type(self.filename)[0] + if mimetype is not None: + if mimetype.startswith('image'): + return 'image' + elif mimetype.startswith('video'): + return 'video' + return 'file' + return 'unknown' + + def __str__(self) -> str: + return self.filename + + +def extra_listdir(path: str) -> List[File]: """ Helper function used for identifying file media type for every file in a given directory, extending os.listdir @@ -11,11 +43,7 @@ def extra_listdir(path: str) -> List[Tuple[str, str]]: """ files = [] for file in os.listdir(path): - mediatype = get_all_mediatype(file, path) - if mediatype == 'folder': - files.append((file, mediatype, os.path.join(path, file))) - else: - files.append((file, mediatype)) + files.append(File(file, path)) return files @@ -30,13 +58,3 @@ def get_all_mediatype(head: str, tail: str) -> str: 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'): - return 'image' - elif mimetype.startswith('video'): - return 'video' - return 'file' diff --git a/viewer/templates/browse.html b/viewer/templates/browse.html index 5cbecf4..e823b57 100644 --- a/viewer/templates/browse.html +++ b/viewer/templates/browse.html @@ -1,5 +1,52 @@ {% extends 'base.html' %} +{% block head %} + {{ block.super }} + +{% endblock head %} {% block content %} +
+
+

+ {{ directory.path }} + + {{ files|length }} files + +

+
+
+
+ {% for directory in directories %} +
+ +
+ + {% endfor %} + {% for file in files %} +
+ {% load thumbnail %} + +{# #} + + + {{ file.filename }} + + +
+ {% endfor %} +
+
+
+
{{ directory.path }} @@ -15,16 +62,15 @@ {% for file in files %}