feat: update file download url

This commit is contained in:
Junyan Qin
2025-02-25 16:12:45 +08:00
parent 4db1d2b3a3
commit 9401a79b2b
8 changed files with 45 additions and 17 deletions

View File

@@ -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": "",

View File

@@ -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()

View File

@@ -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):

View File

@@ -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([

View File

@@ -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

View File

@@ -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}"
# 下载图片

View File

@@ -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": "",

View File

@@ -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",