diff --git a/Dockerfile b/Dockerfile index e29bb9d..2c81f8e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,6 +14,7 @@ WORKDIR /app COPY requirements.txt . COPY app.py . +COPY index.html ./templates/index.html RUN pip install --no-cache-dir -r requirements.txt diff --git a/app.py b/app.py index f3ae6a7..f6532d2 100644 --- a/app.py +++ b/app.py @@ -1,36 +1,72 @@ -from flask import Flask, request, jsonify import os +from flask import Flask, request, jsonify, send_file, render_template app = Flask(__name__) -app.config['UPLOAD_FOLDER'] = 'uploads/' -app.config['MAX_CONTENT_LENGTH'] = 5 * 1024 * 1024 # 5MB limit -@app.route('/upload', methods=['POST']) -def upload_file(): - file = request.files['file'] - if file: - filename = file.filename - if not os.path.exists(app.config['UPLOAD_FOLDER']): - os.makedirs(app.config['UPLOAD_FOLDER']) - file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) - return 'File uploaded successfully' - else: - return 'No file specified', 400 +UPLOAD_DIRECTORY = os.environ.get("UPLOAD_DIRECTORY", "./uploads") +UPLOAD_TOKEN = os.environ.get("UPLOAD_TOKEN") -@app.route('/files', methods=['GET']) -def list_files(): +@app.route("/upload", methods=["POST"]) +def upload(): + if UPLOAD_TOKEN and "Token" not in request.headers: + return jsonify({"message": "No upload token provided"}), 401 + + token = request.headers.get("Token") + if UPLOAD_TOKEN and token != UPLOAD_TOKEN: + return jsonify({"message": "Invalid upload token"}), 401 + + if "file" not in request.files: + return jsonify({"message": "No file found"}), 400 + + file = request.files["file"] + if file.filename == "": + return jsonify({"message": "No file found"}), 400 + + if not os.path.exists(UPLOAD_DIRECTORY): + os.makedirs(UPLOAD_DIRECTORY) + + file.save(os.path.join(UPLOAD_DIRECTORY, file.filename)) + + return jsonify({"message": "File saved successfully"}), 200 + +@app.route("/download/", methods=["GET"]) +def download(filename): + file_path = os.path.join(UPLOAD_DIRECTORY, filename) + if not os.path.exists(file_path): + return jsonify({"message": "File not found"}), 404 + + return send_file(file_path, as_attachment=True) + +@app.route("/", methods=["GET"]) +def index(): files = [] - for filename in os.listdir(app.config['UPLOAD_FOLDER']): - path = os.path.join(app.config['UPLOAD_FOLDER'], filename) - if os.path.isfile(path): - files.append(filename) - return jsonify(files) + for filename in os.listdir(UPLOAD_DIRECTORY): + filepath = os.path.join(UPLOAD_DIRECTORY, filename) + if os.path.isfile(filepath): + files.append({"name": filename, "size": os.path.getsize(filepath)}) + return render_template("index.html", files=files) + +@app.route("/metrics", methods=["GET"]) +def metrics(): + files = os.listdir(UPLOAD_DIRECTORY) + file_count = len(files) + total_size = sum(os.path.getsize(os.path.join(UPLOAD_DIRECTORY, f)) for f in files) + file_list = [{"name": f, "size": os.path.getsize(os.path.join(UPLOAD_DIRECTORY, f))} for f in files] + return jsonify({"count": file_count, "total_size": total_size, "files": file_list}), 200 + + + + +# Upload +# curl -X POST -H "Authorization: Bearer myuploadtoken" -F "file=@/path/to/file" http://docker10.grote.lan:5040/upload +# Download +# curl -X GET http://docker10.grote.lan:5040/download/filename.ext > filename.ext +# List +#$ curl -X GET http://docker10.grote.lan:5040/files +# Metriken +# curl -X GET http://docker10.grote.lan:5040/metrics -if __name__ == '__main__': - app.run() -# curl -X POST -F 'file=@/path/to/file' http://localhost:5000/upload -# curl http://localhost:5000/files diff --git a/docker-compose.yml b/docker-compose.yml index 159fd0a..d8a2721 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,3 +9,5 @@ services: env: FLASK_DEBUG: 1 FLASK_APP: app + UPLOAD_DIRECTORY: /uploads + UPLOAD_TOKEN: myuploadtoken diff --git a/index.html b/index.html new file mode 100644 index 0000000..03fb046 --- /dev/null +++ b/index.html @@ -0,0 +1,18 @@ + + + + File List + + +

File List

+ {% if files %} + + {% else %} +

No files found.

+ {% endif %} + +