From 7b3d3beb6ceb99fba87868d79164e26c91364898 Mon Sep 17 00:00:00 2001 From: SXP-Simon Date: Tue, 10 Feb 2026 21:54:09 +0800 Subject: [PATCH] =?UTF-8?q?fix(OneBot):=20=E4=BF=AE=E5=A4=8D=E5=9B=BE?= =?UTF-8?q?=E7=89=87=E5=8F=91=E9=80=81=E5=A4=B1=E8=B4=A5(rich=20media=20er?= =?UTF-8?q?ror)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - ReportGenerator: 改为返回 Base64 图片数据而非内部 URL,避免 OneBot 无法访问 Docker 内部服务 - OneBotAdapter: 增加对 base64:// 协议头的支持 --- .../platform/adapters/onebot_adapter.py | 2 ++ src/infrastructure/reporting/generators.py | 22 +++++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/infrastructure/platform/adapters/onebot_adapter.py b/src/infrastructure/platform/adapters/onebot_adapter.py index 915098b..f6f4020 100644 --- a/src/infrastructure/platform/adapters/onebot_adapter.py +++ b/src/infrastructure/platform/adapters/onebot_adapter.py @@ -374,6 +374,8 @@ class OneBotAdapter(PlatformAdapter): if image_path.startswith(("http://", "https://")): file_str = image_path + elif image_path.startswith("base64://"): + file_str = image_path else: file_str = f"file:///{image_path}" diff --git a/src/infrastructure/reporting/generators.py b/src/infrastructure/reporting/generators.py index c78da0a..055d1e7 100644 --- a/src/infrastructure/reporting/generators.py +++ b/src/infrastructure/reporting/generators.py @@ -107,18 +107,26 @@ class ReportGenerator(IReportGenerator): image_options["quality"] = None logger.info(f"正在尝试渲染策略: {image_options}") - image_url = await html_render_func( + # 改为获取 bytes 数据,避免 OneBot 无法访问内部 URL + image_data = await html_render_func( html_content, # 渲染后的HTML内容 {}, # 空数据字典,因为数据已包含在HTML中 - True, # return_url=True,返回URL而不是下载文件 + False, # return_url=False,直接获取图片数据 image_options, ) - if image_url: - logger.info(f"图片生成成功 ({image_options}): {image_url}") - return image_url, html_content - else: - logger.warning(f"渲染策略 {image_options} 返回空 URL") + if image_data: + if isinstance(image_data, bytes): + b64 = base64.b64encode(image_data).decode("utf-8") + image_url = f"base64://{b64}" + logger.info(f"图片生成成功 ({image_options}): [Base64 Data {len(image_data)} bytes]") + return image_url, html_content + elif isinstance(image_data, str): + # Fallback: 如果返回的是字符串(可能是URL或路径) + logger.info(f"图片生成成功 (String): {image_data}") + return image_data, html_content + + logger.warning(f"渲染策略 {image_options} 返回空数据") except Exception as e: logger.warning(f"渲染策略 {image_options} 失败: {e}")