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

Fix mp3 uploads on chrome #7398

Merged
merged 1 commit into from
Jun 21, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
28 changes: 26 additions & 2 deletions utils/mediaupload.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External Dependencies
*/
import { compact, forEach, get, includes, noop, startsWith } from 'lodash';
import { compact, flatMap, forEach, get, includes, map, noop, startsWith } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -13,6 +13,30 @@ import { __, sprintf } from '@wordpress/i18n';
*/
import apiRequest from '@wordpress/api-request';

/**
* Browsers may use unexpected mime types, and they differ from browser to browser.
* This function computes a flexible array of mime types from the mime type structured provided by the server.
* Converts { jpg|jpeg|jpe: "image/jpeg" } into [ "image/jpeg", "image/jpg", "image/jpeg", "image/jpe" ]
* The computation of this array instead of directly using the object,
* solves the problem in chrome where mp3 files have audio/mp3 as mime type instead of audio/mpeg.
* https://bugs.chromium.org/p/chromium/issues/detail?id=227004
*
* @param {?Object} wpMimeTypesObject Mime type object received from the server.
* Extensions are keys separated by '|' and values are mime types associated with an extension.
*
* @return {?Array} Media Object Promise.
Copy link
Member

Choose a reason for hiding this comment

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

Return description is inaccurate (copy-pasta from createMediaFromFile 🍝 )

*/
export function getMimeTypesArray( wpMimeTypesObject ) {
if ( ! wpMimeTypesObject ) {
return wpMimeTypesObject;
}
return flatMap( wpMimeTypesObject, ( mime, extensionsString ) => {
const [ type ] = mime.split( '/' );
const extensions = extensionsString.split( '|' );
return [ mime, ...map( extensions, ( extension ) => `${ type }/${ extension }` ) ];
} );
}

/**
* Media Upload is used by audio, image, gallery and video blocks to handle uploading a media file
* when a file upload button is activated.
Expand Down Expand Up @@ -48,7 +72,7 @@ export function mediaUpload( {
const isAllowedType = ( fileType ) => startsWith( fileType, `${ allowedType }/` );

// Allowed types for the current WP_User
const allowedMimeTypesForUser = get( window, [ '_wpMediaSettings', 'allowedMimeTypes' ] );
const allowedMimeTypesForUser = getMimeTypesArray( get( window, [ '_wpMediaSettings', 'allowedMimeTypes' ] ) );
const isAllowedMimeTypeForUser = ( fileType ) => {
return includes( allowedMimeTypesForUser, fileType );
};
Expand Down
49 changes: 48 additions & 1 deletion utils/test/mediaupload.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Internal dependencies
*/
import { mediaUpload } from '../mediaupload';
import { mediaUpload, getMimeTypesArray } from '../mediaupload';

// mediaUpload is passed the onImagesChange function
// so we can stub that out have it pass the data to
Expand Down Expand Up @@ -79,3 +79,50 @@ describe( 'mediaUpload', () => {
} );
} );
} );

describe( 'getMimeTypesArray', () => {
it( 'should return the parameter passed if it is "falsy" e.g: undefined or null', () => {
expect( getMimeTypesArray( null ) ).toEqual( null );
expect( getMimeTypesArray( undefined ) ).toEqual( undefined );
} );

it( 'should return an empty array if an empty object is passed', () => {
expect( getMimeTypesArray( {} ) ).toEqual( [] );
} );

it( 'should return the type plus a new mime type with type and subtype with the extension if a type is passed', () => {
expect(
getMimeTypesArray( { ext: 'chicken' } )
).toEqual(
[ 'chicken', 'chicken/ext' ]
);
} );

it( 'should return the mime type passed and a new mime type with type and the extension as subtype', () => {
expect(
getMimeTypesArray( { ext: 'chicken/ribs' } )
).toEqual(
[ 'chicken/ribs', 'chicken/ext' ]
);
} );

it( 'should return the mime type passed and an additional mime type per extension supported', () => {
expect(
getMimeTypesArray( { 'jpg|jpeg|jpe': 'image/jpeg' } )
).toEqual(
[ 'image/jpeg', 'image/jpg', 'image/jpeg', 'image/jpe' ]
);
} );

it( 'should handle multiple mime types', () => {
expect(
getMimeTypesArray( { 'ext|aaa': 'chicken/ribs', aaa: 'bbb' } )
).toEqual( [
'chicken/ribs',
'chicken/ext',
'chicken/aaa',
'bbb',
'bbb/aaa',
] );
} );
} );