commit c9483141d4db69b877ddeba752cb9a82e461d860 Author: Dendy Faist Date: Tue Jan 23 17:31:46 2024 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5db59f5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +__pycache__ +config.toml 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.example b/config.toml.example new file mode 100644 index 0000000..5244fd5 --- /dev/null +++ b/config.toml.example @@ -0,0 +1,8 @@ +name = "Folder share" +base_path = "/path/to/folder" +favicon = "/.favicon.png" + +[qbittorrent] +base_url = "http://127.0.0.1:8080" +username = "admin" +password = "adminadmin" 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 %}