diff options
| author | Michael Grote | 2023-04-13 11:03:56 +0200 |
|---|---|---|
| committer | Michael Grote | 2023-04-13 11:03:56 +0200 |
| commit | afe29d20d285aee13ad025688355e4afd46a430c (patch) | |
| tree | 575eb6c64889c84fe4fbc873288b9c1e3cdc56ec | |
| parent | 6b9d6bc0ac139ff28548e5ba76a73fed62a07a67 (diff) | |
add new endpoints
| -rw-r--r-- | Dockerfile | 1 | ||||
| -rw-r--r-- | app.py | 90 | ||||
| -rw-r--r-- | docker-compose.yml | 2 | ||||
| -rw-r--r-- | index.html | 18 |
4 files changed, 84 insertions, 27 deletions
@@ -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 @@ -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 - -@app.route('/files', methods=['GET']) -def list_files(): + +UPLOAD_DIRECTORY = os.environ.get("UPLOAD_DIRECTORY", "./uploads") +UPLOAD_TOKEN = os.environ.get("UPLOAD_TOKEN") + +@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/<filename>", 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 @@ +<!DOCTYPE html> +<html> +<head> + <title>File List</title> +</head> +<body> + <h1>File List</h1> + {% if files %} + <ul> + {% for file in files %} + <li><a href="{{ url_for('download', filename=file.name) }}">{{ file.name }} ({{ file.size }} bytes)</a></li> + {% endfor %} + </ul> + {% else %} + <p>No files found.</p> + {% endif %} +</body> +</html> |