basic file sending/viewing

This commit is contained in:
Xevion
2020-10-31 12:39:28 -05:00
parent c8fcb09b73
commit 8cbf8396fd
3 changed files with 25 additions and 1 deletions

View File

@@ -17,7 +17,9 @@
<span class="panel-icon pr-4">
<i class="fas fa-{{ file.1 }} fa-lg" aria-hidden="true"></i>
</span>
{{ file.0 }}
<a href="{% url 'file' directory.id file.0 %}">
{{ file.0 }}
</a>
</div>
{% endfor %}
</div>

View File

@@ -5,4 +5,5 @@ from . import views
urlpatterns = [
path('', views.index, name='index'),
path('/<uuid:directory_id>/', views.browse, name='browse'),
path('/<uuid:directory_id>/<str:file>/', views.file, name='file')
]

View File

@@ -37,3 +37,24 @@ def browse(request, directory_id):
)
}
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)
if os.path.exists(path):
return FileResponse(open(path, 'rb'))
else:
context = {
'title': 'Invalid File',
'message': 'The file requested from this directory was not found on the server.'
}
return render(request, 'message.html', context, status=500)
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'
)
}
return render(request, 'message.html', context, status=500)