-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
fix: webchat file attachment renamed with UUID instead of original name #8486
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 3 commits
1104ee7
607c33c
e701ccc
808f0b0
fb2e8d0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -109,14 +109,19 @@ 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 | ||||||||||||||||||||||||||||||||||||||||
| while os.path.exists(dest_path): | ||||||||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里可以限制循环上限(比如 100_000),防止一些奇怪的问题
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 谢谢大佬!空了麻烦您再帮忙看看。 起初我只是觉得AI每次回复的文件都被重命名,就很不方便, 因为我完全没有任何编程经验,我判断不了这个修改是否符合项目的规范,可能会带来哪些风险。 |
||||||||||||||||||||||||||||||||||||||||
| dest_path = os.path.join(attachments_dir, f"{name_part}_{counter}{ext_part}") | ||||||||||||||||||||||||||||||||||||||||
| counter += 1 | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
114
to
+125
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Security & Efficiency Review
Suggested change
|
||||||||||||||||||||||||||||||||||||||||
| 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", | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.