Skip to content

Enforce the file type of the featured image, background media, and video block#2737

Merged
swissspidy merged 19 commits intodevelopfrom
fix/upload-file-type
Jul 13, 2019
Merged

Enforce the file type of the featured image, background media, and video block#2737
swissspidy merged 19 commits intodevelopfrom
fix/upload-file-type

Conversation

@kienstra
Copy link
Copy Markdown
Contributor

@kienstra kienstra commented Jul 3, 2019

  • If the selected featured image, like a .mov file, is the wrong type, it displays a notice and disables the 'Select' button.
  • Applies to the featured image in the normal post block editor and the AMP Story editor. Though maybe it should only apply to the AMP Story editor.
  • Uses the allowedTypes property of the MediaUpload component.

featured-image-select

Here's the notice:

new-notice

Fixes #2727

kienstra added 3 commits July 2, 2019 19:51
This doesn't disable the 'Select' button now,
though it shows a notice both in the AMP Story
editor and the normal post or page block editor.
Before, this simply displayed a notice.
Now, it actually disables the button.
…tion

I didn't write this originally, but I used this
same logic for the file type notice.

So instead of simply copy and pasting it,
this extracts it into a separate function.
@googlebot googlebot added the cla: yes Signed the Google CLA label Jul 3, 2019
@westonruter
Copy link
Copy Markdown
Member

Does this also apply to the video block? Users should be disallowed from selecting or uploading anything but MP4 videos.

@kienstra
Copy link
Copy Markdown
Contributor Author

kienstra commented Jul 3, 2019

Does this also apply to the video block? Users should be disallowed from selecting or uploading anything but MP4 videos.

Hi @westonruter, no, it doesn't apply to the video block or to the 'Background Media' control. I still need to work on that.

@kienstra kienstra changed the title Enforce the file type of the featured image [WIP] Enforce the file type of the featured image Jul 3, 2019
kienstra added 6 commits July 3, 2019 19:12
Refactor much of the existing logic,
which was for featured images.
Rename a file and many functions.
There was duplicated logic,
so use that function in place of the copy-pasted code.
It looks like that wasn't preventing .mov files from appearing.
Also, the new logic uses ALLOWED_MEDIA_TYPES as passed in allowedTypes.
It checks that the attachment.get( 'type' ) matches that,
and the attachment.get( 'type' ) is never 'video/mp4'.
That is only the attachment.get( 'mime' ).
There was a file that should have been moved,
but I forgot to delete it.
There an error:
fatal: Path 'amp.php' exists on disk, but not in 'eb9bb06b77d811ba80ca70ec60fbe551aea4ca73'.
Pascal had suggested an empty commit for this.
It looks like this needed a 'select' handler.
So add one here.
@kienstra
Copy link
Copy Markdown
Contributor Author

kienstra commented Jul 4, 2019

Mainly Working
I Still Need To Test More

This is mainly working for the 'Background Media,' Core Video block, and the featured image.

If any of them have the wrong mime type, like 'video/quicktime' for a .mov file, it displays a notice and disables the 'Select' button:

uploading-video

The same applies for uploading other incorrect types, like .txt files.

I still need to test this more, and I'm working on a unit test for enforceFileType().

kienstra added 2 commits July 5, 2019 18:37
Add a mockClasses.js file to store some mocks.
And use this for the tests in isFileTypeAllowed.js.
@kienstra
Copy link
Copy Markdown
Contributor Author

kienstra commented Jul 6, 2019

The first 3 scenarios are new in this PR, the last 2 are to test regressions.

Scenario Expected behavior
1. AMP Story and normal post editors: upload as featured image a .txt or .mov file 'Select' button disabled, notice appears: "The selected file mime type ... is not allowed."
2. AMP Story Background Media: upload a .txt or .mov file Same as above
3. Core Video block only in AMP Story editor (not normal post editor): upload via the Media Library or 'Upload' button on block anything other than an .mp4 video, like an .mov Same as above
4. AMP Story editor: select or upload as featured image an image with dimensions smaller than1200 x 1600 Shouldn't change in this PR: should display the notice "The selected image is too small..."
5. AMP Story editor: select or upload as featured image an image with dimensions larger than 1200 x 1600, like 1900 x 1900 Shouldn't change in this PR: this should still display the cropper

@kienstra

This comment has been minimized.

kienstra added 4 commits July 5, 2019 21:01
That seems to have caused .mov files to appear
in the Background Media.
So revert the change to that,
and instead change isFileTypeAllowed()
to return true whene a mime type matches
a value in the allowedTypes array.
This PR already blocked selecting uploading them.
But if they were uploaded before, they would
still display in the 'Media Library' tab of the
Media Library.
So this blocks displaying them in that tab if they
were uploaded before.
This caused a failed Travis build,
as it was imported but not used.
Before, some of these were joined using a comma.
Instead, keep to the convention in the rest of this PR.
@kienstra
Copy link
Copy Markdown
Contributor Author

kienstra commented Jul 6, 2019

Request For Review

Hi @swissspidy and @westonruter,
Could you please review this when you can?

In the first 3 scenarios above, this displays a notice in the Media Library and disables the 'Select' button:

notice-select-disabled

Thanks, and have a great weekend!

@kienstra kienstra changed the title [WIP] Enforce the file type of the featured image Enforce the file type of the featured image Jul 6, 2019
@kienstra kienstra changed the title Enforce the file type of the featured image Enforce the file type of the featured image, background media, and video block Jul 6, 2019
There shouldn't have been a period
in the middle of the sentence.
Copy link
Copy Markdown
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

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

Attempting to select a MOV video to upload for page background successfully results in the Select button being disabled and a warning message displayed:

image

Same thing happened successfully when attempting to upload a video via the Media Library button in the video block:

image

Nevertheless, it does not successfully restrict the video type when selecting the Upload button from the video block:

image

@westonruter
Copy link
Copy Markdown
Member

Is applying the restriction to the Video block's Upload button too difficult to include here?

@kienstra
Copy link
Copy Markdown
Contributor Author

kienstra commented Jul 8, 2019

Video Block Upload Button

Hi @westonruter,

Is applying the restriction to the Video block's Upload button too difficult to include here?

Good question. I'll look at that tomorrow if that's alright.

It looks like if the accept prop were video/mp4 instead of video/*, that might restrict uploading to mp4 files.

Maybe there's a way to change that with the editor.BlockEdit filter.

kienstra added 2 commits July 8, 2019 19:12
…mp4' files

There was already logic to prevent uploading in the
Media Library.
But this applies to the 'Upload' button
in the block's Edit component, not the Media Library.
@kienstra
Copy link
Copy Markdown
Contributor Author

kienstra commented Jul 9, 2019

Video Upload Button

Hi @westonruter,

Is applying the restriction to the Video block's Upload button too difficult to include here?

Sure, a72b3e5 restricts the upload type of the Video block to `video/mp4':

only-allows-uploading-mp4

Sorry for the delay.

@westonruter westonruter added this to the v1.2.1 milestone Jul 9, 2019
Copy link
Copy Markdown
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

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

It works! I'll let @swissspidy do a final review of the JS prior to merging.

@kienstra
Copy link
Copy Markdown
Contributor Author

kienstra commented Jul 9, 2019

Thanks, @westonruter!

@kienstra
Copy link
Copy Markdown
Contributor Author

Request For Review

Hi @swissspidy,
Could you please review this?

Here's the expected behavior.

Thanks, and hope you have a great weekend.

@swissspidy swissspidy merged commit 8720098 into develop Jul 13, 2019
@swissspidy swissspidy deleted the fix/upload-file-type branch July 13, 2019 14:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: yes Signed the Google CLA

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Video upload does not work

4 participants