Skip to content
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

Media Utils: Restrict file uploads with multiple prop in uploadMedia and mediaUpload #69175

Merged

Conversation

yogeshbhutkar
Copy link
Contributor

@yogeshbhutkar yogeshbhutkar commented Feb 13, 2025

What?

Closes #68629

This PR adds a multiple prop to the uploadMedia and mediaUpload functions. The prop defaults to True. When set to False, any attempts to drop or select multiple images result in an error message returned to the user, and the process aborts completely due to an early return.

Why?

This PR uses a generalized approach to implement prop-based restrictions on file uploads instead of implementing separate logic in the consumers. Some consumer implementations include:

if ( filesList?.length > 1 ) {

How?

The multiple prop, which accepts a boolean value, has been introduced in the uploadMedia and mediaUpload functions. When multiple is set to false and multiple images are selected, the process is aborted early, triggering the onError callback.

Testing Instructions

  1. Try uploading multiple files in the following places:
    1.1. Site logo block
    1.2. Featured image block
    1.3. Featured image panel in the document sidebar
    1.4. Media & text block
    1.5. Cover block
  2. Ensure multiple files abort the process.

Screencast

final-testing

@yogeshbhutkar yogeshbhutkar marked this pull request as ready for review February 13, 2025 08:54
Copy link

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 props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: yogeshbhutkar <[email protected]>
Co-authored-by: swissspidy <[email protected]>
Co-authored-by: Mayank-Tripathi32 <[email protected]>
Co-authored-by: t-hamano <[email protected]>
Co-authored-by: Mamaduka <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

}: UploadMediaArgs ) {
if ( ! multiple && filesList.length > 1 ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a unit test for this accordingly in packages/media-utils/src/utils/test/upload-media.ts

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change here is unnecessary, let's revert that.

Comment on lines 77 to 80
new UploadError( {
code: 'GENERAL',
message: __( 'Only one file can be used here.' ),
} )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just use a regular Error if you don't want to pass a file.

@yogeshbhutkar
Copy link
Contributor Author

yogeshbhutkar commented Feb 14, 2025

Thanks for the review @swissspidy, I've implemented the above changes. Can you please re-review the PR?

unit-test
Test case works as intended.

@swissspidy swissspidy added [Type] Enhancement A suggestion for improvement. [Feature] Media Anything that impacts the experience of managing media [Package] Media Utils /packages/media-utils labels Feb 14, 2025
}: UploadMediaArgs ) {
if ( ! multiple && filesList.length > 1 ) {
onError?.( new Error( __( 'Only one file can be used here.' ) ) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the error message could be improved, but I don't have better ideas at the moment. Let's follow-up on it as needed.

Copy link
Member

@Mamaduka Mamaduka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, @yogeshbhutkar!

@Mamaduka Mamaduka merged commit 1976565 into WordPress:trunk Feb 15, 2025
68 of 70 checks passed
@github-actions github-actions bot added this to the Gutenberg 20.4 milestone Feb 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Media Anything that impacts the experience of managing media [Package] Media Utils /packages/media-utils [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Media Utils: Allow limiting uploads to a single file
3 participants