mirror of
https://github.com/Xevion/simple-viewer.git
synced 2025-12-06 01:16:24 -06:00
basic site design, directory path reading, uuid setup, file icon mimetype categorization
This commit is contained in:
8
viewer/helpers.py
Normal file
8
viewer/helpers.py
Normal file
@@ -0,0 +1,8 @@
|
||||
def get_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'
|
||||
37
viewer/templates/base.html
Normal file
37
viewer/templates/base.html
Normal file
@@ -0,0 +1,37 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
{% block head %}
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>{{ title }}</title>
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.1/css/bulma.min.css">
|
||||
{% endblock head %}
|
||||
</head>
|
||||
<body class="has-navbar-fixed-top">
|
||||
<nav class="navbar is-fixed-top" role="navigation" aria-label="main navigation">
|
||||
<div class="navbar-menu">
|
||||
<div class="navbar-start">
|
||||
<a class="navbar-item" href="{% url 'index' %}">
|
||||
Index
|
||||
</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-end">
|
||||
<div class="navbar-item">
|
||||
<span class="icon">
|
||||
<i class="fas fa-plus"></i>
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
<div class="columns is-centered my-5">
|
||||
<div class="column is-two-thirds">
|
||||
{% block content %}
|
||||
{% endblock %}
|
||||
</div>
|
||||
</div>
|
||||
<script defer src="https://use.fontawesome.com/releases/v5.14.0/js/all.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
24
viewer/templates/browse.html
Normal file
24
viewer/templates/browse.html
Normal file
@@ -0,0 +1,24 @@
|
||||
{% extends 'base.html' %}
|
||||
{% block content %}
|
||||
<div class="panel">
|
||||
<div class="panel-heading">
|
||||
Files
|
||||
<span style="font-weight: 400; font-style: italic; font-size: 70%;">
|
||||
{{ files|length }}
|
||||
</span>
|
||||
<span style="vertical-align: middle;" class="panel-icon">
|
||||
<a href="{% url 'index' %}">
|
||||
<i class="fas fa-arrow-up" aria-hidden="true"></i>
|
||||
</a>
|
||||
</span>
|
||||
</div>
|
||||
{% for file in files %}
|
||||
<div class="panel-block">
|
||||
<span class="panel-icon pr-4">
|
||||
<i class="fas fa-{{ file.1 }} fa-lg" aria-hidden="true"></i>
|
||||
</span>
|
||||
{{ file.0 }}
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
{% endblock content %}
|
||||
24
viewer/templates/index.html
Normal file
24
viewer/templates/index.html
Normal file
@@ -0,0 +1,24 @@
|
||||
{% extends 'base.html' %}
|
||||
{% block head %}
|
||||
{{ block.super }}
|
||||
<style>
|
||||
.panel-icon {
|
||||
margin-right: 1em;
|
||||
}
|
||||
</style>
|
||||
{% endblock head %}
|
||||
{% block content %}
|
||||
<div class="panel">
|
||||
<div class="panel-heading">Directories</div>
|
||||
{% for served_directory in directories %}
|
||||
<div class="panel-block">
|
||||
<span class="panel-icon">
|
||||
<i class="fas fa-folder fa-lg" aria-hidden="true"></i>
|
||||
</span>
|
||||
<a href="{% url 'browse' served_directory.id %}">
|
||||
{{ served_directory.path }}
|
||||
</a>
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
{% endblock content %}
|
||||
15
viewer/templates/message.html
Normal file
15
viewer/templates/message.html
Normal file
@@ -0,0 +1,15 @@
|
||||
{% extends 'base.html' %}
|
||||
{% block content %}
|
||||
<div class="card">
|
||||
<header class="card-header">
|
||||
<p class="card-header-title">
|
||||
{{ title }}
|
||||
</p>
|
||||
</header>
|
||||
<div class="card-content">
|
||||
<div class="content">
|
||||
{{ message }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock content %}
|
||||
@@ -3,5 +3,6 @@ from django.urls import path
|
||||
from . import views
|
||||
|
||||
urlpatterns = [
|
||||
path('', views.index, name='index')
|
||||
path('', views.index, name='index'),
|
||||
path('/<uuid:directory_id>/', views.browse, name='browse'),
|
||||
]
|
||||
|
||||
@@ -1,7 +1,39 @@
|
||||
from django.shortcuts import render
|
||||
import mimetypes
|
||||
import os
|
||||
|
||||
from django.http import FileResponse
|
||||
from django.shortcuts import render, get_object_or_404
|
||||
|
||||
from viewer.helpers import get_mediatype
|
||||
from viewer.models import ServedDirectory
|
||||
|
||||
from django.http import HttpResponse
|
||||
|
||||
def index(request):
|
||||
"""Index view for the simple-viewer project."""
|
||||
return HttpResponse('Hello, World.')
|
||||
directories = ServedDirectory.objects.all()
|
||||
context = {'title': 'Index',
|
||||
'directories': directories}
|
||||
return render(request, 'index.html', context)
|
||||
|
||||
|
||||
def browse(request, directory_id):
|
||||
dir = get_object_or_404(ServedDirectory, id=directory_id)
|
||||
|
||||
if os.path.isdir(dir.path):
|
||||
files = [
|
||||
(file, get_mediatype(mimetypes.guess_type(file)[0])) for file in os.listdir(dir.path)
|
||||
]
|
||||
context = {
|
||||
'title': f'Browse - {os.path.dirname(dir.path)}',
|
||||
'files': files,
|
||||
'directory': dir
|
||||
}
|
||||
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'
|
||||
)
|
||||
}
|
||||
return render(request, 'message.html', context, status=500)
|
||||
|
||||
Reference in New Issue
Block a user