From e6b69042de8d9a10d271b5c6314b1279a54624ea Mon Sep 17 00:00:00 2001 From: Jackxwb <34720403+Jackxwb@users.noreply.github.com> Date: Sun, 6 Apr 2025 01:06:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=8F=91=E9=80=81=E6=97=B6?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=B7=AF=E5=BE=84=E6=98=A0=E5=B0=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- astrbot/core/pipeline/respond/stage.py | 67 +++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/astrbot/core/pipeline/respond/stage.py b/astrbot/core/pipeline/respond/stage.py index a43f0b32d..f095b2717 100644 --- a/astrbot/core/pipeline/respond/stage.py +++ b/astrbot/core/pipeline/respond/stage.py @@ -11,13 +11,16 @@ from astrbot.core import logger from astrbot.core.message.message_event_result import BaseMessageComponent from astrbot.core.star.star_handler import star_handlers_registry, EventType from astrbot.core.star.star import star_map -from astrbot.core.message.components import Plain, Reply, At +from astrbot.core.message.components import Plain, Reply, At, File +import os @register_stage class RespondStage(Stage): async def initialize(self, ctx: PipelineContext): self.ctx = ctx + self.config = ctx.astrbot_config + self.platform_settings: dict = self.config.get("platform_settings", {}) self.reply_with_mention = ctx.astrbot_config["platform_settings"][ "reply_with_mention" @@ -72,6 +75,60 @@ class RespondStage(Stage): # random return random.uniform(self.interval[0], self.interval[1]) + def pathMapping(self, mappings, srcPath: str)->str: + """路径映射处理函数。尝试支援 Windows 和 Linux 的路径映射。 + Args: + mappings: 映射规则列表 + srcPath: 原路径 + Returns: + str: 处理后的路径 + """ + for mapping in mappings: + rule = mapping.split(":") + if len(rule) == 2: + from_, to_ = mapping.split(":") + elif len(rule) > 4 or len(rule) == 1: + # 切割后大于4个项目,或者只有1个项目,那肯定是错误的,只能是2,3,4个项目 + logger.warning(f"路径映射规则错误: {mapping}") + continue + else: + # rule.len == 3 or 4 + if(os.path.exists(rule[0]+":"+rule[1])): + # 前面两个项目合并路径存在,说明是本地Window路径。后面一个或两个项目组成的路径本地大概率无法解析,直接拼接 + from_ = rule[0] + ":" + rule[1] + if len(rule) == 3: + to_ = rule[2] + else: + to_ = rule[2] + ":" + rule[3] + else: + # 前面两个项目合并路径不存在,说明第一个项目是本地Linux路径,后面一个或两个项目直接拼接。 + from_ = rule[0] + if len(rule) == 3: + to_ = rule[1] + ":" + rule[2] + else: + # 这种情况下存在四个项目,说明规则也是错误的 + logger.warning(f"路径映射规则错误: {mapping}") + continue + + from_ = from_.removesuffix("/") + from_ = from_.removesuffix("\\") + to_ = to_.removesuffix("/") + to_ = to_.removesuffix("\\") + # logger.debug(f"\t路径映射-规则(处理): {from_} -> {to_}") + + url = srcPath.removeprefix("file://") + if url.startswith(from_): + srcPath = url.replace(from_, to_, 1) + if ":" in srcPath: + # Windows路径处理 + srcPath = srcPath.replace("/", "\\") + else: + # Linux路径处理 + srcPath = srcPath.replace("\\", "/") + logger.info(f"路径映射: {url} -> {srcPath}") + return srcPath + return srcPath + async def process( self, event: AstrMessageEvent ) -> Union[None, AsyncGenerator[None, None]]: @@ -80,6 +137,14 @@ class RespondStage(Stage): return if len(result.chain) > 0: + # 检查路径映射 + if mappings := self.platform_settings.get("path_mapping", []): + for idx, component in enumerate(event.get_result().chain): + if isinstance(component, (File)) and component.file: + # 支持 File 消息段的路径映射。 + component.file = self.pathMapping(mappings, component.file) + event.get_result().chain[idx] = component + await event._pre_send() if self.enable_seg and (