diff --git a/pkg/core/migrations/m025_gewechat_config.py b/pkg/core/migrations/m025_gewechat_config.py index b5b01a43..65b5c1d5 100644 --- a/pkg/core/migrations/m025_gewechat_config.py +++ b/pkg/core/migrations/m025_gewechat_config.py @@ -23,7 +23,7 @@ class GewechatConfigMigration(migration.Migration): "adapter": "gewechat", "enable": False, "gewechat_url": "http://your-gewechat-server:2531", - "gewechat_downloadImage_port": 2532, + "gewechat_file_url": "http://your-gewechat-server:2532", "port": 2286, "callback_url": "http://your-callback-url:2286/gewechat/callback", "app_id": "", diff --git a/pkg/core/migrations/m034_gewechat_file_url_config.py b/pkg/core/migrations/m034_gewechat_file_url_config.py new file mode 100644 index 00000000..44bbd65e --- /dev/null +++ b/pkg/core/migrations/m034_gewechat_file_url_config.py @@ -0,0 +1,29 @@ +from __future__ import annotations + +from urllib.parse import urlparse + +from .. import migration + + +@migration.migration_class("gewechat-file-url-config", 34) +class GewechatFileUrlConfigMigration(migration.Migration): + """迁移""" + + async def need_migrate(self) -> bool: + """判断当前环境是否需要运行此迁移""" + + for adapter in self.ap.platform_cfg.data['platform-adapters']: + if adapter['adapter'] == 'gewechat': + if 'gewechat_file_url' not in adapter: + return True + return False + + async def run(self): + """执行迁移""" + for adapter in self.ap.platform_cfg.data['platform-adapters']: + if adapter['adapter'] == 'gewechat': + if 'gewechat_file_url' not in adapter: + parsed_url = urlparse(adapter['gewechat_url']) + adapter['gewechat_file_url'] = f"{parsed_url.scheme}://{parsed_url.hostname}:2532" + + await self.ap.platform_cfg.dump_config() diff --git a/pkg/core/stages/migrate.py b/pkg/core/stages/migrate.py index d317596e..ce6e41a5 100644 --- a/pkg/core/stages/migrate.py +++ b/pkg/core/stages/migrate.py @@ -11,7 +11,7 @@ from ..migrations import m015_gitee_ai_config, m016_dify_service_api, m017_dify_ from ..migrations import m020_wecom_config, m021_lark_config, m022_lmstudio_config, m023_siliconflow_config, m024_discord_config, m025_gewechat_config from ..migrations import m026_qqofficial_config, m027_wx_official_account_config, m028_aliyun_requester_config from ..migrations import m029_dashscope_app_api_config, m030_lark_config_cmpl, m031_dingtalk_config, m032_volcark_config -from ..migrations import m033_dify_thinking_config +from ..migrations import m033_dify_thinking_config, m034_gewechat_file_url_config @stage.stage_class("MigrationStage") class MigrationStage(stage.BootingStage): diff --git a/pkg/platform/sources/gewechat.py b/pkg/platform/sources/gewechat.py index e6e2d87e..c7478635 100644 --- a/pkg/platform/sources/gewechat.py +++ b/pkg/platform/sources/gewechat.py @@ -86,11 +86,11 @@ class GewechatMessageConverter(adapter.MessageConverter): try: base64_str, image_format = await image.get_gewechat_image_base64( gewechat_url=self.config["gewechat_url"], + gewechat_file_url=self.config["gewechat_file_url"] app_id=self.config["app_id"], xml_content=image_xml, token=self.config["token"], image_type=2, - download_image_port=self.config["gewechat_downloadImage_port"] ) return platform_message.MessageChain([ diff --git a/pkg/platform/sources/gewechat.yaml b/pkg/platform/sources/gewechat.yaml index 05b721a3..01967ffc 100644 --- a/pkg/platform/sources/gewechat.yaml +++ b/pkg/platform/sources/gewechat.yaml @@ -17,13 +17,13 @@ spec: type: string required: true default: "" - - name: gewechat_downloadImage_port + - name: gewechat_file_url label: - en_US: GeWeChat download image port - zh_CN: GeWeChat 下载图片的端口 - type: int + en_US: GeWeChat file download URL + zh_CN: GeWeChat 文件下载URL + type: string required: true - default: 2532 + default: "" - name: port label: en_US: Port diff --git a/pkg/utils/image.py b/pkg/utils/image.py index 5f185615..16077ace 100644 --- a/pkg/utils/image.py +++ b/pkg/utils/image.py @@ -17,21 +17,21 @@ from urllib.parse import urlparse async def get_gewechat_image_base64( gewechat_url: str, + gewechat_file_url: str, app_id: str, xml_content: str, token: str, image_type: int = 2, - download_image_port: int = 2532, ) -> typing.Tuple[str, str]: """从gewechat服务器获取图片并转换为base64格式 Args: gewechat_url (str): gewechat服务器地址(用于获取图片URL) + gewechat_file_url (str): gewechat文件下载服务地址 app_id (str): gewechat应用ID xml_content (str): 图片的XML内容 token (str): Gewechat API Token image_type (int, optional): 图片类型. Defaults to 2. - download_image_port (int, optional): 图片下载服务端口. Defaults to 2532. Returns: typing.Tuple[str, str]: (base64编码, 图片格式) @@ -80,8 +80,7 @@ async def get_gewechat_image_base64( raise Exception(f"获取图片下载链接网络错误: {str(e)}") # 解析原始URL并替换端口 - parsed_url = urlparse(gewechat_url) - base_url = f"http://{parsed_url.hostname}:{download_image_port}" + base_url = gewechat_file_url download_url = f"{base_url}/download/{file_url}" # 下载图片 diff --git a/templates/platform.json b/templates/platform.json index 2cccf058..a0fb8ce2 100644 --- a/templates/platform.json +++ b/templates/platform.json @@ -64,7 +64,7 @@ "adapter": "gewechat", "enable": false, "gewechat_url": "http://your-gewechat-server:2531", - "gewechat_downloadImage_port": 2532, + "gewechat_file_url": "http://your-gewechat-server:2532", "port": 2286, "callback_url": "http://your-callback-url:2286/gewechat/callback", "app_id": "", diff --git a/templates/schema/platform.json b/templates/schema/platform.json index 2f1114bd..925330a7 100644 --- a/templates/schema/platform.json +++ b/templates/schema/platform.json @@ -325,10 +325,10 @@ "default": "", "description": "gewechat 的 url" }, - "gewechat_downloadImage_port": { - "type": "integer", - "default": 2532, - "description": "gewechat 下载图片的端口" + "gewechat_file_url": { + "type": "string", + "default": "", + "description": "gewechat 文件下载URL" }, "port": { "type": "integer",