From 4cc1402d21d2487e8f9698a48375f878bd469909 Mon Sep 17 00:00:00 2001 From: Xevion Date: Sat, 31 Oct 2020 18:03:25 -0500 Subject: [PATCH 1/4] begin working on add directory functionality, finished bulma frontend form design --- viewer/templates/add.html | 64 ++++++++++++++++++++++++++++++++++++++ viewer/templates/base.html | 4 ++- viewer/urls.py | 6 ++-- viewer/views.py | 10 +++++- 4 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 viewer/templates/add.html diff --git a/viewer/templates/add.html b/viewer/templates/add.html new file mode 100644 index 0000000..4caa799 --- /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/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..a34832f 100644 --- a/viewer/views.py +++ b/viewer/views.py @@ -1,6 +1,6 @@ import os -from django.http import FileResponse +from django.http import FileResponse, HttpResponse from django.shortcuts import render, get_object_or_404 from viewer.helpers import extra_listdir @@ -54,3 +54,11 @@ def file(request, directory_id, file): ) } return render(request, 'message.html', context, status=500) + + +def add(request): + return render(request, 'add.html', {'title': 'Add New Directory'}) + + +def submit_new(request): + return HttpResponse('') From 688ffe88ddc428c2ae7c2652e6e1d65916572f7c Mon Sep 17 00:00:00 2001 From: Xevion Date: Sat, 31 Oct 2020 18:34:54 -0500 Subject: [PATCH 2/4] added server side directory adding, validation and redirecting, show directory path and mention 'files' near filecount in panel header --- viewer/templates/add.html | 2 +- viewer/templates/browse.html | 4 ++-- viewer/views.py | 26 ++++++++++++++++++++++++-- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/viewer/templates/add.html b/viewer/templates/add.html index 4caa799..6d69511 100644 --- a/viewer/templates/add.html +++ b/viewer/templates/add.html @@ -17,7 +17,7 @@
Additionally, files can be matched recursively if you so wish. Please note that this functionality may create anomalous behavior around duplicate files.

-
+ {% csrf_token %}
diff --git a/viewer/templates/browse.html b/viewer/templates/browse.html index dde12aa..70e8348 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 diff --git a/viewer/views.py b/viewer/views.py index a34832f..dc68dd1 100644 --- a/viewer/views.py +++ b/viewer/views.py @@ -1,7 +1,8 @@ import os -from django.http import FileResponse, HttpResponse +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 @@ -61,4 +62,25 @@ def add(request): def submit_new(request): - return HttpResponse('') + 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,))) From cda841065ab557e23278cb837cd64cf552399b7a Mon Sep 17 00:00:00 2001 From: Xevion Date: Sat, 31 Oct 2020 18:47:16 -0500 Subject: [PATCH 3/4] added directory prefill shortcut creation --- viewer/helpers.py | 11 +++++++++-- viewer/templates/add.html | 2 +- viewer/templates/browse.html | 17 +++++++++++++---- viewer/views.py | 5 ++++- 4 files changed, 27 insertions(+), 8 deletions(-) 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 index 6d69511..f5d8de9 100644 --- a/viewer/templates/add.html +++ b/viewer/templates/add.html @@ -22,7 +22,7 @@ - + {% load static %} {% endblock content %} diff --git a/viewer/views.py b/viewer/views.py index dc68dd1..712ed85 100644 --- a/viewer/views.py +++ b/viewer/views.py @@ -58,7 +58,10 @@ def file(request, directory_id, file): def add(request): - return render(request, 'add.html', {'title': 'Add New Directory'}) + 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): From db45f6fd7d0c489aa6e461cf66010c755068cb6f Mon Sep 17 00:00:00 2001 From: Xevion Date: Sat, 31 Oct 2020 18:48:46 -0500 Subject: [PATCH 4/4] small filtered tag marker on index page --- viewer/templates/index.html | 9 +++++++++ 1 file changed, 9 insertions(+) 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 %}