fix: chatui cannot persist file segment (#5386)

This commit is contained in:
Soulter
2026-02-23 22:02:49 +08:00
committed by GitHub
parent e357d9de74
commit a56e43d17e
2 changed files with 12 additions and 7 deletions
@@ -11,13 +11,13 @@ from astrbot.core.utils.astrbot_path import get_astrbot_data_path
from .webchat_queue_mgr import webchat_queue_mgr from .webchat_queue_mgr import webchat_queue_mgr
imgs_dir = os.path.join(get_astrbot_data_path(), "webchat", "imgs") attachments_dir = os.path.join(get_astrbot_data_path(), "attachments")
class WebChatMessageEvent(AstrMessageEvent): class WebChatMessageEvent(AstrMessageEvent):
def __init__(self, message_str, message_obj, platform_meta, session_id) -> None: def __init__(self, message_str, message_obj, platform_meta, session_id) -> None:
super().__init__(message_str, message_obj, platform_meta, session_id) super().__init__(message_str, message_obj, platform_meta, session_id)
os.makedirs(imgs_dir, exist_ok=True) os.makedirs(attachments_dir, exist_ok=True)
@staticmethod @staticmethod
async def _send( async def _send(
@@ -69,7 +69,7 @@ class WebChatMessageEvent(AstrMessageEvent):
elif isinstance(comp, Image): elif isinstance(comp, Image):
# save image to local # save image to local
filename = f"{str(uuid.uuid4())}.jpg" filename = f"{str(uuid.uuid4())}.jpg"
path = os.path.join(imgs_dir, filename) path = os.path.join(attachments_dir, filename)
image_base64 = await comp.convert_to_base64() image_base64 = await comp.convert_to_base64()
with open(path, "wb") as f: with open(path, "wb") as f:
f.write(base64.b64decode(image_base64)) f.write(base64.b64decode(image_base64))
@@ -85,7 +85,7 @@ class WebChatMessageEvent(AstrMessageEvent):
elif isinstance(comp, Record): elif isinstance(comp, Record):
# save record to local # save record to local
filename = f"{str(uuid.uuid4())}.wav" filename = f"{str(uuid.uuid4())}.wav"
path = os.path.join(imgs_dir, filename) path = os.path.join(attachments_dir, filename)
record_base64 = await comp.convert_to_base64() record_base64 = await comp.convert_to_base64()
with open(path, "wb") as f: with open(path, "wb") as f:
f.write(base64.b64decode(record_base64)) f.write(base64.b64decode(record_base64))
@@ -104,7 +104,7 @@ class WebChatMessageEvent(AstrMessageEvent):
original_name = comp.name or os.path.basename(file_path) original_name = comp.name or os.path.basename(file_path)
ext = os.path.splitext(original_name)[1] or "" ext = os.path.splitext(original_name)[1] or ""
filename = f"{uuid.uuid4()!s}{ext}" filename = f"{uuid.uuid4()!s}{ext}"
dest_path = os.path.join(imgs_dir, filename) dest_path = os.path.join(attachments_dir, filename)
shutil.copy2(file_path, dest_path) shutil.copy2(file_path, dest_path)
data = f"[FILE]{filename}" data = f"[FILE]{filename}"
await web_chat_back_queue.put( await web_chat_back_queue.put(
+7 -2
View File
@@ -215,8 +215,13 @@ class ChatRoute(Route):
filename: 存储的文件名 filename: 存储的文件名
attach_type: 附件类型 (image, record, file, video) attach_type: 附件类型 (image, record, file, video)
""" """
file_path = os.path.join(self.attachments_dir, os.path.basename(filename)) basename = os.path.basename(filename)
if not os.path.exists(file_path): candidate_paths = [
os.path.join(self.attachments_dir, basename),
os.path.join(self.legacy_img_dir, basename),
]
file_path = next((p for p in candidate_paths if os.path.exists(p)), None)
if not file_path:
return None return None
# guess mime type # guess mime type