@wordpress/media-utils 3.4.1 → 4.0.0
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.
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
## 4.0.0 (2022-05-18)
|
|
6
|
+
|
|
7
|
+
### Breaking Change
|
|
8
|
+
|
|
9
|
+
- The `onError` now always receives the `message` as a string ([#39448](https://github.com/WordPress/gutenberg/pull/39448)).
|
|
10
|
+
|
|
11
|
+
## 3.6.0 (2022-05-04)
|
|
12
|
+
|
|
13
|
+
## 3.5.0 (2022-04-21)
|
|
14
|
+
|
|
5
15
|
## 3.4.0 (2022-04-08)
|
|
6
16
|
|
|
7
17
|
## 3.3.0 (2022-03-23)
|
|
@@ -8,8 +8,6 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
8
8
|
exports.getMimeTypesArray = getMimeTypesArray;
|
|
9
9
|
exports.uploadMedia = uploadMedia;
|
|
10
10
|
|
|
11
|
-
var _element = require("@wordpress/element");
|
|
12
|
-
|
|
13
11
|
var _lodash = require("lodash");
|
|
14
12
|
|
|
15
13
|
var _apiFetch = _interopRequireDefault(require("@wordpress/api-fetch"));
|
|
@@ -109,14 +107,6 @@ async function uploadMedia(_ref) {
|
|
|
109
107
|
|
|
110
108
|
const isAllowedMimeTypeForUser = fileType => {
|
|
111
109
|
return (0, _lodash.includes)(allowedMimeTypesForUser, fileType);
|
|
112
|
-
}; // Build the error message including the filename.
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
const triggerError = error => {
|
|
116
|
-
error.message = [(0, _element.createElement)("strong", {
|
|
117
|
-
key: "filename"
|
|
118
|
-
}, error.file.name), ': ', error.message];
|
|
119
|
-
onError(error);
|
|
120
110
|
};
|
|
121
111
|
|
|
122
112
|
const validFiles = [];
|
|
@@ -125,9 +115,10 @@ async function uploadMedia(_ref) {
|
|
|
125
115
|
// Verify if user is allowed to upload this mime type.
|
|
126
116
|
// Defer to the server when type not detected.
|
|
127
117
|
if (allowedMimeTypesForUser && mediaFile.type && !isAllowedMimeTypeForUser(mediaFile.type)) {
|
|
128
|
-
|
|
118
|
+
onError({
|
|
129
119
|
code: 'MIME_TYPE_NOT_ALLOWED_FOR_USER',
|
|
130
|
-
message: (0, _i18n.
|
|
120
|
+
message: (0, _i18n.sprintf)( // translators: %s: file name.
|
|
121
|
+
(0, _i18n.__)('%s: Sorry, you are not allowed to upload this file type.'), mediaFile.name),
|
|
131
122
|
file: mediaFile
|
|
132
123
|
});
|
|
133
124
|
continue;
|
|
@@ -136,9 +127,10 @@ async function uploadMedia(_ref) {
|
|
|
136
127
|
|
|
137
128
|
|
|
138
129
|
if (mediaFile.type && !isAllowedType(mediaFile.type)) {
|
|
139
|
-
|
|
130
|
+
onError({
|
|
140
131
|
code: 'MIME_TYPE_NOT_SUPPORTED',
|
|
141
|
-
message: (0, _i18n.
|
|
132
|
+
message: (0, _i18n.sprintf)( // translators: %s: file name.
|
|
133
|
+
(0, _i18n.__)('%s: Sorry, this file type is not supported here.'), mediaFile.name),
|
|
142
134
|
file: mediaFile
|
|
143
135
|
});
|
|
144
136
|
continue;
|
|
@@ -146,9 +138,10 @@ async function uploadMedia(_ref) {
|
|
|
146
138
|
|
|
147
139
|
|
|
148
140
|
if (maxUploadFileSize && mediaFile.size > maxUploadFileSize) {
|
|
149
|
-
|
|
141
|
+
onError({
|
|
150
142
|
code: 'SIZE_ABOVE_LIMIT',
|
|
151
|
-
message: (0, _i18n.
|
|
143
|
+
message: (0, _i18n.sprintf)( // translators: %s: file name.
|
|
144
|
+
(0, _i18n.__)('%s: This file exceeds the maximum upload size for this site.'), mediaFile.name),
|
|
152
145
|
file: mediaFile
|
|
153
146
|
});
|
|
154
147
|
continue;
|
|
@@ -156,9 +149,10 @@ async function uploadMedia(_ref) {
|
|
|
156
149
|
|
|
157
150
|
|
|
158
151
|
if (mediaFile.size <= 0) {
|
|
159
|
-
|
|
152
|
+
onError({
|
|
160
153
|
code: 'EMPTY_FILE',
|
|
161
|
-
message: (0, _i18n.
|
|
154
|
+
message: (0, _i18n.sprintf)( // translators: %s: file name.
|
|
155
|
+
(0, _i18n.__)('%s: This file is empty.'), mediaFile.name),
|
|
162
156
|
file: mediaFile
|
|
163
157
|
});
|
|
164
158
|
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;AACA,WAAO,CACNH,IADM,EAEN,GAAG,iBAAKI,UAAL,EAAmBC,SAAF,IAAkB,GAAGH,IAAM,IAAIG,SAAW,EAA3D,CAFG,CAAP;AAIA,GAPM,CAAP;AAQA;AA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D,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, 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"]}
|
|
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","validFiles","mediaFile","code","message","name","file","size","push","url","length","savedMedia","createMediaFromFile","mediaObject","alt","alt_text","caption","title","raw","source_url","error","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,EAA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\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\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\tonError( {\n\t\t\t\tcode: 'MIME_TYPE_NOT_ALLOWED_FOR_USER',\n\t\t\t\tmessage: sprintf(\n\t\t\t\t\t// translators: %s: file name.\n\t\t\t\t\t__(\n\t\t\t\t\t\t'%s: Sorry, you are not allowed to upload this file type.'\n\t\t\t\t\t),\n\t\t\t\t\tmediaFile.name\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\tonError( {\n\t\t\t\tcode: 'MIME_TYPE_NOT_SUPPORTED',\n\t\t\t\tmessage: sprintf(\n\t\t\t\t\t// translators: %s: file name.\n\t\t\t\t\t__( '%s: Sorry, this file type is not supported here.' ),\n\t\t\t\t\tmediaFile.name\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// 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\tonError( {\n\t\t\t\tcode: 'SIZE_ABOVE_LIMIT',\n\t\t\t\tmessage: sprintf(\n\t\t\t\t\t// translators: %s: file name.\n\t\t\t\t\t__(\n\t\t\t\t\t\t'%s: This file exceeds the maximum upload size for this site.'\n\t\t\t\t\t),\n\t\t\t\t\tmediaFile.name\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\tonError( {\n\t\t\t\tcode: 'EMPTY_FILE',\n\t\t\t\tmessage: sprintf(\n\t\t\t\t\t// translators: %s: file name.\n\t\t\t\t\t__( '%s: This file is empty.' ),\n\t\t\t\t\tmediaFile.name\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\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,5 +1,3 @@
|
|
|
1
|
-
import { createElement } from "@wordpress/element";
|
|
2
|
-
|
|
3
1
|
/**
|
|
4
2
|
* External dependencies
|
|
5
3
|
*/
|
|
@@ -94,14 +92,6 @@ export async function uploadMedia(_ref) {
|
|
|
94
92
|
|
|
95
93
|
const isAllowedMimeTypeForUser = fileType => {
|
|
96
94
|
return includes(allowedMimeTypesForUser, fileType);
|
|
97
|
-
}; // Build the error message including the filename.
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
const triggerError = error => {
|
|
101
|
-
error.message = [createElement("strong", {
|
|
102
|
-
key: "filename"
|
|
103
|
-
}, error.file.name), ': ', error.message];
|
|
104
|
-
onError(error);
|
|
105
95
|
};
|
|
106
96
|
|
|
107
97
|
const validFiles = [];
|
|
@@ -110,9 +100,10 @@ export async function uploadMedia(_ref) {
|
|
|
110
100
|
// Verify if user is allowed to upload this mime type.
|
|
111
101
|
// Defer to the server when type not detected.
|
|
112
102
|
if (allowedMimeTypesForUser && mediaFile.type && !isAllowedMimeTypeForUser(mediaFile.type)) {
|
|
113
|
-
|
|
103
|
+
onError({
|
|
114
104
|
code: 'MIME_TYPE_NOT_ALLOWED_FOR_USER',
|
|
115
|
-
message:
|
|
105
|
+
message: sprintf( // translators: %s: file name.
|
|
106
|
+
__('%s: Sorry, you are not allowed to upload this file type.'), mediaFile.name),
|
|
116
107
|
file: mediaFile
|
|
117
108
|
});
|
|
118
109
|
continue;
|
|
@@ -121,9 +112,10 @@ export async function uploadMedia(_ref) {
|
|
|
121
112
|
|
|
122
113
|
|
|
123
114
|
if (mediaFile.type && !isAllowedType(mediaFile.type)) {
|
|
124
|
-
|
|
115
|
+
onError({
|
|
125
116
|
code: 'MIME_TYPE_NOT_SUPPORTED',
|
|
126
|
-
message:
|
|
117
|
+
message: sprintf( // translators: %s: file name.
|
|
118
|
+
__('%s: Sorry, this file type is not supported here.'), mediaFile.name),
|
|
127
119
|
file: mediaFile
|
|
128
120
|
});
|
|
129
121
|
continue;
|
|
@@ -131,9 +123,10 @@ export async function uploadMedia(_ref) {
|
|
|
131
123
|
|
|
132
124
|
|
|
133
125
|
if (maxUploadFileSize && mediaFile.size > maxUploadFileSize) {
|
|
134
|
-
|
|
126
|
+
onError({
|
|
135
127
|
code: 'SIZE_ABOVE_LIMIT',
|
|
136
|
-
message:
|
|
128
|
+
message: sprintf( // translators: %s: file name.
|
|
129
|
+
__('%s: This file exceeds the maximum upload size for this site.'), mediaFile.name),
|
|
137
130
|
file: mediaFile
|
|
138
131
|
});
|
|
139
132
|
continue;
|
|
@@ -141,9 +134,10 @@ export async function uploadMedia(_ref) {
|
|
|
141
134
|
|
|
142
135
|
|
|
143
136
|
if (mediaFile.size <= 0) {
|
|
144
|
-
|
|
137
|
+
onError({
|
|
145
138
|
code: 'EMPTY_FILE',
|
|
146
|
-
message:
|
|
139
|
+
message: sprintf( // translators: %s: file name.
|
|
140
|
+
__('%s: This file is empty.'), mediaFile.name),
|
|
147
141
|
file: mediaFile
|
|
148
142
|
});
|
|
149
143
|
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,aA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F,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"]}
|
|
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","validFiles","mediaFile","code","message","name","file","size","push","url","length","savedMedia","createMediaFromFile","mediaObject","alt","alt_text","caption","title","raw","source_url","error","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,KA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,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\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\tonError( {\n\t\t\t\tcode: 'MIME_TYPE_NOT_ALLOWED_FOR_USER',\n\t\t\t\tmessage: sprintf(\n\t\t\t\t\t// translators: %s: file name.\n\t\t\t\t\t__(\n\t\t\t\t\t\t'%s: Sorry, you are not allowed to upload this file type.'\n\t\t\t\t\t),\n\t\t\t\t\tmediaFile.name\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\tonError( {\n\t\t\t\tcode: 'MIME_TYPE_NOT_SUPPORTED',\n\t\t\t\tmessage: sprintf(\n\t\t\t\t\t// translators: %s: file name.\n\t\t\t\t\t__( '%s: Sorry, this file type is not supported here.' ),\n\t\t\t\t\tmediaFile.name\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// 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\tonError( {\n\t\t\t\tcode: 'SIZE_ABOVE_LIMIT',\n\t\t\t\tmessage: sprintf(\n\t\t\t\t\t// translators: %s: file name.\n\t\t\t\t\t__(\n\t\t\t\t\t\t'%s: This file exceeds the maximum upload size for this site.'\n\t\t\t\t\t),\n\t\t\t\t\tmediaFile.name\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\tonError( {\n\t\t\t\tcode: 'EMPTY_FILE',\n\t\t\t\tmessage: sprintf(\n\t\t\t\t\t// translators: %s: file name.\n\t\t\t\t\t__( '%s: This file is empty.' ),\n\t\t\t\t\tmediaFile.name\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\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
|
+
"version": "4.0.0",
|
|
4
4
|
"description": "WordPress Media Upload Utils.",
|
|
5
5
|
"author": "The WordPress Contributors",
|
|
6
6
|
"license": "GPL-2.0-or-later",
|
|
@@ -26,14 +26,14 @@
|
|
|
26
26
|
"module": "build-module/index.js",
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"@babel/runtime": "^7.16.0",
|
|
29
|
-
"@wordpress/api-fetch": "^6.
|
|
30
|
-
"@wordpress/blob": "^3.
|
|
31
|
-
"@wordpress/element": "^4.
|
|
32
|
-
"@wordpress/i18n": "^4.
|
|
29
|
+
"@wordpress/api-fetch": "^6.6.0",
|
|
30
|
+
"@wordpress/blob": "^3.9.0",
|
|
31
|
+
"@wordpress/element": "^4.7.0",
|
|
32
|
+
"@wordpress/i18n": "^4.9.0",
|
|
33
33
|
"lodash": "^4.17.21"
|
|
34
34
|
},
|
|
35
35
|
"publishConfig": {
|
|
36
36
|
"access": "public"
|
|
37
37
|
},
|
|
38
|
-
"gitHead": "
|
|
38
|
+
"gitHead": "198fa129cf1af8dc615918987ea6795cd40ab7df"
|
|
39
39
|
}
|
|
@@ -104,17 +104,6 @@ export async function uploadMedia( {
|
|
|
104
104
|
return includes( allowedMimeTypesForUser, fileType );
|
|
105
105
|
};
|
|
106
106
|
|
|
107
|
-
// Build the error message including the filename.
|
|
108
|
-
const triggerError = ( error ) => {
|
|
109
|
-
error.message = [
|
|
110
|
-
<strong key="filename">{ error.file.name }</strong>,
|
|
111
|
-
': ',
|
|
112
|
-
error.message,
|
|
113
|
-
];
|
|
114
|
-
|
|
115
|
-
onError( error );
|
|
116
|
-
};
|
|
117
|
-
|
|
118
107
|
const validFiles = [];
|
|
119
108
|
|
|
120
109
|
for ( const mediaFile of files ) {
|
|
@@ -125,10 +114,14 @@ export async function uploadMedia( {
|
|
|
125
114
|
mediaFile.type &&
|
|
126
115
|
! isAllowedMimeTypeForUser( mediaFile.type )
|
|
127
116
|
) {
|
|
128
|
-
|
|
117
|
+
onError( {
|
|
129
118
|
code: 'MIME_TYPE_NOT_ALLOWED_FOR_USER',
|
|
130
|
-
message:
|
|
131
|
-
|
|
119
|
+
message: sprintf(
|
|
120
|
+
// translators: %s: file name.
|
|
121
|
+
__(
|
|
122
|
+
'%s: Sorry, you are not allowed to upload this file type.'
|
|
123
|
+
),
|
|
124
|
+
mediaFile.name
|
|
132
125
|
),
|
|
133
126
|
file: mediaFile,
|
|
134
127
|
} );
|
|
@@ -138,9 +131,13 @@ export async function uploadMedia( {
|
|
|
138
131
|
// Check if the block supports this mime type.
|
|
139
132
|
// Defer to the server when type not detected.
|
|
140
133
|
if ( mediaFile.type && ! isAllowedType( mediaFile.type ) ) {
|
|
141
|
-
|
|
134
|
+
onError( {
|
|
142
135
|
code: 'MIME_TYPE_NOT_SUPPORTED',
|
|
143
|
-
message:
|
|
136
|
+
message: sprintf(
|
|
137
|
+
// translators: %s: file name.
|
|
138
|
+
__( '%s: Sorry, this file type is not supported here.' ),
|
|
139
|
+
mediaFile.name
|
|
140
|
+
),
|
|
144
141
|
file: mediaFile,
|
|
145
142
|
} );
|
|
146
143
|
continue;
|
|
@@ -148,10 +145,14 @@ export async function uploadMedia( {
|
|
|
148
145
|
|
|
149
146
|
// Verify if file is greater than the maximum file upload size allowed for the site.
|
|
150
147
|
if ( maxUploadFileSize && mediaFile.size > maxUploadFileSize ) {
|
|
151
|
-
|
|
148
|
+
onError( {
|
|
152
149
|
code: 'SIZE_ABOVE_LIMIT',
|
|
153
|
-
message:
|
|
154
|
-
|
|
150
|
+
message: sprintf(
|
|
151
|
+
// translators: %s: file name.
|
|
152
|
+
__(
|
|
153
|
+
'%s: This file exceeds the maximum upload size for this site.'
|
|
154
|
+
),
|
|
155
|
+
mediaFile.name
|
|
155
156
|
),
|
|
156
157
|
file: mediaFile,
|
|
157
158
|
} );
|
|
@@ -160,9 +161,13 @@ export async function uploadMedia( {
|
|
|
160
161
|
|
|
161
162
|
// Don't allow empty files to be uploaded.
|
|
162
163
|
if ( mediaFile.size <= 0 ) {
|
|
163
|
-
|
|
164
|
+
onError( {
|
|
164
165
|
code: 'EMPTY_FILE',
|
|
165
|
-
message:
|
|
166
|
+
message: sprintf(
|
|
167
|
+
// translators: %s: file name.
|
|
168
|
+
__( '%s: This file is empty.' ),
|
|
169
|
+
mediaFile.name
|
|
170
|
+
),
|
|
166
171
|
file: mediaFile,
|
|
167
172
|
} );
|
|
168
173
|
continue;
|