🐛 修复 url_to_file 函数在 Windows 上返回的 uri 可能不正确的问题

This commit is contained in:
Li Chuangbo 2022-09-09 16:37:15 +08:00
parent 10f6e3ae1d
commit ab001df770
No known key found for this signature in database
3 changed files with 9 additions and 4 deletions

View File

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

View File

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

View File

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