This repository has been archived on 2024-11-06. You can view files and clone it, but cannot push or open issues or pull requests.
python-api-server/app.py

87 lines
3.1 KiB
Python
Raw Normal View History

2023-04-12 16:08:55 +02:00
import os
2023-04-13 11:46:17 +02:00
import re
2023-04-13 11:34:16 +02:00
import uuid
2023-04-13 11:46:17 +02:00
from flask import Flask, request, jsonify, send_from_directory
2023-04-13 12:28:27 +02:00
import datetime
2023-04-13 11:03:56 +02:00
2023-04-13 11:29:47 +02:00
app = Flask(__name__)
2023-04-13 12:36:54 +02:00
app.config['UPLOAD_DIRECTORY'] = os.environ.get('UPLOAD_DIRECTORY', '/uploads')
2023-04-13 11:57:26 +02:00
app.config['MAX_CONTENT_LENGTH'] = int(os.environ.get('MAX_CONTENT_LENGTH', '5')) * 1024 * 1024 # in MB
2023-04-13 11:34:16 +02:00
2023-04-13 11:46:17 +02:00
VALID_FILENAME_REGEX = r'^[a-zA-Z0-9\-_\.]+$'
2023-04-13 11:34:16 +02:00
2023-04-13 12:28:27 +02:00
AUTH_TOKEN = os.environ.get('AUTH_TOKEN', 'myuploadtoken')
2023-04-13 11:57:26 +02:00
2023-04-13 11:46:17 +02:00
def is_valid_filename(filename):
return bool(re.match(VALID_FILENAME_REGEX, filename))
2023-04-13 11:03:56 +02:00
2023-04-13 11:46:17 +02:00
@app.route('/upload', methods=['POST'])
def upload_file():
if 'file' not in request.files:
return jsonify({'error': 'No file part in the request'}), 400
2023-04-13 11:03:56 +02:00
2023-04-13 11:57:26 +02:00
if 'token' not in request.headers:
return jsonify({'error': 'No token supplied'}), 401
2023-04-13 12:19:18 +02:00
if request.headers['token'] != AUTH_TOKEN:
2023-04-13 11:57:26 +02:00
return jsonify({'error': 'Invalid token supplied'}), 401
2023-04-13 11:46:17 +02:00
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
2023-04-13 12:36:54 +02:00
file.save(os.path.join(app.config['UPLOAD_DIRECTORY'], filename))
2023-04-13 12:15:52 +02:00
return jsonify({'success': 'File \'{}\' successfully uploaded'.format(filename)})
2023-04-13 11:46:17 +02:00
@app.route('/download/<filename>', methods=['GET'])
def download_file(filename):
try:
2023-04-13 12:36:54 +02:00
return send_from_directory(app.config['UPLOAD_DIRECTORY'], filename)
2023-04-13 11:46:17 +02:00
except FileNotFoundError:
2023-04-13 13:51:45 +02:00
return jsonify({'error': 'File \'{}\' not found'}), 404
2023-04-13 11:46:17 +02:00
2023-04-13 12:19:27 +02:00
@app.route('/delete/<filename>', methods=['DELETE'])
def delete_file(filename):
if 'token' not in request.headers:
return jsonify({'error': 'No token supplied'}), 401
if request.headers['token'] != AUTH_TOKEN:
return jsonify({'error': 'Invalid token supplied'}), 401
2023-04-13 12:36:54 +02:00
file_path = os.path.join(app.config['UPLOAD_DIRECTORY'], filename)
2023-04-13 12:28:27 +02:00
if not os.path.isfile(file_path):
2023-04-13 14:30:29 +02:00
return jsonify({'error': 'File not found'}), 404
2023-04-13 12:19:27 +02:00
2023-04-13 12:28:27 +02:00
os.remove(file_path)
return jsonify({'success': 'File \'{}\' successfully deleted'.format(filename)})
@app.route('/list', methods=['GET'])
def list_files():
if 'token' not in request.headers:
return jsonify({'error': 'No token supplied'}), 401
if request.headers['token'] != AUTH_TOKEN:
return jsonify({'error': 'Invalid token supplied'}), 401
files = []
2023-04-13 12:36:54 +02:00
for filename in os.listdir(app.config['UPLOAD_DIRECTORY']):
file_path = os.path.join(app.config['UPLOAD_DIRECTORY'], filename)
2023-04-13 12:28:27 +02:00
if os.path.isfile(file_path):
stats = os.stat(file_path)
size = stats.st_size
last_modified = datetime.datetime.fromtimestamp(stats.st_mtime).strftime('%Y-%m-%d %H:%M:%S')
files.append({
'name': filename,
'size': size,
'last_modified': last_modified
})
return jsonify({'files': files})
2023-04-13 11:46:17 +02:00
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=int(os.environ.get('PORT', 5000)))