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

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from

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

Warning: Type of PR label mismatch

To merge this PR, it requires exactly 1 label indicating the type of PR. Other labels are optional and not being checked here.

  • Type-related labels to choose from: [Type] Automated Testing, [Type] Breaking Change, [Type] Bug, [Type] Build Tooling, [Type] Code Quality, [Type] Copy, [Type] Developer Documentation, [Type] Enhancement, [Type] Experimental, [Type] Feature, [Type] New API, [Type] Task, [Type] Technical Prototype, [Type] Performance, [Type] Project Management, [Type] Regression, [Type] Security, [Type] WP Core Ticket, Backport from WordPress Core, Gutenberg Plugin.
  • Labels found: .

Read more about Type labels in Gutenberg. Don't worry if you don't have the required permissions to add labels; the PR reviewer should be able to help with the task.

1 similar comment
Copy link

Warning: Type of PR label mismatch

To merge this PR, it requires exactly 1 label indicating the type of PR. Other labels are optional and not being checked here.

  • Type-related labels to choose from: [Type] Automated Testing, [Type] Breaking Change, [Type] Bug, [Type] Build Tooling, [Type] Code Quality, [Type] Copy, [Type] Developer Documentation, [Type] Enhancement, [Type] Experimental, [Type] Feature, [Type] New API, [Type] Task, [Type] Technical Prototype, [Type] Performance, [Type] Project Management, [Type] Regression, [Type] Security, [Type] WP Core Ticket, Backport from WordPress Core, Gutenberg Plugin.
  • Labels found: .

Read more about Type labels in Gutenberg. Don't worry if you don't have the required permissions to add labels; the PR reviewer should be able to help with the task.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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