Skip to content

Commit

Permalink
Merge pull request #1428 from scut-chenzk/chenzk
Browse files Browse the repository at this point in the history
修复收到从微信发出的图片消息保存到本地失败的问题
  • Loading branch information
zhayujie committed Sep 26, 2023
2 parents b45eea5 + 90cdff3 commit 7c8fb7e
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions channel/wework/wework_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from bridge.context import ContextType
from channel.chat_message import ChatMessage
from common.log import logger
from ntwork.const import send_type


def get_with_retry(get_func, max_retries=5, delay=5):
Expand Down Expand Up @@ -41,15 +42,37 @@ def cdn_download(wework, message, file_name):
data = message["data"]
aes_key = data["cdn"]["aes_key"]
file_size = data["cdn"]["size"]
file_type = 2
file_id = data["cdn"]["file_id"]

# 获取当前工作目录,然后与文件名拼接得到保存路径
current_dir = os.getcwd()
save_path = os.path.join(current_dir, "tmp", file_name)

result = wework.c2c_cdn_download(file_id, aes_key, file_size, file_type, save_path)
logger.debug(result)
# 下载保存图片到本地
if "url" in data["cdn"].keys() and "auth_key" in data["cdn"].keys():
url = data["cdn"]["url"]
auth_key = data["cdn"]["auth_key"]
# result = wework.wx_cdn_download(url, auth_key, aes_key, file_size, save_path) # ntwork库本身接口有问题,缺失了aes_key这个参数
"""
下载wx类型的cdn文件,以https开头
"""
data = {
'url': url,
'auth_key': auth_key,
'aes_key': aes_key,
'size': file_size,
'save_path': save_path
}
result = wework._WeWork__send_sync(send_type.MT_WXCDN_DOWNLOAD_MSG, data) # 直接用wx_cdn_download的接口内部实现来调用
elif "file_id" in data["cdn"].keys():
file_type = 2
file_id = data["cdn"]["file_id"]
result = wework.c2c_cdn_download(file_id, aes_key, file_size, file_type, save_path)
else:
logger.error(f"something is wrong, data: {data}")
return

# 输出下载结果
logger.debug(f"result: {result}")


def c2c_download_and_convert(wework, message, file_name):
Expand Down

0 comments on commit 7c8fb7e

Please sign in to comment.