From 4e4048561f49d73b25a6730fdf21b35c54fd26ce Mon Sep 17 00:00:00 2001 From: Dendy Faist Date: Wed, 14 Feb 2024 12:04:56 +0100 Subject: [PATCH] Mass initial commit --- src/__init__.py | 53 ++++++++++++++++++++++++++++++++++++++++ templates/directory.html | 7 ++++++ 2 files changed, 60 insertions(+) diff --git a/src/__init__.py b/src/__init__.py index f19ee53..83655ee 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -1,6 +1,8 @@ #!/usr/bin/env python3 import os +import sys +import zipfile from flask import Flask, render_template, send_file, request @@ -61,6 +63,57 @@ def index(path): return send_file(internal_path) +@app.route('/mass_download') +def mass_download(): + internal_base = config.get('base_path') + + base = request.args.get('base', '') + files = request.args.getlist('file') + + # Checks ################################################### + + if base == '': + return {"error": "Base path not supplied"}, 400 + + if '..' in base or base[0] != '/': + return {"error": "Invalid base path"}, 400 + + # TODO: If it's 1 file and it's not a directory, then redirect to the file + if len(files) < 1: + return {"error": "A minimum of two files is required"}, 400 + + # TODO: Check if the files exist + # TODO: Check if the files are bigger than a certain amount? + + # Actual serving ########################################### + + for i_file in files: + internal_path = os.path.join(internal_base, base[1:], i_file) + + # FIXME: I'm broke + if os.path.isdir(internal_path): + for root, _, subfiles in os.walk(internal_path): + for i_subfile in subfiles: + external_root = root[len(internal_base) + 1:] + print(external_root) + files.append(os.path.join(root, external_root)) + print(files) + + with zipfile.ZipFile('/tmp/download.zip', 'w') as f_zip: + for i_file in files: + internal_path = os.path.join(internal_base, base[1:], i_file) + + # print(internal_path) + + if not os.path.exists(internal_path): + # And what is this?¿ + return {'error': f'The following path does not exist: "{i_file}"'}, 400 + + f_zip.write(internal_path) + + return [base, files], 200 + + @app.route('/search') def search(): q = request.args.get('q', '') diff --git a/templates/directory.html b/templates/directory.html index 15b60c3..a4f0dc3 100644 --- a/templates/directory.html +++ b/templates/directory.html @@ -15,7 +15,13 @@

{% endif %} +
+ + +
+ {% for dir in dirs %} + {{ dir }}
{% endfor %} @@ -26,6 +32,7 @@ {% endif %} {% for file in files %} + {{ file }}
{% endfor %}