diff --git a/astrbot/core/platform/sources/webchat/webchat_event.py b/astrbot/core/platform/sources/webchat/webchat_event.py index bc1e1a6bcd..ed988773df 100644 --- a/astrbot/core/platform/sources/webchat/webchat_event.py +++ b/astrbot/core/platform/sources/webchat/webchat_event.py @@ -109,14 +109,22 @@ async def _send( }, ) elif isinstance(comp, File): - # save file to local + # save file to local with original name file_path = await comp.get_file() original_name = comp.name or os.path.basename(file_path) - ext = os.path.splitext(original_name)[1] or "" - filename = f"{uuid.uuid4()!s}{ext}" - dest_path = os.path.join(attachments_dir, filename) + safe_name = os.path.basename(original_name) + dest_path = os.path.join(attachments_dir, safe_name) + name_part, ext_part = os.path.splitext(safe_name) + # dedup: if file with same name exists, append a counter + counter = 1 + max_attempts = 1000 + while os.path.exists(dest_path) and counter <= max_attempts: + dest_path = os.path.join( + attachments_dir, f"{name_part}_{counter}{ext_part}" + ) + counter += 1 shutil.copy2(file_path, dest_path) - data = f"[FILE]{filename}" + data = f"[FILE]{os.path.basename(dest_path)}" await web_chat_back_queue.put( { "type": "file",