mirror of
https://github.com/Xevion/simple-viewer.git
synced 2025-12-05 23:16:23 -06:00
added manual refresh functionality, fixed mistake with meaning of File directory field (foreign key not path)
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
# Generated by Django 3.1.2 on 2020-11-01 01:46
|
||||
|
||||
from django.db import migrations
|
||||
import jsonfield.fields
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('viewer', '0004_file'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='serveddirectory',
|
||||
name='known_subdirectories',
|
||||
field=jsonfield.fields.JSONField(default=[], verbose_name='Tracked Subdirectories JSON'),
|
||||
),
|
||||
]
|
||||
@@ -1,4 +1,3 @@
|
||||
import json
|
||||
import mimetypes
|
||||
import os
|
||||
import uuid
|
||||
@@ -43,7 +42,7 @@ class ServedDirectory(models.Model):
|
||||
entry = self.files.filter(filename__exact=file).first()
|
||||
if entry is None:
|
||||
# create the file entry
|
||||
entry = File.create(full_path=file_path)
|
||||
entry = File.create(full_path=file_path, parent=self)
|
||||
entry.save()
|
||||
else:
|
||||
# directory found, remember it
|
||||
@@ -52,7 +51,6 @@ class ServedDirectory(models.Model):
|
||||
# Dump subdirectories found
|
||||
self.known_subdirectories = directories
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return self.path
|
||||
|
||||
@@ -64,13 +62,13 @@ class File(models.Model):
|
||||
directory = models.ForeignKey(ServedDirectory, on_delete=models.CASCADE, related_name='files')
|
||||
|
||||
@classmethod
|
||||
def create(cls, full_path: str) -> 'File':
|
||||
def create(cls, full_path: str, parent: ServedDirectory) -> 'File':
|
||||
"""Simple shortcut for creating a File database entry with just the path."""
|
||||
return File(
|
||||
path=full_path,
|
||||
filename=os.path.basename(full_path),
|
||||
mediatype=File.get_mediatype(full_path),
|
||||
directory=os.path.dirname(full_path)
|
||||
directory=parent
|
||||
)
|
||||
|
||||
def get_url(self, directory: ServedDirectory) -> str:
|
||||
|
||||
@@ -21,6 +21,11 @@
|
||||
<span class="pl-1" style="font-weight: 400; font-style: italic; font-size: 70%;">
|
||||
{{ files|length }} files
|
||||
</span>
|
||||
<span class="icon">
|
||||
<a href="{% url 'refresh' directory.id %}">
|
||||
<i class="fas fa-sync"></i>
|
||||
</a>
|
||||
</span>
|
||||
</p>
|
||||
</div>
|
||||
<div class="card-content">
|
||||
|
||||
@@ -7,5 +7,6 @@ urlpatterns = [
|
||||
path('add/', views.add, name='add'),
|
||||
path('add/submit', views.submit_new, name='add_submit'),
|
||||
path('<uuid:directory_id>/', views.browse, name='browse'),
|
||||
path('<uuid:directory_id>/<str:file>/', views.file, name='file')
|
||||
path('<uuid:directory_id>/<str:file>/', views.file, name='file'),
|
||||
path('<uuid:directory_id>/refresh', views.refresh, name='refresh')
|
||||
]
|
||||
|
||||
@@ -4,7 +4,6 @@ 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
|
||||
|
||||
|
||||
@@ -17,29 +16,29 @@ def index(request):
|
||||
|
||||
|
||||
def browse(request, directory_id):
|
||||
dir = get_object_or_404(ServedDirectory, id=directory_id)
|
||||
directory = get_object_or_404(ServedDirectory, id=directory_id)
|
||||
|
||||
if os.path.isdir(dir.path):
|
||||
if os.path.isdir(directory.path):
|
||||
context = {
|
||||
'title': f'Browse - {os.path.dirname(dir.path)}',
|
||||
'files': extra_listdir(dir.path),
|
||||
'directory': dir
|
||||
'title': f'Browse - {os.path.dirname(directory.path)}',
|
||||
'files': directory.files.all(),
|
||||
'directory': directory
|
||||
}
|
||||
return render(request, 'browse.html', context)
|
||||
else:
|
||||
context = {
|
||||
'title': 'Invalid Directory',
|
||||
'message': 'The path this server directory points to {}.'.format(
|
||||
'exists, but is not a directory' if os.path.exists(dir.path) else 'does not exist'
|
||||
'exists, but is not a directory' if os.path.exists(directory.path) else 'does not exist'
|
||||
)
|
||||
}
|
||||
return render(request, 'message.html', context, status=500)
|
||||
|
||||
|
||||
def file(request, directory_id, file):
|
||||
dir = get_object_or_404(ServedDirectory, id=directory_id)
|
||||
if os.path.isdir(dir.path):
|
||||
path = os.path.join(dir.path, file)
|
||||
directory = get_object_or_404(ServedDirectory, id=directory_id)
|
||||
if os.path.isdir(directory.path):
|
||||
path = os.path.join(directory.path, file)
|
||||
if os.path.exists(path):
|
||||
return FileResponse(open(path, 'rb'))
|
||||
else:
|
||||
@@ -51,7 +50,7 @@ def file(request, directory_id, file):
|
||||
context = {
|
||||
'title': 'Invalid Directory',
|
||||
'message': 'The path this server directory points to {}.'.format(
|
||||
'exists, but is not a directory' if os.path.exists(dir.path) else 'does not exist'
|
||||
'exists, but is not a directory' if os.path.exists(directory.path) else 'does not exist'
|
||||
)
|
||||
}
|
||||
return render(request, 'message.html', context, status=500)
|
||||
@@ -64,6 +63,13 @@ def add(request):
|
||||
return render(request, 'add.html', context)
|
||||
|
||||
|
||||
def refresh(request, directory_id):
|
||||
"""A simple API view for refreshing a directory. May schedule new thumbnail generation."""
|
||||
directory = get_object_or_404(ServedDirectory, id=directory_id)
|
||||
directory.refresh()
|
||||
return HttpResponseRedirect(reverse('browse', args=(directory.id,)))
|
||||
|
||||
|
||||
def submit_new(request):
|
||||
try:
|
||||
s = ServedDirectory(
|
||||
|
||||
Reference in New Issue
Block a user