-
-
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
Open
march-7th-mini
wants to merge
5
commits into
AstrBotDevs:master
Choose a base branch
from
march-7th-mini:fix/webchat-file-rename
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1104ee7
fix: webchat file attachment renamed with UUID instead of original name
march-7th-mini 607c33c
Merge branch 'AstrBotDevs:master' into fix/webchat-file-rename
march-7th-mini e701ccc
fix: sanitize filename and optimize dedup loop
march-7th-mini 808f0b0
Limit file name deduplication attempts to 10,000
march-7th-mini fb2e8d0
fix: decrease dedup max attempts to 1000 and fix ruff formatting
march-7th-mini File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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 | ||||||||||||||||||||||||||||||||||||||||
|
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", | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.