fix upload
This commit is contained in:
parent
b90e9ac724
commit
23094c6981
2 changed files with 46 additions and 66 deletions
110
app.py
110
app.py
|
@ -1,79 +1,59 @@
|
||||||
import os
|
import os
|
||||||
from flask import Flask, request, redirect, url_for, send_from_directory, abort
|
import uuid
|
||||||
from werkzeug.utils import secure_filename
|
from flask import Flask, request, jsonify, send_from_directory, render_template
|
||||||
|
|
||||||
UPLOAD_FOLDER = os.environ.get('UPLOAD_FOLDER', '/app/uploads')
|
|
||||||
ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'}
|
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
|
||||||
|
UPLOAD_DIRECTORY = os.environ.get("UPLOAD_DIRECTORY", "/uploads")
|
||||||
|
if not os.path.exists(UPLOAD_DIRECTORY):
|
||||||
|
os.makedirs(UPLOAD_DIRECTORY)
|
||||||
|
|
||||||
|
UPLOAD_TOKEN = os.environ.get("UPLOAD_TOKEN")
|
||||||
|
|
||||||
def allowed_file(filename):
|
def allowed_file(filename):
|
||||||
return '.' in filename and \
|
return True
|
||||||
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
|
||||||
|
|
||||||
@app.route('/')
|
@app.route("/")
|
||||||
def index():
|
def index():
|
||||||
file_list = os.listdir(app.config['UPLOAD_FOLDER'])
|
files = []
|
||||||
return """
|
for filename in os.listdir(UPLOAD_DIRECTORY):
|
||||||
<!doctype html>
|
path = os.path.join(UPLOAD_DIRECTORY, filename)
|
||||||
<html lang=en>
|
if os.path.isfile(path):
|
||||||
<head>
|
files.append({"filename": filename, "size": os.path.getsize(path)})
|
||||||
<meta charset=utf-8>
|
total_size = sum(f["size"] for f in files)
|
||||||
<title>File List</title>
|
return render_template("index.html", files=files, total_size=total_size, count=len(files))
|
||||||
</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('/upload', methods=['POST'])
|
@app.route("/upload", methods=["POST"])
|
||||||
def upload_file():
|
def upload():
|
||||||
# check if the post request has the file part
|
if "file" not in request.files:
|
||||||
if 'file' not in request.files:
|
return "No file found", 400
|
||||||
return redirect(request.url)
|
file = request.files["file"]
|
||||||
file = request.files['file']
|
if file.filename == "":
|
||||||
# if user does not select file, browser also
|
return "No file selected", 400
|
||||||
# submit an empty part without filename
|
if not allowed_file(file.filename):
|
||||||
if file.filename == '':
|
return "Invalid file type", 400
|
||||||
return redirect(request.url)
|
if UPLOAD_TOKEN and request.headers.get("Authorization") != f"Bearer {UPLOAD_TOKEN}":
|
||||||
if file and allowed_file(file.filename):
|
return "Unauthorized", 401
|
||||||
filename = secure_filename(file.filename)
|
filename = str(uuid.uuid4())
|
||||||
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
|
file.save(os.path.join(UPLOAD_DIRECTORY, filename))
|
||||||
return 'file uploaded successfully'
|
return jsonify({"filename": filename})
|
||||||
else:
|
|
||||||
return 'invalid file type'
|
|
||||||
|
|
||||||
@app.route('/download/<filename>', methods=['GET'])
|
@app.route("/download/<filename>", methods=["GET"])
|
||||||
def download_file(filename):
|
def download(filename):
|
||||||
try:
|
return send_from_directory(UPLOAD_DIRECTORY, filename)
|
||||||
return send_from_directory(app.config['UPLOAD_FOLDER'], filename, as_attachment=True)
|
|
||||||
except FileNotFoundError:
|
|
||||||
abort(404)
|
|
||||||
|
|
||||||
@app.route('/metrics', methods=['GET'])
|
@app.route("/metrics")
|
||||||
def get_metrics():
|
def metrics():
|
||||||
file_list = os.listdir(app.config['UPLOAD_FOLDER'])
|
files = []
|
||||||
file_count = len(file_list)
|
for filename in os.listdir(UPLOAD_DIRECTORY):
|
||||||
total_size = 0
|
path = os.path.join(UPLOAD_DIRECTORY, filename)
|
||||||
for filename in file_list:
|
if os.path.isfile(path):
|
||||||
total_size += os.path.getsize(os.path.join(app.config['UPLOAD_FOLDER'], filename))
|
files.append({"filename": filename, "size": os.path.getsize(path)})
|
||||||
return {
|
total_size = sum(f["size"] for f in files)
|
||||||
'file_count': file_count,
|
return jsonify({"count": len(files), "total_size": total_size, "files": files})
|
||||||
'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)
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
app.run(host="0.0.0.0", port=5040, debug=True)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
version: '3'
|
version: '3'
|
||||||
services:
|
services:
|
||||||
python-api-server:
|
python-api-server:
|
||||||
image: quotengrote/python-api-server
|
image: quotengrote/python-api-server:latest
|
||||||
ports:
|
ports:
|
||||||
- "5040:5000"
|
- "5040:5000"
|
||||||
volumes:
|
volumes:
|
||||||
|
|
Reference in a new issue