mirror of
https://github.com/PaiGramTeam/PamGram.git
synced 2024-11-21 21:58:04 +00:00
🐛 修复 url_to_file
函数在 Windows 上返回的 uri 可能不正确的问题
This commit is contained in:
parent
10f6e3ae1d
commit
ab001df770
@ -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",
|
||||
|
@ -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",
|
||||
|
@ -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:
|
||||
|
Loading…
Reference in New Issue
Block a user