|
| 1 | +from pathlib import Path |
| 2 | +import subprocess |
| 3 | +import uuid |
| 4 | +from flask import request |
| 5 | + |
| 6 | +from flask import ( |
| 7 | + Blueprint, jsonify, request, jsonify, session |
| 8 | +) |
| 9 | +from werkzeug.security import check_password_hash |
| 10 | +from flask import current_app as app |
| 11 | + |
| 12 | +bp = Blueprint('actions', __name__) |
| 13 | + |
| 14 | + |
| 15 | +@bp.route('/message', methods=['POST']) |
| 16 | +def log_entry(): |
| 17 | + auth = session.get('user_info', None) |
| 18 | + if auth is None: |
| 19 | + return jsonify({'error': 'no auth found in session'}) |
| 20 | + access_level = auth[2] |
| 21 | + if access_level > 2: |
| 22 | + return jsonify({'error': 'access level < 2 is required for this action'}) |
| 23 | + filename_param = request.form.get('filename') |
| 24 | + if filename_param is None: |
| 25 | + return jsonify({'error': 'filename parameter is required'}) |
| 26 | + text_param = request.form.get('text') |
| 27 | + if text_param is None: |
| 28 | + return jsonify({'error': 'text parameter is required'}) |
| 29 | + |
| 30 | + user_id = auth[0] |
| 31 | + user_dir = Path("data/" + str(user_id)) |
| 32 | + if not user_dir.exists(): |
| 33 | + user_dir.mkdir() |
| 34 | + |
| 35 | + filename = filename_param + ".txt" |
| 36 | + path = user_dir / filename |
| 37 | + with path.open("w", encoding ="utf-8") as f: |
| 38 | + f.write(text_param) |
| 39 | + return jsonify({'success': True}) |
| 40 | + |
| 41 | + |
| 42 | +@bp.route('/grep_processes') |
| 43 | +def grep_processes(): |
| 44 | + name = request.args.get('name') |
| 45 | + res = subprocess.run(["ps aux | grep " + name + " | awk '{print $11}'"], shell=True, capture_output=True) |
| 46 | + if res.stdout is None: |
| 47 | + return jsonify({'error': 'no stdout returned'}) |
| 48 | + out = res.stdout.decode("utf-8") |
| 49 | + names = out.split('\n') |
| 50 | + return jsonify({'success': True, 'names': names}) |
| 51 | + |
0 commit comments