Paste: Give each pasted data-URI image unique filename to avoid upload collisions.#79480
Open
mecskyverse wants to merge 2 commits into
Open
Paste: Give each pasted data-URI image unique filename to avoid upload collisions.#79480mecskyverse wants to merge 2 commits into
mecskyverse wants to merge 2 commits into
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Unlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @traed. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What?
Closes #73092
Pasted images (e.g. copying content from Google Docs) could end up in the wrong order or get duplicated, with some images overwriting others. This PR fixes that by giving each pasted image a unique filename before upload.
Why?
When you paste HTML containing images, each embedded image arrives as a
data:URI. InimageCorrector(packages/blocks/src/api/raw-handling/image-corrector.ts), every such image was turned into aFilewhose name was derived from its MIME type alone:So every pasted image was named image.png (or image.jpeg, etc.). Each pasted image mounts as its own block and immediately fires its own mediaUpload(), so pasting N images fires N concurrent uploads — all with the same filename.
On the server, wp_unique_filename() derives the next available filename by scanning the uploads directory at request time. Under concurrency, multiple requests scan at the same moment, all compute the same "next available" name, and sometimes overwrite each other. The result is the intermittent reordering/duplication reported in the issue (e.g. 1,2,3,4,3,6,7,8,8,10). This matches the findings in the issue — the source HTML from Google Docs is in the correct order, so the problem is on our end, and it only shows up under load/timing (which is why it's intermittent and hard to reproduce).
How?
Generate a unique filename per pasted image so concurrent uploads no longer collide:
Because each file now has a distinct base name, wp_unique_filename() gives each its own independent sequence and they can no longer overwrite one another — even when uploaded fully in parallel.
Naming impact
This only affects
data: URIimages embedded in pasted content (Google Docs, screenshots pasted as data URIs, other rich editors, etc.).Note on filenames: pasted data-URI images never had a real filename — image.png was a placeholder created from the MIME type. Previously they used to become image-1.png, image-2.png, etc. After this change and WordPress's normal de-duplication across sessions/reloads, they may appear as image-1-1.png, image-1-2.png, image-1-3.png, image-2-1, image-3-1, image-3-2 and so on.
Testing Instructions
(At most ~50 images in 1 copy/paste upload is supported)
Automated Test:
A new unit test asserts that two pasted data-URI images receive distinct
image-N.pngfilenames.Testing performed
Screenshots or screencast
Before
Before.Image.Collision.mp4
After
After.Image.Collision-2.mov
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Opus 4.8 (via Claude Code)
Used for: Initial code search, test case addition, PR Description, Creation of test images for testing is done by AI.
Testing cases, code addition, review and confirmation of fix working is done by me.