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

Editor: Make mime type config less confusing #10592

Closed
swissspidy opened this issue Feb 16, 2022 · 12 comments · Fixed by #10709
Closed

Editor: Make mime type config less confusing #10592

swissspidy opened this issue Feb 16, 2022 · 12 comments · Fixed by #10709
Assignees
Labels
P2 Should do soon Type: Code Quality Things that need a refactor, rewrite or just some good old developer ❤️

Comments

@swissspidy
Copy link
Collaborator

Task Description

We currently pass this list of mime/file types to the editor:

'allowedFileTypes' => $this->types->get_allowed_file_types(),
'allowedTranscodableMimeTypes' => $this->types->get_allowed_transcodable_mime_types(),
'allowedImageFileTypes' => $this->types->get_file_type_exts( $image_mime_types ),
'allowedImageMimeTypes' => $image_mime_types,
'allowedAudioFileTypes' => $this->types->get_file_type_exts( $audio_mime_types ),
'allowedAudioMimeTypes' => $audio_mime_types,
'allowedMimeTypes' => $mime_types,

As we are working on some documentation on using the decoupled story editor, it became more obvious how confusing this config is.

  • allowedMimeTypes: map of allowed mime types for actual story elements. For example, allowedMimeTypes.image might contain image/svg+xml or something to say that you can drop SVGs on the canvas
  • allowedImageMimeTypes : this is used for places in the design/document tab where you can upload images (video poster, story poster, publisher logo). For example, here we would never want to allow uploading SVGs. Hence this separate config.
  • allowedAudioMimeTypes: similarly, this is used for when you want to add background audio to a page.

In addition to that, allowedFileTypes is just a simple array of file types so that in the editor we can say “You are only allowed to upload jpeg, png, gif” etc. We can just get rid of this and use getExtensionFromMimeType from the media package instead.

allowedTranscodableMimeTypes is really pointless too, which is why I opened #10591 for that already.

To-do

  • Rename allowedMimeTypes to somehting more obvious, like for example allowedElementMimeTypes
  • Maybe rename allowedImageMimeTypes and allowedAudioMimeTypes to something more obvious too
  • Make allowedFileTypes obsolete by assembling that on-the-fly in the editor instead, using getExtensionFromMimeType
@swissspidy swissspidy added P1 High priority, must do soon Type: Code Quality Things that need a refactor, rewrite or just some good old developer ❤️ Pod: WP & Infra P2 Should do soon and removed P1 High priority, must do soon labels Feb 16, 2022
@spacedmonkey
Copy link
Contributor

I wonder if we should have one object with all this data. Something like this

const fileTypes = {
    images: {
       jpg: 'image/jpeg',
       jpeg: 'image/jpeg',
       png: 'image/png',
    }, 
    videos: {
        mp4: 'video/mp4',
    },
    audio: { ...}
}

So to get allowedImageFileTypes or allowedImageMimeTypes. You would do this.

const allowedImageFileTypes = Object.keys( fileTypes.images );
const allowedImageMimeTypes = Object.values( fileTypes.images );

There is also a difference between allow images types and list images that can be uploaded. One is used for allow images for posters. How would be best denote that?

@swissspidy
Copy link
Collaborator Author

Including file types in there is redundant and makes it harder to maintain for us and everyone trying to build an editor. We can operate with just mime types and then use getExtensionFromMimeType on the client-side.

There is also a difference between allow images types and list images that can be uploaded. One is used for allow images for posters. How would be best denote that?

The suggestion above is to rename the existing fields to make that more clear.

@spacedmonkey
Copy link
Contributor

We can operate with just mime types and then use getExtensionFromMimeType on the client-side.

Do we even need allowedFileTypes, allowedAudioFileTypes and allowedImageFileTypes then? Don't we just need the mime types? Meaning this could become.

const fileTypes = {
    images: ['image/jpeg', 'image/png',], 
    videos: ['video/mp4', 'video/webm'],
    audio: [ ... ]
}

So to get allowedImageFileTypes or allowedImageMimeTypes. You would do this.

const allowedImageMimeTypes  = fileTypes.images;
const allowedImageFileTypes = allowedImageMimeTypes.map( ( mimeType ) => getExtensionFromMimeType( mimeType )) ;

@swissspidy
Copy link
Collaborator Author

Precisely. That's what I meant in the ticket description by saying we can make it obsolete.

@ayushnirwal
Copy link
Contributor

There might be a problem with passing down mime types and getting an extension array in the case of image/jpeg since both .jpeg and .jpg have the mime-type image/jpeg.
if allowedImageMimeTypes is ['image/jpeg'] allowedImageFileTypes would be ['jpeg'] but MediaUpload would accept both jpeg and jpg files.

@swissspidy
Copy link
Collaborator Author

Thanks for flagging that. Agreed that we need to keep that in mind.

As far as I know we only use the file extensions to display user-facing messages like You can upload jpeg, jpg, png, mp4. For MediaUpload and others, the checks are based on the mime type.

If the user-facing message now says You can upload jpeg, png, mp4 (omitting jpg and FWIW also jpe), that is OK.

@spacedmonkey
Copy link
Contributor

There is a similar argument for mp4/m4v files as well. Also what if developers add a custom file type, like videopress or a new file format. It may not be part of the mine type package.

@swissspidy
Copy link
Collaborator Author

I'm not concerned about invalid mime types, we don't really support that.

@spacedmonkey
Copy link
Contributor

My orignal suggestion #10592 (comment) would handle this issue.

As you can see in my sudo code shows, that structure can support multiple file extensions with the same mime types.

@swissspidy
Copy link
Collaborator Author

Not catering for mime types with multiple corresponding file extensions is fine, because file extension info is only used for error messages.

Again, I want us to avoid having to pass file extensions via config. It's redundant. So let's try it first without. If it doesn't work for some reason we can always revisit.

And something like video/videopress is not supported anyway because it's an invalid mime type.

@spacedmonkey spacedmonkey self-assigned this Feb 24, 2022
@spacedmonkey
Copy link
Contributor

I have a POC PR up here #10709.

I noticed a problem. The getExtensionFromMimeType function doesn't work as expected. Some functions return odd file extensions or nothing at all.

Screenshot 2022-02-25 at 10 19 02

Take this example. audio/mpeg, returns mpga, not mp3 as expected. audio/acc returns null and not acc file.

This is an issue with the mimetype library being used. This will have to be fixed or replaces. Can you advice @swissspidy

@swissspidy
Copy link
Collaborator Author

It seems to work as expected, although it's definitely confusing at first.

This is related to @ayushnirwal's comment above.

audio/mpeg maps to multiple file extensions: mpga,mp2,mp2a,mp3,m2a,m3a. You're getting the first one.

The mime package doesn't offer a way to get all of them, see broofa/mime#254

audio/aac returns nothing because it has no extension defined in mime-db but audio/x-aac does, see https://github.com/jshttp/mime-db/blob/ebb6bf92ea39d3168a50942295d5edfdcdce641a/db.json#L6367-L6369 and jshttp/mime-db#81

It's not a huge deal because again, this is only used for error messages.

To make it less confusing it should be fine to have a hardcoded list of mime types for these cases

See #10718 for my suggestion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P2 Should do soon Type: Code Quality Things that need a refactor, rewrite or just some good old developer ❤️
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants