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

Show a warning when a disallowed filetype is dropped on a MediaUpload #9578

Merged
merged 6 commits into from Sep 3, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 19 additions & 5 deletions packages/editor/src/utils/media-upload/media-upload.js
Expand Up @@ -78,15 +78,29 @@ export function mediaUpload( {
};

files.forEach( ( mediaFile, idx ) => {
if ( ! isAllowedType( mediaFile.type ) ) {
return;
}

// verify if user is allowed to upload this mime type
if ( allowedMimeTypesForUser && ! isAllowedMimeTypeForUser( mediaFile.type ) ) {
onError( {
code: 'MIME_TYPE_NOT_ALLOWED_FOR_USER',
message: __( 'Sorry, this file type is not permitted for security reasons.' ),
message: [
<strong key="filename">{ mediaFile.name }</strong>,
': ',
__( 'Sorry, this file type is not permitted for security reasons.' ),
],
file: mediaFile,
} );
return;
}

// Check if the block supports this mime type
if ( ! isAllowedType( mediaFile.type ) ) {
onError( {
code: 'MIME_TYPE_NOT_SUPPORTED_FOR_BLOCK',
message: [
<strong key="filename">{ mediaFile.name }</strong>,
': ',
__( 'Sorry, this file type is not supported by this block.' ),
Copy link
Contributor

Choose a reason for hiding this comment

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

We're outside of the block's code in this function, it can be used in a block agnostic context. Should remove "block" from the message and the code?

Copy link
Member Author

Choose a reason for hiding this comment

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

👍🏻

],
file: mediaFile,
} );
return;
Expand Down
13 changes: 12 additions & 1 deletion packages/editor/src/utils/media-upload/test/media-upload.js
Expand Up @@ -24,8 +24,19 @@ describe( 'mediaUpload', () => {
} );

it( 'should do nothing on invalid image type', () => {
mediaUpload( { filesList: [ invalidMediaObj ], onFileChange: onFileChangeSpy, allowedType: 'image' } );
const onError = jest.fn();
mediaUpload( {
filesList: [ invalidMediaObj ],
onFileChange: onFileChangeSpy,
allowedType: 'image',
onError,
} );
expect( onFileChangeSpy ).not.toHaveBeenCalled();
expect( onError ).toBeCalledWith( {
code: 'MIME_TYPE_NOT_SUPPORTED_FOR_BLOCK',
file: invalidMediaObj,
message: 'Sorry, this file type is not supported by this block.',
} );
} );

it( 'should call error handler with the correct error object if file size is greater than the maximum', () => {
Expand Down