@wordpress/media-utils 3.0.3 → 3.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -126,7 +126,7 @@ async function uploadMedia({
126
126
  if (allowedMimeTypesForUser && mediaFile.type && !isAllowedMimeTypeForUser(mediaFile.type)) {
127
127
  triggerError({
128
128
  code: 'MIME_TYPE_NOT_ALLOWED_FOR_USER',
129
- message: (0, _i18n.__)('Sorry, this file type is not permitted for security reasons.'),
129
+ message: (0, _i18n.__)('Sorry, you are not allowed to upload this file type.'),
130
130
  file: mediaFile
131
131
  });
132
132
  continue;
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/media-utils/src/utils/upload-media.js"],"names":["getMimeTypesArray","wpMimeTypesObject","mime","extensionsString","type","split","extensions","extension","uploadMedia","allowedTypes","additionalData","filesList","maxUploadFileSize","onError","noop","onFileChange","wpAllowedMimeTypes","files","filesSet","setAndUpdateFiles","idx","value","isAllowedType","fileType","allowedType","allowedMimeTypesForUser","isAllowedMimeTypeForUser","triggerError","error","message","file","name","validFiles","mediaFile","code","size","push","url","length","savedMedia","createMediaFromFile","mediaObject","alt","alt_text","caption","title","raw","source_url","data","window","FormData","append","replace","key","path","body","method"],"mappings":";;;;;;;;;;;;AAGA;;AAiBA;;AACA;;AACA;;AAtBA;AACA;AACA;;AAeA;AACA;AACA;;AAKA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASA,iBAAT,CAA4BC,iBAA5B,EAAgD;AACtD,MAAK,CAAEA,iBAAP,EAA2B;AAC1B,WAAOA,iBAAP;AACA;;AACD,SAAO,qBAASA,iBAAT,EAA4B,CAAEC,IAAF,EAAQC,gBAAR,KAA8B;AAChE,UAAM,CAAEC,IAAF,IAAWF,IAAI,CAACG,KAAL,CAAY,GAAZ,CAAjB;AACA,UAAMC,UAAU,GAAGH,gBAAgB,CAACE,KAAjB,CAAwB,GAAxB,CAAnB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EAAO,IAAP,CAAjB;AACA,UAAIS,OAAJ;;AACA,UAAK,iBAAKD,KAAL,EAAY,CAAE,SAAF,CAAZ,CAAL,EAAmC;AAClCC,QAAAA,OAAO,GAAG,iBAAKD,KAAL,EAAY,CAAE,SAAF,CAAZ,CAAV;AACA,OAFD,MAEO;AACNC,QAAAA,OAAO,GAAG,oBACT;AACA,sBAAI,qDAAJ,CAFS,EAGTI,SAAS,CAACF,IAHD,CAAV;AAKA;;AACDlB,MAAAA,OAAO,CAAE;AACRqB,QAAAA,IAAI,EAAE,SADE;AAERL,QAAAA,OAFQ;AAGRC,QAAAA,IAAI,EAAEG;AAHE,OAAF,CAAP;AAKA;AACD;AACD;AAED;AACA;AACA;AACA;AACA;AACA;;;AACA,SAASO,mBAAT,CAA8BV,IAA9B,EAAoCpB,cAApC,EAAqD;AACpD;AACA,QAAMsC,IAAI,GAAG,IAAIC,MAAM,CAACC,QAAX,EAAb;AACAF,EAAAA,IAAI,CAACG,MAAL,CAAa,MAAb,EAAqBrB,IAArB,EAA2BA,IAAI,CAACC,IAAL,IAAaD,IAAI,CAAC1B,IAAL,CAAUgD,OAAV,CAAmB,GAAnB,EAAwB,GAAxB,CAAxC;AACA,uBAAS1C,cAAT,EAAyB,CAAEW,KAAF,EAASgC,GAAT,KAAkBL,IAAI,CAACG,MAAL,CAAaE,GAAb,EAAkBhC,KAAlB,CAA3C;AACA,SAAO,uBAAU;AAChBiC,IAAAA,IAAI,EAAE,cADU;AAEhBC,IAAAA,IAAI,EAAEP,IAFU;AAGhBQ,IAAAA,MAAM,EAAE;AAHQ,GAAV,CAAP;AAKA","sourcesContent":["/**\n * External dependencies\n */\nimport {\n\tcompact,\n\tflatMap,\n\tforEach,\n\tget,\n\thas,\n\tincludes,\n\tmap,\n\tnoop,\n\tomit,\n\tsome,\n\tstartsWith,\n} from 'lodash';\n\n/**\n * WordPress dependencies\n */\nimport apiFetch from '@wordpress/api-fetch';\nimport { createBlobURL, revokeBlobURL } from '@wordpress/blob';\nimport { __, sprintf } from '@wordpress/i18n';\n\n/**\n * Browsers may use unexpected mime types, and they differ from browser to browser.\n * This function computes a flexible array of mime types from the mime type structured provided by the server.\n * Converts { jpg|jpeg|jpe: \"image/jpeg\" } into [ \"image/jpeg\", \"image/jpg\", \"image/jpeg\", \"image/jpe\" ]\n * The computation of this array instead of directly using the object,\n * solves the problem in chrome where mp3 files have audio/mp3 as mime type instead of audio/mpeg.\n * https://bugs.chromium.org/p/chromium/issues/detail?id=227004\n *\n * @param {?Object} wpMimeTypesObject Mime type object received from the server.\n * Extensions are keys separated by '|' and values are mime types associated with an extension.\n *\n * @return {?Array} An array of mime types or the parameter passed if it was \"falsy\".\n */\nexport function getMimeTypesArray( wpMimeTypesObject ) {\n\tif ( ! wpMimeTypesObject ) {\n\t\treturn wpMimeTypesObject;\n\t}\n\treturn flatMap( wpMimeTypesObject, ( mime, extensionsString ) => {\n\t\tconst [ type ] = mime.split( '/' );\n\t\tconst extensions = extensionsString.split( '|' );\n\t\treturn [\n\t\t\tmime,\n\t\t\t...map( extensions, ( extension ) => `${ type }/${ extension }` ),\n\t\t];\n\t} );\n}\n\n/**\n *\tMedia Upload is used by audio, image, gallery, video, and file blocks to\n *\thandle uploading a media file when a file upload button is activated.\n *\n *\tTODO: future enhancement to add an upload indicator.\n *\n * @param {Object} $0 Parameters object passed to the function.\n * @param {?Array} $0.allowedTypes Array with the types of media that can be uploaded, if unset all types are allowed.\n * @param {?Object} $0.additionalData Additional data to include in the request.\n * @param {Array} $0.filesList List of files.\n * @param {?number} $0.maxUploadFileSize Maximum upload size in bytes allowed for the site.\n * @param {Function} $0.onError Function called when an error happens.\n * @param {Function} $0.onFileChange Function called each time a file or a temporary representation of the file is available.\n * @param {?Object} $0.wpAllowedMimeTypes List of allowed mime types and file extensions.\n */\nexport async function uploadMedia( {\n\tallowedTypes,\n\tadditionalData = {},\n\tfilesList,\n\tmaxUploadFileSize,\n\tonError = noop,\n\tonFileChange,\n\twpAllowedMimeTypes = null,\n} ) {\n\t// Cast filesList to array\n\tconst files = [ ...filesList ];\n\n\tconst filesSet = [];\n\tconst setAndUpdateFiles = ( idx, value ) => {\n\t\trevokeBlobURL( get( filesSet, [ idx, 'url' ] ) );\n\t\tfilesSet[ idx ] = value;\n\t\tonFileChange( compact( filesSet ) );\n\t};\n\n\t// Allowed type specified by consumer\n\tconst isAllowedType = ( fileType ) => {\n\t\tif ( ! allowedTypes ) {\n\t\t\treturn true;\n\t\t}\n\t\treturn some( allowedTypes, ( allowedType ) => {\n\t\t\t// If a complete mimetype is specified verify if it matches exactly the mime type of the file.\n\t\t\tif ( includes( allowedType, '/' ) ) {\n\t\t\t\treturn allowedType === fileType;\n\t\t\t}\n\t\t\t// Otherwise a general mime type is used and we should verify if the file mimetype starts with it.\n\t\t\treturn startsWith( fileType, `${ allowedType }/` );\n\t\t} );\n\t};\n\n\t// Allowed types for the current WP_User\n\tconst allowedMimeTypesForUser = getMimeTypesArray( wpAllowedMimeTypes );\n\tconst isAllowedMimeTypeForUser = ( fileType ) => {\n\t\treturn includes( allowedMimeTypesForUser, fileType );\n\t};\n\n\t// Build the error message including the filename\n\tconst triggerError = ( error ) => {\n\t\terror.message = [\n\t\t\t<strong key=\"filename\">{ error.file.name }</strong>,\n\t\t\t': ',\n\t\t\terror.message,\n\t\t];\n\n\t\tonError( error );\n\t};\n\n\tconst validFiles = [];\n\n\tfor ( const mediaFile of files ) {\n\t\t// Verify if user is allowed to upload this mime type.\n\t\t// Defer to the server when type not detected.\n\t\tif (\n\t\t\tallowedMimeTypesForUser &&\n\t\t\tmediaFile.type &&\n\t\t\t! isAllowedMimeTypeForUser( mediaFile.type )\n\t\t) {\n\t\t\ttriggerError( {\n\t\t\t\tcode: 'MIME_TYPE_NOT_ALLOWED_FOR_USER',\n\t\t\t\tmessage: __(\n\t\t\t\t\t'Sorry, this file type is not permitted for security reasons.'\n\t\t\t\t),\n\t\t\t\tfile: mediaFile,\n\t\t\t} );\n\t\t\tcontinue;\n\t\t}\n\n\t\t// Check if the block supports this mime type.\n\t\t// Defer to the server when type not detected.\n\t\tif ( mediaFile.type && ! isAllowedType( mediaFile.type ) ) {\n\t\t\ttriggerError( {\n\t\t\t\tcode: 'MIME_TYPE_NOT_SUPPORTED',\n\t\t\t\tmessage: __( 'Sorry, this file type is not supported here.' ),\n\t\t\t\tfile: mediaFile,\n\t\t\t} );\n\t\t\tcontinue;\n\t\t}\n\n\t\t// verify if file is greater than the maximum file upload size allowed for the site.\n\t\tif ( maxUploadFileSize && mediaFile.size > maxUploadFileSize ) {\n\t\t\ttriggerError( {\n\t\t\t\tcode: 'SIZE_ABOVE_LIMIT',\n\t\t\t\tmessage: __(\n\t\t\t\t\t'This file exceeds the maximum upload size for this site.'\n\t\t\t\t),\n\t\t\t\tfile: mediaFile,\n\t\t\t} );\n\t\t\tcontinue;\n\t\t}\n\n\t\t// Don't allow empty files to be uploaded.\n\t\tif ( mediaFile.size <= 0 ) {\n\t\t\ttriggerError( {\n\t\t\t\tcode: 'EMPTY_FILE',\n\t\t\t\tmessage: __( 'This file is empty.' ),\n\t\t\t\tfile: mediaFile,\n\t\t\t} );\n\t\t\tcontinue;\n\t\t}\n\n\t\tvalidFiles.push( mediaFile );\n\n\t\t// Set temporary URL to create placeholder media file, this is replaced\n\t\t// with final file from media gallery when upload is `done` below\n\t\tfilesSet.push( { url: createBlobURL( mediaFile ) } );\n\t\tonFileChange( filesSet );\n\t}\n\n\tfor ( let idx = 0; idx < validFiles.length; ++idx ) {\n\t\tconst mediaFile = validFiles[ idx ];\n\t\ttry {\n\t\t\tconst savedMedia = await createMediaFromFile(\n\t\t\t\tmediaFile,\n\t\t\t\tadditionalData\n\t\t\t);\n\t\t\tconst mediaObject = {\n\t\t\t\t...omit( savedMedia, [ 'alt_text', 'source_url' ] ),\n\t\t\t\talt: savedMedia.alt_text,\n\t\t\t\tcaption: get( savedMedia, [ 'caption', 'raw' ], '' ),\n\t\t\t\ttitle: savedMedia.title.raw,\n\t\t\t\turl: savedMedia.source_url,\n\t\t\t};\n\t\t\tsetAndUpdateFiles( idx, mediaObject );\n\t\t} catch ( error ) {\n\t\t\t// Reset to empty on failure.\n\t\t\tsetAndUpdateFiles( idx, null );\n\t\t\tlet message;\n\t\t\tif ( has( error, [ 'message' ] ) ) {\n\t\t\t\tmessage = get( error, [ 'message' ] );\n\t\t\t} else {\n\t\t\t\tmessage = sprintf(\n\t\t\t\t\t// translators: %s: file name\n\t\t\t\t\t__( 'Error while uploading file %s to the media library.' ),\n\t\t\t\t\tmediaFile.name\n\t\t\t\t);\n\t\t\t}\n\t\t\tonError( {\n\t\t\t\tcode: 'GENERAL',\n\t\t\t\tmessage,\n\t\t\t\tfile: mediaFile,\n\t\t\t} );\n\t\t}\n\t}\n}\n\n/**\n * @param {File} file Media File to Save.\n * @param {?Object} additionalData Additional data to include in the request.\n *\n * @return {Promise} Media Object Promise.\n */\nfunction createMediaFromFile( file, additionalData ) {\n\t// Create upload payload\n\tconst data = new window.FormData();\n\tdata.append( 'file', file, file.name || file.type.replace( '/', '.' ) );\n\tforEach( additionalData, ( value, key ) => data.append( key, value ) );\n\treturn apiFetch( {\n\t\tpath: '/wp/v2/media',\n\t\tbody: data,\n\t\tmethod: 'POST',\n\t} );\n}\n"]}
1
+ {"version":3,"sources":["@wordpress/media-utils/src/utils/upload-media.js"],"names":["getMimeTypesArray","wpMimeTypesObject","mime","extensionsString","type","split","extensions","extension","uploadMedia","allowedTypes","additionalData","filesList","maxUploadFileSize","onError","noop","onFileChange","wpAllowedMimeTypes","files","filesSet","setAndUpdateFiles","idx","value","isAllowedType","fileType","allowedType","allowedMimeTypesForUser","isAllowedMimeTypeForUser","triggerError","error","message","file","name","validFiles","mediaFile","code","size","push","url","length","savedMedia","createMediaFromFile","mediaObject","alt","alt_text","caption","title","raw","source_url","data","window","FormData","append","replace","key","path","body","method"],"mappings":";;;;;;;;;;;;AAGA;;AAiBA;;AACA;;AACA;;AAtBA;AACA;AACA;;AAeA;AACA;AACA;;AAKA;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SAAO,uBAAU;AAChBiC,IAAAA,IAAI,EAAE,cADU;AAEhBC,IAAAA,IAAI,EAAEP,IAFU;AAGhBQ,IAAAA,MAAM,EAAE;AAHQ,GAAV,CAAP;AAKA","sourcesContent":["/**\n * External dependencies\n */\nimport {\n\tcompact,\n\tflatMap,\n\tforEach,\n\tget,\n\thas,\n\tincludes,\n\tmap,\n\tnoop,\n\tomit,\n\tsome,\n\tstartsWith,\n} from 'lodash';\n\n/**\n * WordPress dependencies\n */\nimport apiFetch from '@wordpress/api-fetch';\nimport { createBlobURL, revokeBlobURL } from '@wordpress/blob';\nimport { __, sprintf } from '@wordpress/i18n';\n\n/**\n * Browsers may use unexpected mime types, and they differ from browser to browser.\n * This function computes a flexible array of mime types from the mime type structured provided by the server.\n * Converts { jpg|jpeg|jpe: \"image/jpeg\" } into [ \"image/jpeg\", \"image/jpg\", \"image/jpeg\", \"image/jpe\" ]\n * The computation of this array instead of directly using the object,\n * solves the problem in chrome where mp3 files have audio/mp3 as mime type instead of audio/mpeg.\n * https://bugs.chromium.org/p/chromium/issues/detail?id=227004\n *\n * @param {?Object} wpMimeTypesObject Mime type object received from the server.\n * Extensions are keys separated by '|' and values are mime types associated with an extension.\n *\n * @return {?Array} An array of mime types or the parameter passed if it was \"falsy\".\n */\nexport function getMimeTypesArray( wpMimeTypesObject ) {\n\tif ( ! wpMimeTypesObject ) {\n\t\treturn wpMimeTypesObject;\n\t}\n\treturn flatMap( wpMimeTypesObject, ( mime, extensionsString ) => {\n\t\tconst [ type ] = mime.split( '/' );\n\t\tconst extensions = extensionsString.split( '|' );\n\t\treturn [\n\t\t\tmime,\n\t\t\t...map( extensions, ( extension ) => `${ type }/${ extension }` ),\n\t\t];\n\t} );\n}\n\n/**\n *\tMedia Upload is used by audio, image, gallery, video, and file blocks to\n *\thandle uploading a media file when a file upload button is activated.\n *\n *\tTODO: future enhancement to add an upload indicator.\n *\n * @param {Object} $0 Parameters object passed to the function.\n * @param {?Array} $0.allowedTypes Array with the types of media that can be uploaded, if unset all types are allowed.\n * @param {?Object} $0.additionalData Additional data to include in the request.\n * @param {Array} $0.filesList List of files.\n * @param {?number} $0.maxUploadFileSize Maximum upload size in bytes allowed for the site.\n * @param {Function} $0.onError Function called when an error happens.\n * @param {Function} $0.onFileChange Function called each time a file or a temporary representation of the file is available.\n * @param {?Object} $0.wpAllowedMimeTypes List of allowed mime types and file extensions.\n */\nexport async function uploadMedia( {\n\tallowedTypes,\n\tadditionalData = {},\n\tfilesList,\n\tmaxUploadFileSize,\n\tonError = noop,\n\tonFileChange,\n\twpAllowedMimeTypes = null,\n} ) {\n\t// Cast filesList to array\n\tconst files = [ ...filesList ];\n\n\tconst filesSet = [];\n\tconst setAndUpdateFiles = ( idx, value ) => {\n\t\trevokeBlobURL( get( filesSet, [ idx, 'url' ] ) );\n\t\tfilesSet[ idx ] = value;\n\t\tonFileChange( compact( filesSet ) );\n\t};\n\n\t// Allowed type specified by consumer\n\tconst isAllowedType = ( fileType ) => {\n\t\tif ( ! allowedTypes ) {\n\t\t\treturn true;\n\t\t}\n\t\treturn some( allowedTypes, ( allowedType ) => {\n\t\t\t// If a complete mimetype is specified verify if it matches exactly the mime type of the file.\n\t\t\tif ( includes( allowedType, '/' ) ) {\n\t\t\t\treturn allowedType === fileType;\n\t\t\t}\n\t\t\t// Otherwise a general mime type is used and we should verify if the file mimetype starts with it.\n\t\t\treturn startsWith( fileType, `${ allowedType }/` );\n\t\t} );\n\t};\n\n\t// Allowed types for the current WP_User\n\tconst allowedMimeTypesForUser = getMimeTypesArray( wpAllowedMimeTypes );\n\tconst isAllowedMimeTypeForUser = ( fileType ) => {\n\t\treturn includes( allowedMimeTypesForUser, fileType );\n\t};\n\n\t// Build the error message including the filename\n\tconst triggerError = ( error ) => {\n\t\terror.message = [\n\t\t\t<strong key=\"filename\">{ error.file.name }</strong>,\n\t\t\t': ',\n\t\t\terror.message,\n\t\t];\n\n\t\tonError( error );\n\t};\n\n\tconst validFiles = [];\n\n\tfor ( const mediaFile of files ) {\n\t\t// Verify if user is allowed to upload this mime type.\n\t\t// Defer to the server when type not detected.\n\t\tif (\n\t\t\tallowedMimeTypesForUser &&\n\t\t\tmediaFile.type &&\n\t\t\t! isAllowedMimeTypeForUser( mediaFile.type )\n\t\t) {\n\t\t\ttriggerError( {\n\t\t\t\tcode: 'MIME_TYPE_NOT_ALLOWED_FOR_USER',\n\t\t\t\tmessage: __(\n\t\t\t\t\t'Sorry, you are not allowed to upload this file type.'\n\t\t\t\t),\n\t\t\t\tfile: mediaFile,\n\t\t\t} );\n\t\t\tcontinue;\n\t\t}\n\n\t\t// Check if the block supports this mime type.\n\t\t// Defer to the server when type not detected.\n\t\tif ( mediaFile.type && ! isAllowedType( mediaFile.type ) ) {\n\t\t\ttriggerError( {\n\t\t\t\tcode: 'MIME_TYPE_NOT_SUPPORTED',\n\t\t\t\tmessage: __( 'Sorry, this file type is not supported here.' ),\n\t\t\t\tfile: mediaFile,\n\t\t\t} );\n\t\t\tcontinue;\n\t\t}\n\n\t\t// verify if file is greater than the maximum file upload size allowed for the site.\n\t\tif ( maxUploadFileSize && mediaFile.size > maxUploadFileSize ) {\n\t\t\ttriggerError( {\n\t\t\t\tcode: 'SIZE_ABOVE_LIMIT',\n\t\t\t\tmessage: __(\n\t\t\t\t\t'This file exceeds the maximum upload size for this site.'\n\t\t\t\t),\n\t\t\t\tfile: mediaFile,\n\t\t\t} );\n\t\t\tcontinue;\n\t\t}\n\n\t\t// Don't allow empty files to be uploaded.\n\t\tif ( mediaFile.size <= 0 ) {\n\t\t\ttriggerError( {\n\t\t\t\tcode: 'EMPTY_FILE',\n\t\t\t\tmessage: __( 'This file is empty.' ),\n\t\t\t\tfile: mediaFile,\n\t\t\t} );\n\t\t\tcontinue;\n\t\t}\n\n\t\tvalidFiles.push( mediaFile );\n\n\t\t// Set temporary URL to create placeholder media file, this is replaced\n\t\t// with final file from media gallery when upload is `done` below\n\t\tfilesSet.push( { url: createBlobURL( mediaFile ) } );\n\t\tonFileChange( filesSet );\n\t}\n\n\tfor ( let idx = 0; idx < validFiles.length; ++idx ) {\n\t\tconst mediaFile = validFiles[ idx ];\n\t\ttry {\n\t\t\tconst savedMedia = await createMediaFromFile(\n\t\t\t\tmediaFile,\n\t\t\t\tadditionalData\n\t\t\t);\n\t\t\tconst mediaObject = {\n\t\t\t\t...omit( savedMedia, [ 'alt_text', 'source_url' ] ),\n\t\t\t\talt: savedMedia.alt_text,\n\t\t\t\tcaption: get( savedMedia, [ 'caption', 'raw' ], '' ),\n\t\t\t\ttitle: savedMedia.title.raw,\n\t\t\t\turl: savedMedia.source_url,\n\t\t\t};\n\t\t\tsetAndUpdateFiles( idx, mediaObject );\n\t\t} catch ( error ) {\n\t\t\t// Reset to empty on failure.\n\t\t\tsetAndUpdateFiles( idx, null );\n\t\t\tlet message;\n\t\t\tif ( has( error, [ 'message' ] ) ) {\n\t\t\t\tmessage = get( error, [ 'message' ] );\n\t\t\t} else {\n\t\t\t\tmessage = sprintf(\n\t\t\t\t\t// translators: %s: file name\n\t\t\t\t\t__( 'Error while uploading file %s to the media library.' ),\n\t\t\t\t\tmediaFile.name\n\t\t\t\t);\n\t\t\t}\n\t\t\tonError( {\n\t\t\t\tcode: 'GENERAL',\n\t\t\t\tmessage,\n\t\t\t\tfile: mediaFile,\n\t\t\t} );\n\t\t}\n\t}\n}\n\n/**\n * @param {File} file Media File to Save.\n * @param {?Object} additionalData Additional data to include in the request.\n *\n * @return {Promise} Media Object Promise.\n */\nfunction createMediaFromFile( file, additionalData ) {\n\t// Create upload payload\n\tconst data = new window.FormData();\n\tdata.append( 'file', file, file.name || file.type.replace( '/', '.' ) );\n\tforEach( additionalData, ( value, key ) => data.append( key, value ) );\n\treturn apiFetch( {\n\t\tpath: '/wp/v2/media',\n\t\tbody: data,\n\t\tmethod: 'POST',\n\t} );\n}\n"]}
@@ -111,7 +111,7 @@ export async function uploadMedia({
111
111
  if (allowedMimeTypesForUser && mediaFile.type && !isAllowedMimeTypeForUser(mediaFile.type)) {
112
112
  triggerError({
113
113
  code: 'MIME_TYPE_NOT_ALLOWED_FOR_USER',
114
- message: __('Sorry, this file type is not permitted for security reasons.'),
114
+ message: __('Sorry, you are not allowed to upload this file type.'),
115
115
  file: mediaFile
116
116
  });
117
117
  continue;
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/media-utils/src/utils/upload-media.js"],"names":["compact","flatMap","forEach","get","has","includes","map","noop","omit","some","startsWith","apiFetch","createBlobURL","revokeBlobURL","__","sprintf","getMimeTypesArray","wpMimeTypesObject","mime","extensionsString","type","split","extensions","extension","uploadMedia","allowedTypes","additionalData","filesList","maxUploadFileSize","onError","onFileChange","wpAllowedMimeTypes","files","filesSet","setAndUpdateFiles","idx","value","isAllowedType","fileType","allowedType","allowedMimeTypesForUser","isAllowedMimeTypeForUser","triggerError","error","message","file","name","validFiles","mediaFile","code","size","push","url","length","savedMedia","createMediaFromFile","mediaObject","alt","alt_text","caption","title","raw","source_url","data","window","FormData","append","replace","key","path","body","method"],"mappings":";;AAAA;AACA;AACA;AACA,SACCA,OADD,EAECC,OAFD,EAGCC,OAHD,EAICC,GAJD,EAKCC,GALD,EAMCC,QAND,EAOCC,GAPD,EAQCC,IARD,EASCC,IATD,EAUCC,IAVD,EAWCC,UAXD,QAYO,QAZP;AAcA;AACA;AACA;;AACA,OAAOC,QAAP,MAAqB,sBAArB;AACA,SAASC,aAAT,EAAwBC,aAAxB,QAA6C,iBAA7C;AACA,SAASC,EAAT,EAAaC,OAAb,QAA4B,iBAA5B;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASC,iBAAT,CAA4BC,iBAA5B,EAAgD;AACtD,MAAK,CAAEA,iBAAP,EAA2B;AAC1B,WAAOA,iBAAP;AACA;;AACD,SAAOhB,OAAO,CAAEgB,iBAAF,EAAqB,CAAEC,IAAF,EAAQC,gBAAR,KAA8B;AAChE,UAAM,CAAEC,IAAF,IAAWF,IAAI,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AAEP,IAFU;AAGhBQ,IAAAA,MAAM,EAAE;AAHQ,GAAF,CAAf;AAKA","sourcesContent":["/**\n * External dependencies\n */\nimport {\n\tcompact,\n\tflatMap,\n\tforEach,\n\tget,\n\thas,\n\tincludes,\n\tmap,\n\tnoop,\n\tomit,\n\tsome,\n\tstartsWith,\n} from 'lodash';\n\n/**\n * WordPress dependencies\n */\nimport apiFetch from '@wordpress/api-fetch';\nimport { createBlobURL, revokeBlobURL } from '@wordpress/blob';\nimport { __, sprintf } from '@wordpress/i18n';\n\n/**\n * Browsers may use unexpected mime types, and they differ from browser to browser.\n * This function computes a flexible array of mime types from the mime type structured provided by the server.\n * Converts { jpg|jpeg|jpe: \"image/jpeg\" } into [ \"image/jpeg\", \"image/jpg\", \"image/jpeg\", \"image/jpe\" ]\n * The computation of this array instead of directly using the object,\n * solves the problem in chrome where mp3 files have audio/mp3 as mime type instead of audio/mpeg.\n * https://bugs.chromium.org/p/chromium/issues/detail?id=227004\n *\n * @param {?Object} wpMimeTypesObject Mime type object received from the server.\n * Extensions are keys separated by '|' and values are mime types associated with an extension.\n *\n * @return {?Array} An array of mime types or the parameter passed if it was \"falsy\".\n */\nexport function getMimeTypesArray( wpMimeTypesObject ) {\n\tif ( ! wpMimeTypesObject ) {\n\t\treturn wpMimeTypesObject;\n\t}\n\treturn flatMap( wpMimeTypesObject, ( mime, extensionsString ) => {\n\t\tconst [ type ] = mime.split( '/' );\n\t\tconst extensions = extensionsString.split( '|' );\n\t\treturn [\n\t\t\tmime,\n\t\t\t...map( extensions, ( extension ) => `${ type }/${ extension }` ),\n\t\t];\n\t} );\n}\n\n/**\n *\tMedia Upload is used by audio, image, gallery, video, and file blocks to\n *\thandle uploading a media file when a file upload button is activated.\n *\n *\tTODO: future enhancement to add an upload indicator.\n *\n * @param {Object} $0 Parameters object passed to the function.\n * @param {?Array} $0.allowedTypes Array with the types of media that can be uploaded, if unset all types are allowed.\n * @param {?Object} $0.additionalData Additional data to include in the request.\n * @param {Array} $0.filesList List of files.\n * @param {?number} $0.maxUploadFileSize Maximum upload size in bytes allowed for the site.\n * @param {Function} $0.onError Function called when an error happens.\n * @param {Function} $0.onFileChange Function called each time a file or a temporary representation of the file is available.\n * @param {?Object} $0.wpAllowedMimeTypes List of allowed mime types and file extensions.\n */\nexport async function uploadMedia( {\n\tallowedTypes,\n\tadditionalData = {},\n\tfilesList,\n\tmaxUploadFileSize,\n\tonError = noop,\n\tonFileChange,\n\twpAllowedMimeTypes = null,\n} ) {\n\t// Cast filesList to array\n\tconst files = [ ...filesList ];\n\n\tconst filesSet = [];\n\tconst setAndUpdateFiles = ( idx, value ) => {\n\t\trevokeBlobURL( get( filesSet, [ idx, 'url' ] ) );\n\t\tfilesSet[ idx ] = value;\n\t\tonFileChange( compact( filesSet ) );\n\t};\n\n\t// Allowed type specified by consumer\n\tconst isAllowedType = ( fileType ) => {\n\t\tif ( ! allowedTypes ) {\n\t\t\treturn true;\n\t\t}\n\t\treturn some( allowedTypes, ( allowedType ) => {\n\t\t\t// If a complete mimetype is specified verify if it matches exactly the mime type of the file.\n\t\t\tif ( includes( allowedType, '/' ) ) {\n\t\t\t\treturn allowedType === fileType;\n\t\t\t}\n\t\t\t// Otherwise a general mime type is used and we should verify if the file mimetype starts with it.\n\t\t\treturn startsWith( fileType, `${ allowedType }/` );\n\t\t} );\n\t};\n\n\t// Allowed types for the current WP_User\n\tconst allowedMimeTypesForUser = getMimeTypesArray( wpAllowedMimeTypes );\n\tconst isAllowedMimeTypeForUser = ( fileType ) => {\n\t\treturn includes( allowedMimeTypesForUser, fileType );\n\t};\n\n\t// Build the error message including the filename\n\tconst triggerError = ( error ) => {\n\t\terror.message = [\n\t\t\t<strong key=\"filename\">{ error.file.name }</strong>,\n\t\t\t': ',\n\t\t\terror.message,\n\t\t];\n\n\t\tonError( error );\n\t};\n\n\tconst validFiles = [];\n\n\tfor ( const mediaFile of files ) {\n\t\t// Verify if user is allowed to upload this mime type.\n\t\t// Defer to the server when type not detected.\n\t\tif (\n\t\t\tallowedMimeTypesForUser &&\n\t\t\tmediaFile.type &&\n\t\t\t! isAllowedMimeTypeForUser( mediaFile.type )\n\t\t) {\n\t\t\ttriggerError( {\n\t\t\t\tcode: 'MIME_TYPE_NOT_ALLOWED_FOR_USER',\n\t\t\t\tmessage: __(\n\t\t\t\t\t'Sorry, this file type is not permitted for security reasons.'\n\t\t\t\t),\n\t\t\t\tfile: mediaFile,\n\t\t\t} );\n\t\t\tcontinue;\n\t\t}\n\n\t\t// Check if the block supports this mime type.\n\t\t// Defer to the server when type not detected.\n\t\tif ( mediaFile.type && ! isAllowedType( mediaFile.type ) ) {\n\t\t\ttriggerError( {\n\t\t\t\tcode: 'MIME_TYPE_NOT_SUPPORTED',\n\t\t\t\tmessage: __( 'Sorry, this file type is not supported here.' ),\n\t\t\t\tfile: mediaFile,\n\t\t\t} );\n\t\t\tcontinue;\n\t\t}\n\n\t\t// verify if file is greater than the maximum file upload size allowed for the site.\n\t\tif ( maxUploadFileSize && mediaFile.size > maxUploadFileSize ) {\n\t\t\ttriggerError( {\n\t\t\t\tcode: 'SIZE_ABOVE_LIMIT',\n\t\t\t\tmessage: __(\n\t\t\t\t\t'This file exceeds the maximum upload size for this site.'\n\t\t\t\t),\n\t\t\t\tfile: mediaFile,\n\t\t\t} );\n\t\t\tcontinue;\n\t\t}\n\n\t\t// Don't allow empty files to be uploaded.\n\t\tif ( mediaFile.size <= 0 ) {\n\t\t\ttriggerError( {\n\t\t\t\tcode: 'EMPTY_FILE',\n\t\t\t\tmessage: __( 'This file is empty.' ),\n\t\t\t\tfile: mediaFile,\n\t\t\t} );\n\t\t\tcontinue;\n\t\t}\n\n\t\tvalidFiles.push( mediaFile );\n\n\t\t// Set temporary URL to create placeholder media file, this is replaced\n\t\t// with final file from media gallery when upload is `done` below\n\t\tfilesSet.push( { url: createBlobURL( mediaFile ) } );\n\t\tonFileChange( filesSet );\n\t}\n\n\tfor ( let idx = 0; idx < validFiles.length; ++idx ) {\n\t\tconst mediaFile = validFiles[ idx ];\n\t\ttry {\n\t\t\tconst savedMedia = await createMediaFromFile(\n\t\t\t\tmediaFile,\n\t\t\t\tadditionalData\n\t\t\t);\n\t\t\tconst mediaObject = {\n\t\t\t\t...omit( savedMedia, [ 'alt_text', 'source_url' ] ),\n\t\t\t\talt: savedMedia.alt_text,\n\t\t\t\tcaption: get( savedMedia, [ 'caption', 'raw' ], '' ),\n\t\t\t\ttitle: savedMedia.title.raw,\n\t\t\t\turl: savedMedia.source_url,\n\t\t\t};\n\t\t\tsetAndUpdateFiles( idx, mediaObject );\n\t\t} catch ( error ) {\n\t\t\t// Reset to empty on failure.\n\t\t\tsetAndUpdateFiles( idx, null );\n\t\t\tlet message;\n\t\t\tif ( has( error, [ 'message' ] ) ) {\n\t\t\t\tmessage = get( error, [ 'message' ] );\n\t\t\t} else {\n\t\t\t\tmessage = sprintf(\n\t\t\t\t\t// translators: %s: file name\n\t\t\t\t\t__( 'Error while uploading file %s to the media library.' ),\n\t\t\t\t\tmediaFile.name\n\t\t\t\t);\n\t\t\t}\n\t\t\tonError( {\n\t\t\t\tcode: 'GENERAL',\n\t\t\t\tmessage,\n\t\t\t\tfile: mediaFile,\n\t\t\t} );\n\t\t}\n\t}\n}\n\n/**\n * @param {File} file Media File to Save.\n * @param {?Object} additionalData Additional data to include in the request.\n *\n * @return {Promise} Media Object Promise.\n */\nfunction createMediaFromFile( file, additionalData ) {\n\t// Create upload payload\n\tconst data = new window.FormData();\n\tdata.append( 'file', file, file.name || file.type.replace( '/', '.' ) );\n\tforEach( additionalData, ( value, key ) => data.append( key, value ) );\n\treturn apiFetch( {\n\t\tpath: '/wp/v2/media',\n\t\tbody: data,\n\t\tmethod: 'POST',\n\t} );\n}\n"]}
1
+ {"version":3,"sources":["@wordpress/media-utils/src/utils/upload-media.js"],"names":["compact","flatMap","forEach","get","has","includes","map","noop","omit","some","startsWith","apiFetch","createBlobURL","revokeBlobURL","__","sprintf","getMimeTypesArray","wpMimeTypesObject","mime","extensionsString","type","split","extensions","extension","uploadMedia","allowedTypes","additionalData","filesList","maxUploadFileSize","onError","onFileChange","wpAllowedMimeTypes","files","filesSet","setAndUpdateFiles","idx","value","isAllowedType","fileType","allowedType","allowedMimeTypesForUser","isAllowedMimeTypeForUser","triggerError","error","message","file","name","validFiles","mediaFile","code","size","push","url","length","savedMedia","createMediaFromFile","mediaObject","alt","alt_text","caption","title","raw","source_url","data","window","FormData","append","replace","key","path","body","method"],"mappings":";;AAAA;AACA;AACA;AACA,SACCA,OADD,EAECC,OAFD,EAGCC,OAHD,EAICC,GAJD,EAKCC,GALD,EAMCC,QAND,EAOCC,GAPD,EAQCC,IARD,EASCC,IATD,EAUCC,IAVD,EAWCC,UAXD,QAYO,QAZP;AAcA;AACA;AACA;;AACA,OAAOC,QAAP,MAAqB,sBAArB;AACA,SAASC,aAAT,EAAwBC,aAAxB,QAA6C,iBAA7C;AACA,SAASC,EAAT,EAAaC,OAAb,QAA4B,iBAA5B;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASC,iBAAT,CAA4BC,iBAA5B,EAAgD;AACtD,MAAK,CAAEA,iBAAP,EAA2B;AAC1B,WAAOA,iBAAP;AACA;;AACD,SAAOhB,OAAO,CAAEgB,iBAAF,EAAqB,CAAEC,IAAF,EAAQC,gBAAR,KAA8B;AAChE,UAAM,CAAEC,IAAF,IAAWF,IAAI,CAACG,KAAL,CAAY,GAAZ,CAAjB;AACA,UAAMC,UAAU,GAAGH,gBAAgB,CAACE,KAAjB,CAAwB,GAAxB,CAAnB;AACA,WAAO,CACNH,IADM,EAEN,GAAGZ,GAAG,CAAEgB,UAAF,EAAgBC,SAAF,IAAkB,GAAGH,IAAM,IAAIG,SAAW,EAAxD,CAFA,CAAP;AAIA,GAPa,CAAd;AAQA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,eAAeC,W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mC;AAClCC,QAAAA,OAAO,GAAGzC,GAAG,CAAEwC,KAAF,EAAS,CAAE,SAAF,CAAT,CAAb;AACA,OAFD,MAEO;AACNC,QAAAA,OAAO,GAAG7B,OAAO,EAChB;AACAD,QAAAA,EAAE,CAAE,qDAAF,CAFc,EAGhBkC,SAAS,CAACF,IAHM,CAAjB;AAKA;;AACDjB,MAAAA,OAAO,CAAE;AACRoB,QAAAA,IAAI,EAAE,SADE;AAERL,QAAAA,OAFQ;AAGRC,QAAAA,IAAI,EAAEG;AAHE,OAAF,CAAP;AAKA;AACD;AACD;AAED;AACA;AACA;AACA;AACA;AACA;;AACA,SAASO,mBAAT,CAA8BV,IAA9B,EAAoCnB,cAApC,EAAqD;AACpD;AACA,QAAMqC,IAAI,GAAG,IAAIC,MAAM,CAACC,QAAX,EAAb;AACAF,EAAAA,IAAI,CAACG,MAAL,CAAa,MAAb,EAAqBrB,IAArB,EAA2BA,IAAI,CAACC,IAAL,IAAaD,IAAI,CAACzB,IAAL,CAAU+C,OAAV,CAAmB,GAAnB,EAAwB,GAAxB,CAAxC;AACAjE,EAAAA,OAAO,CAAEwB,cAAF,EAAkB,CAAEU,KAAF,EAASgC,GAAT,KAAkBL,IAAI,CAACG,MAAL,CAAaE,GAAb,EAAkBhC,KAAlB,CAApC,CAAP;AACA,SAAOzB,QAAQ,CAAE;AAChB0D,IAAAA,IAAI,EAAE,cADU;AAEhBC,IAAAA,IAAI,EAAEP,IAFU;AAGhBQ,IAAAA,MAAM,EAAE;AAHQ,GAAF,CAAf;AAKA","sourcesContent":["/**\n * External dependencies\n */\nimport {\n\tcompact,\n\tflatMap,\n\tforEach,\n\tget,\n\thas,\n\tincludes,\n\tmap,\n\tnoop,\n\tomit,\n\tsome,\n\tstartsWith,\n} from 'lodash';\n\n/**\n * WordPress dependencies\n */\nimport apiFetch from '@wordpress/api-fetch';\nimport { createBlobURL, revokeBlobURL } from '@wordpress/blob';\nimport { __, sprintf } from '@wordpress/i18n';\n\n/**\n * Browsers may use unexpected mime types, and they differ from browser to browser.\n * This function computes a flexible array of mime types from the mime type structured provided by the server.\n * Converts { jpg|jpeg|jpe: \"image/jpeg\" } into [ \"image/jpeg\", \"image/jpg\", \"image/jpeg\", \"image/jpe\" ]\n * The computation of this array instead of directly using the object,\n * solves the problem in chrome where mp3 files have audio/mp3 as mime type instead of audio/mpeg.\n * https://bugs.chromium.org/p/chromium/issues/detail?id=227004\n *\n * @param {?Object} wpMimeTypesObject Mime type object received from the server.\n * Extensions are keys separated by '|' and values are mime types associated with an extension.\n *\n * @return {?Array} An array of mime types or the parameter passed if it was \"falsy\".\n */\nexport function getMimeTypesArray( wpMimeTypesObject ) {\n\tif ( ! wpMimeTypesObject ) {\n\t\treturn wpMimeTypesObject;\n\t}\n\treturn flatMap( wpMimeTypesObject, ( mime, extensionsString ) => {\n\t\tconst [ type ] = mime.split( '/' );\n\t\tconst extensions = extensionsString.split( '|' );\n\t\treturn [\n\t\t\tmime,\n\t\t\t...map( extensions, ( extension ) => `${ type }/${ extension }` ),\n\t\t];\n\t} );\n}\n\n/**\n *\tMedia Upload is used by audio, image, gallery, video, and file blocks to\n *\thandle uploading a media file when a file upload button is activated.\n *\n *\tTODO: future enhancement to add an upload indicator.\n *\n * @param {Object} $0 Parameters object passed to the function.\n * @param {?Array} $0.allowedTypes Array with the types of media that can be uploaded, if unset all types are allowed.\n * @param {?Object} $0.additionalData Additional data to include in the request.\n * @param {Array} $0.filesList List of files.\n * @param {?number} $0.maxUploadFileSize Maximum upload size in bytes allowed for the site.\n * @param {Function} $0.onError Function called when an error happens.\n * @param {Function} $0.onFileChange Function called each time a file or a temporary representation of the file is available.\n * @param {?Object} $0.wpAllowedMimeTypes List of allowed mime types and file extensions.\n */\nexport async function uploadMedia( {\n\tallowedTypes,\n\tadditionalData = {},\n\tfilesList,\n\tmaxUploadFileSize,\n\tonError = noop,\n\tonFileChange,\n\twpAllowedMimeTypes = null,\n} ) {\n\t// Cast filesList to array\n\tconst files = [ ...filesList ];\n\n\tconst filesSet = [];\n\tconst setAndUpdateFiles = ( idx, value ) => {\n\t\trevokeBlobURL( get( filesSet, [ idx, 'url' ] ) );\n\t\tfilesSet[ idx ] = value;\n\t\tonFileChange( compact( filesSet ) );\n\t};\n\n\t// Allowed type specified by consumer\n\tconst isAllowedType = ( fileType ) => {\n\t\tif ( ! allowedTypes ) {\n\t\t\treturn true;\n\t\t}\n\t\treturn some( allowedTypes, ( allowedType ) => {\n\t\t\t// If a complete mimetype is specified verify if it matches exactly the mime type of the file.\n\t\t\tif ( includes( allowedType, '/' ) ) {\n\t\t\t\treturn allowedType === fileType;\n\t\t\t}\n\t\t\t// Otherwise a general mime type is used and we should verify if the file mimetype starts with it.\n\t\t\treturn startsWith( fileType, `${ allowedType }/` );\n\t\t} );\n\t};\n\n\t// Allowed types for the current WP_User\n\tconst allowedMimeTypesForUser = getMimeTypesArray( wpAllowedMimeTypes );\n\tconst isAllowedMimeTypeForUser = ( fileType ) => {\n\t\treturn includes( allowedMimeTypesForUser, fileType );\n\t};\n\n\t// Build the error message including the filename\n\tconst triggerError = ( error ) => {\n\t\terror.message = [\n\t\t\t<strong key=\"filename\">{ error.file.name }</strong>,\n\t\t\t': ',\n\t\t\terror.message,\n\t\t];\n\n\t\tonError( error );\n\t};\n\n\tconst validFiles = [];\n\n\tfor ( const mediaFile of files ) {\n\t\t// Verify if user is allowed to upload this mime type.\n\t\t// Defer to the server when type not detected.\n\t\tif (\n\t\t\tallowedMimeTypesForUser &&\n\t\t\tmediaFile.type &&\n\t\t\t! isAllowedMimeTypeForUser( mediaFile.type )\n\t\t) {\n\t\t\ttriggerError( {\n\t\t\t\tcode: 'MIME_TYPE_NOT_ALLOWED_FOR_USER',\n\t\t\t\tmessage: __(\n\t\t\t\t\t'Sorry, you are not allowed to upload this file type.'\n\t\t\t\t),\n\t\t\t\tfile: mediaFile,\n\t\t\t} );\n\t\t\tcontinue;\n\t\t}\n\n\t\t// Check if the block supports this mime type.\n\t\t// Defer to the server when type not detected.\n\t\tif ( mediaFile.type && ! isAllowedType( mediaFile.type ) ) {\n\t\t\ttriggerError( {\n\t\t\t\tcode: 'MIME_TYPE_NOT_SUPPORTED',\n\t\t\t\tmessage: __( 'Sorry, this file type is not supported here.' ),\n\t\t\t\tfile: mediaFile,\n\t\t\t} );\n\t\t\tcontinue;\n\t\t}\n\n\t\t// verify if file is greater than the maximum file upload size allowed for the site.\n\t\tif ( maxUploadFileSize && mediaFile.size > maxUploadFileSize ) {\n\t\t\ttriggerError( {\n\t\t\t\tcode: 'SIZE_ABOVE_LIMIT',\n\t\t\t\tmessage: __(\n\t\t\t\t\t'This file exceeds the maximum upload size for this site.'\n\t\t\t\t),\n\t\t\t\tfile: mediaFile,\n\t\t\t} );\n\t\t\tcontinue;\n\t\t}\n\n\t\t// Don't allow empty files to be uploaded.\n\t\tif ( mediaFile.size <= 0 ) {\n\t\t\ttriggerError( {\n\t\t\t\tcode: 'EMPTY_FILE',\n\t\t\t\tmessage: __( 'This file is empty.' ),\n\t\t\t\tfile: mediaFile,\n\t\t\t} );\n\t\t\tcontinue;\n\t\t}\n\n\t\tvalidFiles.push( mediaFile );\n\n\t\t// Set temporary URL to create placeholder media file, this is replaced\n\t\t// with final file from media gallery when upload is `done` below\n\t\tfilesSet.push( { url: createBlobURL( mediaFile ) } );\n\t\tonFileChange( filesSet );\n\t}\n\n\tfor ( let idx = 0; idx < validFiles.length; ++idx ) {\n\t\tconst mediaFile = validFiles[ idx ];\n\t\ttry {\n\t\t\tconst savedMedia = await createMediaFromFile(\n\t\t\t\tmediaFile,\n\t\t\t\tadditionalData\n\t\t\t);\n\t\t\tconst mediaObject = {\n\t\t\t\t...omit( savedMedia, [ 'alt_text', 'source_url' ] ),\n\t\t\t\talt: savedMedia.alt_text,\n\t\t\t\tcaption: get( savedMedia, [ 'caption', 'raw' ], '' ),\n\t\t\t\ttitle: savedMedia.title.raw,\n\t\t\t\turl: savedMedia.source_url,\n\t\t\t};\n\t\t\tsetAndUpdateFiles( idx, mediaObject );\n\t\t} catch ( error ) {\n\t\t\t// Reset to empty on failure.\n\t\t\tsetAndUpdateFiles( idx, null );\n\t\t\tlet message;\n\t\t\tif ( has( error, [ 'message' ] ) ) {\n\t\t\t\tmessage = get( error, [ 'message' ] );\n\t\t\t} else {\n\t\t\t\tmessage = sprintf(\n\t\t\t\t\t// translators: %s: file name\n\t\t\t\t\t__( 'Error while uploading file %s to the media library.' ),\n\t\t\t\t\tmediaFile.name\n\t\t\t\t);\n\t\t\t}\n\t\t\tonError( {\n\t\t\t\tcode: 'GENERAL',\n\t\t\t\tmessage,\n\t\t\t\tfile: mediaFile,\n\t\t\t} );\n\t\t}\n\t}\n}\n\n/**\n * @param {File} file Media File to Save.\n * @param {?Object} additionalData Additional data to include in the request.\n *\n * @return {Promise} Media Object Promise.\n */\nfunction createMediaFromFile( file, additionalData ) {\n\t// Create upload payload\n\tconst data = new window.FormData();\n\tdata.append( 'file', file, file.name || file.type.replace( '/', '.' ) );\n\tforEach( additionalData, ( value, key ) => data.append( key, value ) );\n\treturn apiFetch( {\n\t\tpath: '/wp/v2/media',\n\t\tbody: data,\n\t\tmethod: 'POST',\n\t} );\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/media-utils",
3
- "version": "3.0.3",
3
+ "version": "3.0.4",
4
4
  "description": "WordPress Media Upload Utils.",
5
5
  "author": "The WordPress Contributors",
6
6
  "license": "GPL-2.0-or-later",
@@ -26,7 +26,7 @@
26
26
  "module": "build-module/index.js",
27
27
  "dependencies": {
28
28
  "@babel/runtime": "^7.13.10",
29
- "@wordpress/api-fetch": "^5.2.4",
29
+ "@wordpress/api-fetch": "^5.2.5",
30
30
  "@wordpress/blob": "^3.2.1",
31
31
  "@wordpress/element": "^4.0.3",
32
32
  "@wordpress/i18n": "^4.2.3",
@@ -35,5 +35,5 @@
35
35
  "publishConfig": {
36
36
  "access": "public"
37
37
  },
38
- "gitHead": "157f4ae53ab98e574af01f72213ae5a9613159ff"
38
+ "gitHead": "393c2b5533837fd637e998d23f0124c081a10df0"
39
39
  }
@@ -128,7 +128,7 @@ export async function uploadMedia( {
128
128
  triggerError( {
129
129
  code: 'MIME_TYPE_NOT_ALLOWED_FOR_USER',
130
130
  message: __(
131
- 'Sorry, this file type is not permitted for security reasons.'
131
+ 'Sorry, you are not allowed to upload this file type.'
132
132
  ),
133
133
  file: mediaFile,
134
134
  } );