From 81ae0021e28e7dc25ef4c3af63acf523642dbfe7 Mon Sep 17 00:00:00 2001 From: Dendy Faist Date: Tue, 23 Jan 2024 17:31:46 +0100 Subject: [PATCH] Initial commit --- .gitignore | 1 + app.py | 6 ++++ config.toml | 8 +++++ src/__init__.py | 63 ++++++++++++++++++++++++++++++++++++++++ static/style/main.css | 17 +++++++++++ templates/base.html | 22 ++++++++++++++ templates/directory.html | 21 ++++++++++++++ templates/error.html | 9 ++++++ 8 files changed, 147 insertions(+) create mode 100644 .gitignore create mode 100644 app.py create mode 100644 config.toml create mode 100644 src/__init__.py create mode 100644 static/style/main.css create mode 100644 templates/base.html create mode 100644 templates/directory.html create mode 100644 templates/error.html diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bee8a64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__ diff --git a/app.py b/app.py new file mode 100644 index 0000000..bcf32e2 --- /dev/null +++ b/app.py @@ -0,0 +1,6 @@ +#!/usr/bin/env python3 + +from src import app + +if __name__ == "__main__": + app.run(debug=True) diff --git a/config.toml b/config.toml new file mode 100644 index 0000000..6877fc4 --- /dev/null +++ b/config.toml @@ -0,0 +1,8 @@ +name = "film.lidiarock.one" +base_path = "/mnt/hakodate/cul" +favicon = "/.favnikon.png" + +[qbittorrent] +base_url = "http://127.0.0.1:8080" +username = "" +password = "" diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..13835f6 --- /dev/null +++ b/src/__init__.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python3 + +import os +import tomllib +from flask import Flask, render_template, send_file + +with open("config.toml", "rb") as f: + config = tomllib.load(f) + +app = Flask( + __name__, + template_folder="../templates", + static_folder="../static", +) + +app.jinja_env.globals.update( + config=config, + path_join=os.path.join, +) + +# Catch all + + +@app.route('/', defaults={'path': ''}) +@app.route('/') +def index(path): + internal_path = os.path.join(config['base_path'], path) + path = '/' + path + + # Checks ################################################### + + if '..' in path: + return 'Path cannot contain double dots, i.e. "..".' + + if not os.path.exists(internal_path): + return ( + render_template( + "error.html", + code=404, + msg=f'The path "{path}" does not exist.' + ), + 404, + ) + + # Actual serving ########################################### + + if os.path.isdir(internal_path): + _, dirs, files = next(os.walk(internal_path)) + files = filter(lambda x: not x.startswith("."), files) + dirs = filter(lambda x: not x.startswith("."), dirs) + + return ( + render_template( + "directory.html", + path=path, + dirs=dirs, + files=files, + ), + 200, + ) + + # Path exists, not a folder, must be a file, send + return send_file(internal_path) diff --git a/static/style/main.css b/static/style/main.css new file mode 100644 index 0000000..5c11865 --- /dev/null +++ b/static/style/main.css @@ -0,0 +1,17 @@ +body { + background-color: #191919; + color: white; + font-family: monospace; +} + +a { + text-decoration: none; + color: lightgrey; +} + a:visited { + color: grey; + } + a:hover { + color: #191919; + background-color: white; + } diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 0000000..a536865 --- /dev/null +++ b/templates/base.html @@ -0,0 +1,22 @@ + + + + + + + + + {% block title %}{% endblock %} - {{ config["name"] }} + + + + + {# #} + + + {% block body %}{% endblock %} + + diff --git a/templates/directory.html b/templates/directory.html new file mode 100644 index 0000000..cb29d7d --- /dev/null +++ b/templates/directory.html @@ -0,0 +1,21 @@ +{% extends 'base.html' %} + +{% block title %}{{ path }}{% endblock %} + +{% block body %} +<-- +

+ +{% for dir in dirs %} +{{ dir }}
+{% endfor %} + +

+ +

Archivos

+ +{% for file in files %} +{{ file }}
+{% endfor %} + +{% endblock %} diff --git a/templates/error.html b/templates/error.html new file mode 100644 index 0000000..f9dad71 --- /dev/null +++ b/templates/error.html @@ -0,0 +1,9 @@ +{% extends 'base.html' %} + +{% block title %}Error {{ code }}{% endblock %} + +{% block body %} +

Error {{ code }}

+

{{ msg }}

+Return to Index +{% endblock %}