diff --git a/admin/dashboard/blueprints/plugin_routes.py b/admin/dashboard/blueprints/plugin_routes.py index ca0145d..83437d8 100644 --- a/admin/dashboard/blueprints/plugin_routes.py +++ b/admin/dashboard/blueprints/plugin_routes.py @@ -1,4 +1,8 @@ +import json import logging +import os + +import toml from flask import Blueprint, request, jsonify, render_template, current_app from admin.dashboard.blueprints.auth import login_required @@ -148,4 +152,101 @@ def reload_plugin(): return jsonify({"success": False, "message": f"插件 {plugin_name} 重载失败"}) except Exception as e: LOG.error(f"重载插件失败: {str(e)}", exc_info=True) - return jsonify({"success": False, "message": f"重载插件失败: {str(e)}"}) \ No newline at end of file + return jsonify({"success": False, "message": f"重载插件失败: {str(e)}"}) + + +@plugin_routes.route('/api/plugins/config/raw', methods=['GET']) +@login_required +def get_raw_plugin_config(): + """获取插件原始配置文件内容""" + try: + server = current_app.dashboard_server + plugin_name = request.args.get('plugin_name') + if not plugin_name: + return jsonify({"success": False, "message": "缺少插件名称参数"}) + + # 获取插件管理器 + + # 查找插件 + display_name, plugin = server.plugin_manager.find_plugin_by_name(plugin_name) + + if not plugin: + return jsonify({"success": False, "message": f"未找到插件: {plugin_name}"}) + + # 获取配置文件路径 + config_path = plugin.get_config_path() + + if not os.path.exists(config_path): + return jsonify({"success": False, "message": f"配置文件不存在: {config_path}"}) + + # 读取配置文件内容 + with open(config_path, 'r', encoding='utf-8') as f: + config_text = f.read() + + # 确定配置文件格式 + format_type = 'toml' + if config_path.endswith('.json'): + format_type = 'json' + elif config_path.endswith('.yaml') or config_path.endswith('.yml'): + format_type = 'yaml' + + return jsonify({ + "success": True, + "data": config_text, + "format": format_type + }) + except Exception as e: + LOG.error(f"获取插件配置文件失败: {str(e)}", exc_info=True) + return jsonify({"success": False, "message": f"获取配置文件失败: {str(e)}"}) + +@plugin_routes.route('/api/plugins/config/update', methods=['POST']) +@login_required +def update_plugin_config(): + """更新插件配置""" + try: + server = current_app.dashboard_server + data = request.json + plugin_name = data.get('plugin_name') + config_text = data.get('config_text') + format_type = data.get('format', 'toml') + + if not plugin_name or config_text is None: + return jsonify({"success": False, "message": "缺少必要参数"}) + + # 查找插件 + # 获取插件管理器 + display_name, plugin = server.plugin_manager.find_plugin_by_name(plugin_name) + + if not plugin: + return jsonify({"success": False, "message": f"未找到插件: {plugin_name}"}) + + # 获取配置文件路径 + config_path = plugin.get_config_path() + + # 确保配置目录存在 + os.makedirs(os.path.dirname(config_path), exist_ok=True) + + # 写入配置文件 + with open(config_path, 'w', encoding='utf-8') as f: + f.write(config_text) + + # 解析配置并更新插件内部配置 + try: + if format_type == 'toml': + config_obj = toml.loads(config_text) + elif format_type == 'json': + config_obj = json.loads(config_text) + else: + return jsonify({"success": False, "message": f"不支持的配置格式: {format_type}"}) + + # 更新插件内部配置 + plugin._config = config_obj + + return jsonify({"success": True, "message": "配置已保存"}) + except Exception as e: + LOG.error(f"解析配置失败: {str(e)}", exc_info=True) + return jsonify({"success": False, "message": f"配置已保存,但解析失败: {str(e)}"}) + + except Exception as e: + LOG.error(f"更新插件配置失败: {str(e)}", exc_info=True) + return jsonify({"success": False, "message": f"更新配置失败: {str(e)}"}) \ No newline at end of file diff --git a/admin/dashboard/templates/base.html b/admin/dashboard/templates/base.html index 2aae927..95c4b96 100644 --- a/admin/dashboard/templates/base.html +++ b/admin/dashboard/templates/base.html @@ -17,6 +17,8 @@ + + {% endblock %} \ No newline at end of file diff --git a/plugin_common/plugin_interface.py b/plugin_common/plugin_interface.py index a3411a4..06e781a 100644 --- a/plugin_common/plugin_interface.py +++ b/plugin_common/plugin_interface.py @@ -170,7 +170,8 @@ class PluginInterface(ABC): """ return True - # ... 其他现有方法 ... + def get_config_path(self) -> str: + return os.path.join(self._plugin_path, 'config.toml') def can_process(self, data: Any) -> bool: """检查插件是否可以处理给定的数据