import os import re import uuid from flask import Flask, request, jsonify, send_from_directory app = Flask(__name__) app.config['UPLOAD_FOLDER'] = os.environ.get('UPLOAD_FOLDER', '/home/mg/api/python-api-server/uploads') app.config['MAX_CONTENT_LENGTH'] = int(os.environ.get('MAX_CONTENT_LENGTH', '5')) * 1024 * 1024 # in MB VALID_FILENAME_REGEX = r'^[a-zA-Z0-9\-_\.]+$' UPLOAD_TOKEN = os.environ.get('UPLOAD_TOKEN', 'myuploadotken') def is_valid_filename(filename): return bool(re.match(VALID_FILENAME_REGEX, filename)) @app.route('/upload', methods=['POST']) def upload_file(): if 'file' not in request.files: return jsonify({'error': 'No file part in the request'}), 400 if 'token' not in request.headers: return jsonify({'error': 'No token supplied'}), 401 if request.headers['token'] != UPLOAD_TOKEN: return jsonify({'error': 'Invalid token supplied'}), 401 file = request.files['file'] if file.filename == '': return jsonify({'error': 'No file selected for upload'}), 400 if not is_valid_filename(file.filename): return jsonify({'error': 'Invalid filename. Only alphanumeric characters, hyphens, underscores, and periods are allowed.'}), 400 filename = file.filename file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) return jsonify({'success': 'File \'{}\' successfully uploaded'.format(filename)}) @app.route('/download/', methods=['GET']) def download_file(filename): try: return send_from_directory(app.config['UPLOAD_FOLDER'], filename) except FileNotFoundError: return jsonify({'error': 'File not found'}), 404 if __name__ == '__main__': app.run(debug=True, host='0.0.0.0', port=int(os.environ.get('PORT', 5000))) # Upload # touch ../hallowelt && curl -X POST -H "token: myuploadtoken" -F "file=@../hallowelt" http://docker10.grote.lan:5040/upload # Download # wget http://docker10.grote.lan:5040/download/hallowelt # upload check # download # farben im putput weg # wsgi server