From ab001df77019e72e14671a96fc879316aa809fa9 Mon Sep 17 00:00:00 2001 From: Li Chuangbo Date: Fri, 9 Sep 2022 16:37:15 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20=E4=BF=AE=E5=A4=8D=20`url=5Fto?= =?UTF-8?q?=5Ffile`=20=E5=87=BD=E6=95=B0=E5=9C=A8=20Windows=20=E4=B8=8A?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E7=9A=84=20uri=20=E5=8F=AF=E8=83=BD=E4=B8=8D?= =?UTF-8?q?=E6=AD=A3=E7=A1=AE=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/genshin/material.py | 2 +- plugins/genshin/strategy.py | 2 +- utils/helpers.py | 9 +++++++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/plugins/genshin/material.py b/plugins/genshin/material.py index 5c4e6c0..33e8d86 100644 --- a/plugins/genshin/material.py +++ b/plugins/genshin/material.py @@ -49,7 +49,7 @@ class Material(Plugin, BasePlugin): return logger.info(f"用户 {user.full_name}[{user.id}] 查询角色培养素材命令请求 || 参数 {character_name}") await message.reply_chat_action(ChatAction.UPLOAD_PHOTO) - file_path = await url_to_file(url, "") + file_path = await url_to_file(url, return_path=True) caption = "From 米游社 " \ f"查看 [原图]({url})" await message.reply_photo(photo=open(file_path, "rb"), caption=caption, filename=f"{character_name}.png", diff --git a/plugins/genshin/strategy.py b/plugins/genshin/strategy.py index 3447d70..89d0182 100644 --- a/plugins/genshin/strategy.py +++ b/plugins/genshin/strategy.py @@ -50,7 +50,7 @@ class StrategyPlugin(Plugin, BasePlugin): return logger.info(f"用户 {user.full_name}[{user.id}] 查询角色攻略命令请求 || 参数 {character_name}") await message.reply_chat_action(ChatAction.UPLOAD_PHOTO) - file_path = await url_to_file(url, "") + file_path = await url_to_file(url, return_path=True) caption = "From 米游社 西风驿站 " \ f"查看 [原图]({url})" await message.reply_photo(photo=open(file_path, "rb"), caption=caption, filename=f"{character_name}.png", diff --git a/utils/helpers.py b/utils/helpers.py index 05d5f82..a2b3439 100644 --- a/utils/helpers.py +++ b/utils/helpers.py @@ -1,5 +1,6 @@ import hashlib import os +from pathlib import Path from typing import Tuple, Union, Optional, cast import aiofiles @@ -48,7 +49,7 @@ def sha1(text: str) -> str: return _sha1.hexdigest() -async def url_to_file(url: str, prefix: str = "file://") -> str: +async def url_to_file(url: str, return_path: bool = False) -> str: url_sha1 = sha1(url) url_file_name = os.path.basename(url) _, extension = os.path.splitext(url_file_name) @@ -70,7 +71,11 @@ async def url_to_file(url: str, prefix: str = "file://") -> str: async with aiofiles.open(file_dir, mode='wb') as f: await f.write(data.content) logger.debug(f"url_to_file 获取url[{url}] 并下载到 file_dir[{file_dir}]") - return prefix + file_dir + + if return_path: + return file_dir + + return Path(file_dir).as_uri() async def get_genshin_client(user_id: int, region: Optional[RegionEnum] = None) -> Client: