summaryrefslogtreecommitdiffstats
path: root/app.py
diff options
context:
space:
mode:
authorMichael Grote2023-04-13 11:03:56 +0200
committerMichael Grote2023-04-13 11:03:56 +0200
commitafe29d20d285aee13ad025688355e4afd46a430c (patch)
tree575eb6c64889c84fe4fbc873288b9c1e3cdc56ec /app.py
parent6b9d6bc0ac139ff28548e5ba76a73fed62a07a67 (diff)
add new endpoints
Diffstat (limited to 'app.py')
-rw-r--r--app.py90
1 files changed, 63 insertions, 27 deletions
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
-
-@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