add new endpoints

This commit is contained in:
Michael Grote 2023-04-13 11:03:56 +02:00
parent 6b9d6bc0ac
commit afe29d20d2
4 changed files with 82 additions and 25 deletions

View file

@ -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

86
app.py
View file

@ -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/<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

View file

@ -9,3 +9,5 @@ services:
env:
FLASK_DEBUG: 1
FLASK_APP: app
UPLOAD_DIRECTORY: /uploads
UPLOAD_TOKEN: myuploadtoken

18
index.html Normal file
View file

@ -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>