fix download

This commit is contained in:
Michael Grote 2023-04-13 11:29:47 +02:00
parent afe29d20d2
commit b90e9ac724
2 changed files with 72 additions and 51 deletions

121
app.py
View file

@ -1,58 +1,78 @@
import os
from flask import Flask, request, jsonify, send_file, render_template
from flask import Flask, request, redirect, url_for, send_from_directory, abort
from werkzeug.utils import secure_filename
UPLOAD_FOLDER = os.environ.get('UPLOAD_FOLDER', '/app/uploads')
ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'}
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
UPLOAD_DIRECTORY = os.environ.get("UPLOAD_DIRECTORY", "./uploads")
UPLOAD_TOKEN = os.environ.get("UPLOAD_TOKEN")
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@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"])
@app.route('/')
def index():
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)
file_list = os.listdir(app.config['UPLOAD_FOLDER'])
return """
<!doctype html>
<html lang=en>
<head>
<meta charset=utf-8>
<title>File List</title>
</head>
<body>
<h1>File List</h1>
<ul>
%s
</ul>
</body>
</html>
""" % ''.join(f'<li><a href="/download/{filename}">{filename}</a></li>' for filename in file_list)
@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
@app.route('/upload', methods=['POST'])
def upload_file():
# check if the post request has the file part
if 'file' not in request.files:
return redirect(request.url)
file = request.files['file']
# if user does not select file, browser also
# submit an empty part without filename
if file.filename == '':
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return 'file uploaded successfully'
else:
return 'invalid file type'
@app.route('/download/<filename>', methods=['GET'])
def download_file(filename):
try:
return send_from_directory(app.config['UPLOAD_FOLDER'], filename, as_attachment=True)
except FileNotFoundError:
abort(404)
@app.route('/metrics', methods=['GET'])
def get_metrics():
file_list = os.listdir(app.config['UPLOAD_FOLDER'])
file_count = len(file_list)
total_size = 0
for filename in file_list:
total_size += os.path.getsize(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return {
'file_count': file_count,
'total_size': total_size,
'files': [{
'name': filename,
'size': os.path.getsize(os.path.join(app.config['UPLOAD_FOLDER'], filename))
} for filename in file_list]
}
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5040, debug=True)
@ -62,13 +82,14 @@ def metrics():
# Download
# curl -X GET http://docker10.grote.lan:5040/download/filename.ext > filename.ext
# List
#$ curl -X GET http://docker10.grote.lan:5040/files
# curl -X GET http://docker10.grote.lan:5040/files
# Metriken
# curl -X GET http://docker10.grote.lan:5040/metrics
# upload check
# download
# farben im putput weg
# wsgi server

View file

@ -6,7 +6,7 @@ services:
- "5040:5000"
volumes:
- ./uploads:/app/uploads
env:
environment:
FLASK_DEBUG: 1
FLASK_APP: app
UPLOAD_DIRECTORY: /uploads