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
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -313,19 +313,14 @@ function BackgroundImageControls( {

// Drag and drop callback, restricting image to one.
const onFilesDrop = ( filesList ) => {
if ( filesList?.length > 1 ) {
onUploadError(
__( 'Only one image can be used as a background image.' )
);
return;
}
getSettings().mediaUpload( {
allowedTypes: [ IMAGE_BACKGROUND_TYPE ],
filesList,
onFileChange( [ image ] ) {
onSelectMedia( image );
},
onError: onUploadError,
multiple: false,
} );
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ export function MediaPlaceholder( {
filesList: files,
onFileChange: setMedia,
onError,
multiple,
} );
};

Expand Down
6 changes: 1 addition & 5 deletions packages/block-library/src/site-logo/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -499,11 +499,6 @@ export default function LogoEdit( {
};

const onFilesDrop = ( filesList ) => {
if ( filesList?.length > 1 ) {
onUploadError( __( 'Only one image can be used as a site logo.' ) );
return;
}

getSettings().mediaUpload( {
allowedTypes: ALLOWED_MEDIA_TYPES,
filesList,
Expand All @@ -515,6 +510,7 @@ export default function LogoEdit( {
onInitialSelectLogo( image );
},
onError: onUploadError,
multiple: false,
} );
};

Expand Down
1 change: 1 addition & 0 deletions packages/editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ _Parameters_
- _$0.onError_ `Function`: Function called when an error happens.
- _$0.onFileChange_ `Function`: Function called each time a file or a temporary representation of the file is available.
- _$0.onSuccess_ `Function`: Function called after the final representation of the file is available.
- _$0.multiple_ `boolean`: Whether to allow multiple files to be uploaded.

### MediaUploadCheck

Expand Down
2 changes: 2 additions & 0 deletions packages/editor/src/components/post-featured-image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ function PostFeaturedImage( {
noticeOperations.removeAllNotices();
noticeOperations.createErrorNotice( message );
},
multiple: false,
} );
}

Expand Down Expand Up @@ -345,6 +346,7 @@ const applyWithDispatch = withDispatch(
noticeOperations.removeAllNotices();
noticeOperations.createErrorNotice( message );
},
multiple: false,
} );
},
onRemoveImage() {
Expand Down
3 changes: 3 additions & 0 deletions packages/editor/src/utils/media-upload/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const noop = () => {};
* @param {Function} $0.onError Function called when an error happens.
* @param {Function} $0.onFileChange Function called each time a file or a temporary representation of the file is available.
* @param {Function} $0.onSuccess Function called after the final representation of the file is available.
* @param {boolean} $0.multiple Whether to allow multiple files to be uploaded.
*/
export default function mediaUpload( {
additionalData = {},
Expand All @@ -37,6 +38,7 @@ export default function mediaUpload( {
onError = noop,
onFileChange,
onSuccess,
multiple = true,
} ) {
const { getCurrentPost, getEditorSettings } = select( editorStore );
const {
Expand Down Expand Up @@ -92,5 +94,6 @@ export default function mediaUpload( {
onError( message );
},
wpAllowedMimeTypes,
multiple,
} );
}
1 change: 1 addition & 0 deletions packages/media-utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ _Parameters_
- _$0.onFileChange_ `UploadMediaArgs[ 'onFileChange' ]`: Function called each time a file or a temporary representation of the file is available.
- _$0.wpAllowedMimeTypes_ `UploadMediaArgs[ 'wpAllowedMimeTypes' ]`: List of allowed mime types and file extensions.
- _$0.signal_ `UploadMediaArgs[ 'signal' ]`: Abort signal.
- _$0.multiple_ `UploadMediaArgs[ 'multiple' ]`: Whether to allow multiple files to be uploaded.

### validateFileSize

Expand Down
4 changes: 2 additions & 2 deletions packages/media-utils/src/utils/upload-error.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.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
interface UploadErrorArgs {
code: string;
message: string;
file: File;
file?: File;
cause?: Error;
}

Expand All @@ -13,7 +13,7 @@ interface UploadErrorArgs {
*/
export class UploadError extends Error {
code: string;
file: File;
file?: File;

constructor( { code, message, file, cause }: UploadErrorArgs ) {
super( message, { cause } );
Expand Down
14 changes: 14 additions & 0 deletions packages/media-utils/src/utils/upload-media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ interface UploadMediaArgs {
wpAllowedMimeTypes?: Record< string, string > | null;
// Abort signal.
signal?: AbortSignal;
// Whether to allow multiple files to be uploaded.
multiple?: boolean;
}

/**
Expand All @@ -57,6 +59,7 @@ interface UploadMediaArgs {
* @param $0.onFileChange Function called each time a file or a temporary representation of the file is available.
* @param $0.wpAllowedMimeTypes List of allowed mime types and file extensions.
* @param $0.signal Abort signal.
* @param $0.multiple Whether to allow multiple files to be uploaded.
*/
export function uploadMedia( {
wpAllowedMimeTypes,
Expand All @@ -67,7 +70,18 @@ export function uploadMedia( {
onError,
onFileChange,
signal,
multiple = true,
}: 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

onError?.(
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.

);
return;
}

const validFiles = [];

const filesSet: Array< Partial< Attachment > | null > = [];
Expand Down
Loading