set upload-dir variable

This commit is contained in:
Michael Grote 2023-04-13 12:36:54 +02:00
parent 54a2eee795
commit a4433f9394
2 changed files with 7 additions and 7 deletions

12
app.py
View file

@ -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/<filename>', 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

View file

@ -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"