diff --git a/viewer/helpers.py b/viewer/helpers.py index 02cf123..101871b 100644 --- a/viewer/helpers.py +++ b/viewer/helpers.py @@ -1,5 +1,5 @@ import os -from typing import Tuple, List +from typing import List, Tuple def extra_listdir(path: str) -> List[Tuple[str, str]]: @@ -9,7 +9,14 @@ def extra_listdir(path: str) -> List[Tuple[str, str]]: :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)] + 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)) + return files def get_all_mediatype(head: str, tail: str) -> str: diff --git a/viewer/templates/add.html b/viewer/templates/add.html new file mode 100644 index 0000000..f5d8de9 --- /dev/null +++ b/viewer/templates/add.html @@ -0,0 +1,64 @@ +{% extends 'base.html' %} +{% block content %} +
+
+

+ Add New Directory +

+
+
+
+

+ Adds a new Directory to be served by the server. +
+ If specified, a RegEx pattern can be used to filter files and will only display the ones you want. +
+ RegEx patterns can be configured below to match against the entire path or just the filename. +
+ Additionally, files can be matched recursively if you so wish. Please note that this functionality may create anomalous behavior around duplicate files. +

+
+ {% csrf_token %} +
+ +
+ + + + +
+

+ We recommend that you use a full path. Do not escape the path or use a relative path for best results. +

+
+
+ +
+ + + + +
+

+ This is optional. Do not enter anything if you wish to disable RegEx matching and simply add all files. +

+
+ + + +
+
+ +
+
+
+
+
+
+{% endblock content %} diff --git a/viewer/templates/base.html b/viewer/templates/base.html index e03a51c..8a2e8db 100644 --- a/viewer/templates/base.html +++ b/viewer/templates/base.html @@ -20,7 +20,9 @@ diff --git a/viewer/templates/browse.html b/viewer/templates/browse.html index dde12aa..5cbecf4 100644 --- a/viewer/templates/browse.html +++ b/viewer/templates/browse.html @@ -2,9 +2,9 @@ {% block content %}
- Files + {{ directory.path }} - {{ files|length }} + {{ files|length }} files @@ -17,13 +17,22 @@ - - {{ file.0 }} - + {% if file.1 == 'folder' %} + + {{ file.0 }} + + {% else %} + + {{ file.0 }} + {{ file.0 }} + + {% endif %}
{% endfor %}
- + {% load static %} {% endblock content %} diff --git a/viewer/templates/index.html b/viewer/templates/index.html index e07255f..e380830 100644 --- a/viewer/templates/index.html +++ b/viewer/templates/index.html @@ -5,6 +5,12 @@ .panel-icon { margin-right: 1em; } + + .tag:not(body) { + font-size: 0.7rem; + line-height: 1.3; + padding: 0 0.6em; + } {% endblock head %} {% block content %} @@ -17,6 +23,9 @@ {{ served_directory.path }} + {% if served_directory.regex %} + Filtered + {% endif %} {% endfor %} diff --git a/viewer/urls.py b/viewer/urls.py index 2b2acb8..a479eec 100644 --- a/viewer/urls.py +++ b/viewer/urls.py @@ -4,6 +4,8 @@ from . import views urlpatterns = [ path('', views.index, name='index'), - path('//', views.browse, name='browse'), - path('///', views.file, name='file') + path('add/', views.add, name='add'), + path('add/submit', views.submit_new, name='add_submit'), + path('/', views.browse, name='browse'), + path('//', views.file, name='file') ] diff --git a/viewer/views.py b/viewer/views.py index 00b7aa3..712ed85 100644 --- a/viewer/views.py +++ b/viewer/views.py @@ -1,7 +1,8 @@ import os -from django.http import FileResponse +from django.http import FileResponse, HttpResponseRedirect from django.shortcuts import render, get_object_or_404 +from django.urls import reverse from viewer.helpers import extra_listdir from viewer.models import ServedDirectory @@ -54,3 +55,35 @@ def file(request, directory_id, file): ) } return render(request, 'message.html', context, status=500) + + +def add(request): + context = {'title': 'Add New Directory'} + if 'path' in request.GET.keys(): + context['path_prefill'] = request.GET['path'] + return render(request, 'add.html', context) + + +def submit_new(request): + try: + s = ServedDirectory( + path=request.POST['path'], + regex_pattern=request.POST.get('regex'), + regex=request.POST.get('regex') is not None, + match_filename=request.POST.get('match_filename', False), + recursive=request.POST.get('recursive', False) + ) + if not os.path.isdir(request.POST['path']): + raise ValueError('A invalid Directory was specified in the request.') + s.save() + except KeyError: + return render(request, 'message.html', status=403, + context={'title': 'Invalid Options', + 'message': 'The POST request you sent did not have the options required to complete ' + 'the request.'}) + except ValueError: + return render(request, 'message.html', status=400, + context={'title': 'Invalid Directory', + 'message': 'The directory you specified was not a valid directory, either it doesn\'t ' + 'exist or it isn\'t a directory.'}) + return HttpResponseRedirect(reverse('browse', args=(s.id,)))