Skip to content

Commit

Permalink
Merge pull request #72 from Icexbb/main
Browse files Browse the repository at this point in the history
Fix #71
  • Loading branch information
LambdaYH committed May 25, 2024
2 parents 248a374 + 18dd070 commit fcc192c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
6 changes: 5 additions & 1 deletion nonebot_plugin_bottle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,14 @@ async def _(
user_name = user_info.get("card") or user_info.get("nickname")

try:
serialized_content = await serialize_message(message=args)
cache_result = [not part.get("data") for part in serialized_content if part["type"] == "cached_image"]
if any(cache_result):
await throw.finish(MessageSegment.reply(message_id) + "漂流瓶中的图片未能缓存成功。")
add_index = await bottle_manager.add_bottle(
user_id=event.user_id,
group_id=event.group_id,
content=await serialize_message(message=args),
content=serialized_content,
user_name=user_name,
group_name=group_name,
session=session,
Expand Down
22 changes: 13 additions & 9 deletions nonebot_plugin_bottle/data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,19 @@ async def cache_file(msg: Message):


async def cache_image_url(seg: MessageSegment, client: httpx.AsyncClient):
if url := seg.data.get("url"):
try:
r = await client.get(url)
data = r.content
except httpx.TimeoutException:
return
seg.type = "cached_image"
seg.data.clear()
else:
url = seg.data.get("url")
if not url:
return

seg.type = "cached_image"
seg.data.clear()
try:
r = await client.get(url)
data = r.content
except httpx.TimeoutException:
return

if r.status_code != 200 or not data:
return
hash = hashlib.md5(data).hexdigest()
filename = f"{hash}.cache"
Expand Down

0 comments on commit fcc192c

Please sign in to comment.