From 1104ee7f0f750b1b45b7b8b4f09b634931ce4f61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E4=B8=89=E6=9C=88?= Date: Mon, 1 Jun 2026 16:21:07 +0800 Subject: [PATCH 1/4] fix: webchat file attachment renamed with UUID instead of original name Replace UUID-based file naming with original filenames and add dedup counter. --- .../core/platform/sources/webchat/webchat_event.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/astrbot/core/platform/sources/webchat/webchat_event.py b/astrbot/core/platform/sources/webchat/webchat_event.py index bc1e1a6bcd..e60f63b379 100644 --- a/astrbot/core/platform/sources/webchat/webchat_event.py +++ b/astrbot/core/platform/sources/webchat/webchat_event.py @@ -109,14 +109,18 @@ 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) + dest_path = os.path.join(attachments_dir, original_name) + # dedup: if file with same name exists, append a counter + counter = 1 + while os.path.exists(dest_path): + name_part, ext_part = os.path.splitext(original_name) + 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", From e701ccc7015299f10993ba6895cfc1f94971b740 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E4=B8=89=E6=9C=88?= Date: Mon, 1 Jun 2026 17:24:19 +0800 Subject: [PATCH 2/4] fix: sanitize filename and optimize dedup loop MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化版代码:加了 os.path.basename 防路径穿越 + splitext 提到循环外。本地测试一切正常。 --- astrbot/core/platform/sources/webchat/webchat_event.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/astrbot/core/platform/sources/webchat/webchat_event.py b/astrbot/core/platform/sources/webchat/webchat_event.py index e60f63b379..84481f5bb2 100644 --- a/astrbot/core/platform/sources/webchat/webchat_event.py +++ b/astrbot/core/platform/sources/webchat/webchat_event.py @@ -112,11 +112,12 @@ async def _send( # save file to local with original name file_path = await comp.get_file() original_name = comp.name or os.path.basename(file_path) - dest_path = os.path.join(attachments_dir, original_name) + 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 while os.path.exists(dest_path): - name_part, ext_part = os.path.splitext(original_name) dest_path = os.path.join(attachments_dir, f"{name_part}_{counter}{ext_part}") counter += 1 shutil.copy2(file_path, dest_path) From 808f0b00baf0d275dbd2d00ddb66299467b01a01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E4=B8=89=E6=9C=88?= Date: Tue, 2 Jun 2026 10:49:00 +0800 Subject: [PATCH 3/4] Limit file name deduplication attempts to 10,000 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix: 限制防重名循环上限为 10000 --- astrbot/core/platform/sources/webchat/webchat_event.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/astrbot/core/platform/sources/webchat/webchat_event.py b/astrbot/core/platform/sources/webchat/webchat_event.py index 84481f5bb2..b4d89a763e 100644 --- a/astrbot/core/platform/sources/webchat/webchat_event.py +++ b/astrbot/core/platform/sources/webchat/webchat_event.py @@ -117,7 +117,8 @@ async def _send( name_part, ext_part = os.path.splitext(safe_name) # dedup: if file with same name exists, append a counter counter = 1 - while os.path.exists(dest_path): + max_attempts = 10_000 + 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) From fb2e8d0227272f97d429e280e70e9ea4c71a7157 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E4=B8=89=E6=9C=88?= Date: Tue, 2 Jun 2026 11:11:11 +0800 Subject: [PATCH 4/4] fix: decrease dedup max attempts to 1000 and fix ruff formatting MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix: 降低防重名循环上限至 1000,正常情况下使用足够了,修复 ruff 格式检查。 --- astrbot/core/platform/sources/webchat/webchat_event.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/astrbot/core/platform/sources/webchat/webchat_event.py b/astrbot/core/platform/sources/webchat/webchat_event.py index b4d89a763e..ed988773df 100644 --- a/astrbot/core/platform/sources/webchat/webchat_event.py +++ b/astrbot/core/platform/sources/webchat/webchat_event.py @@ -117,9 +117,11 @@ async def _send( name_part, ext_part = os.path.splitext(safe_name) # dedup: if file with same name exists, append a counter counter = 1 - max_attempts = 10_000 + 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}") + dest_path = os.path.join( + attachments_dir, f"{name_part}_{counter}{ext_part}" + ) counter += 1 shutil.copy2(file_path, dest_path) data = f"[FILE]{os.path.basename(dest_path)}"