diff --git a/app.py b/app.py
index 14ba9a6..4ea72fe 100644
--- a/app.py
+++ b/app.py
@@ -1,79 +1,59 @@
import os
-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'}
+import uuid
+from flask import Flask, request, jsonify, send_from_directory, render_template
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):
- return '.' in filename and \
- filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
+ return True
-@app.route('/')
+@app.route("/")
def index():
- file_list = os.listdir(app.config['UPLOAD_FOLDER'])
- return """
-
-
-
-
- File List
-
-
- File List
-
-
-
- """ % ''.join(f'{filename}' for filename in file_list)
+ files = []
+ for filename in os.listdir(UPLOAD_DIRECTORY):
+ path = os.path.join(UPLOAD_DIRECTORY, filename)
+ if os.path.isfile(path):
+ files.append({"filename": filename, "size": os.path.getsize(path)})
+ total_size = sum(f["size"] for f in files)
+ return render_template("index.html", files=files, total_size=total_size, count=len(files))
-@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("/upload", methods=["POST"])
+def upload():
+ if "file" not in request.files:
+ return "No file found", 400
+ file = request.files["file"]
+ if file.filename == "":
+ return "No file selected", 400
+ if not allowed_file(file.filename):
+ return "Invalid file type", 400
+ if UPLOAD_TOKEN and request.headers.get("Authorization") != f"Bearer {UPLOAD_TOKEN}":
+ return "Unauthorized", 401
+ filename = str(uuid.uuid4())
+ file.save(os.path.join(UPLOAD_DIRECTORY, filename))
+ return jsonify({"filename": filename})
-@app.route('/download/', 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("/download/", methods=["GET"])
+def download(filename):
+ return send_from_directory(UPLOAD_DIRECTORY, filename)
-@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)
+@app.route("/metrics")
+def metrics():
+ files = []
+ for filename in os.listdir(UPLOAD_DIRECTORY):
+ path = os.path.join(UPLOAD_DIRECTORY, filename)
+ if os.path.isfile(path):
+ files.append({"filename": filename, "size": os.path.getsize(path)})
+ total_size = sum(f["size"] for f in files)
+ return jsonify({"count": len(files), "total_size": total_size, "files": files})
+if __name__ == "__main__":
+ app.run(host="0.0.0.0", port=5040, debug=True)
diff --git a/docker-compose.yml b/docker-compose.yml
index 7dfc74a..c720b39 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -1,7 +1,7 @@
version: '3'
services:
python-api-server:
- image: quotengrote/python-api-server
+ image: quotengrote/python-api-server:latest
ports:
- "5040:5000"
volumes: