From a4433f939430a016c4d954cdfad97e8f904652e1 Mon Sep 17 00:00:00 2001 From: Michael Grote Date: Thu, 13 Apr 2023 12:36:54 +0200 Subject: [PATCH] set upload-dir variable --- app.py | 12 ++++++------ docker-compose.yml | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app.py b/app.py index 1dd4d82..a8cd615 100644 --- a/app.py +++ b/app.py @@ -5,7 +5,7 @@ from flask import Flask, request, jsonify, send_from_directory import datetime app = Flask(__name__) -app.config['UPLOAD_FOLDER'] = os.environ.get('UPLOAD_FOLDER', '/home/mg/api/python-api-server/uploads') +app.config['UPLOAD_DIRECTORY'] = os.environ.get('UPLOAD_DIRECTORY', '/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\-_\.]+$' @@ -34,13 +34,13 @@ def upload_file(): 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)) + file.save(os.path.join(app.config['UPLOAD_DIRECTORY'], 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) + return send_from_directory(app.config['UPLOAD_DIRECTORY'], filename) except FileNotFoundError: return jsonify({'error': 'File not found'}), 404 @@ -52,7 +52,7 @@ def delete_file(filename): if request.headers['token'] != AUTH_TOKEN: return jsonify({'error': 'Invalid token supplied'}), 401 - file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename) + file_path = os.path.join(app.config['UPLOAD_DIRECTORY'], filename) if not os.path.isfile(file_path): return jsonify({'error': 'File not found'}), 404 @@ -68,8 +68,8 @@ def list_files(): return jsonify({'error': 'Invalid token supplied'}), 401 files = [] - for filename in os.listdir(app.config['UPLOAD_FOLDER']): - file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename) + for filename in os.listdir(app.config['UPLOAD_DIRECTORY']): + file_path = os.path.join(app.config['UPLOAD_DIRECTORY'], filename) if os.path.isfile(file_path): stats = os.stat(file_path) size = stats.st_size diff --git a/docker-compose.yml b/docker-compose.yml index 541bc9f..4c9812f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,5 +9,5 @@ services: environment: FLASK_DEBUG: 1 FLASK_APP: app - UPLOAD_DIRECTORY: /uploads AUTH_TOKEN: myuploadtoken + UPLOAD_DIRECTORY: /uploads # where to store the uploaded files; should be mapped to a docker volume; default "/uploads"