@wordpress/media-utils 3.6.0 → 4.2.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 +10 -0
- package/build/utils/upload-media.js +16 -20
- package/build/utils/upload-media.js.map +1 -1
- package/build-module/utils/upload-media.js +16 -19
- package/build-module/utils/upload-media.js.map +1 -1
- package/package.json +6 -6
- package/src/utils/test/upload-media.test.js +3 -3
- package/src/utils/upload-media.js +28 -22
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
## 4.2.0 (2022-06-15)
|
|
6
|
+
|
|
7
|
+
## 4.1.0 (2022-06-01)
|
|
8
|
+
|
|
9
|
+
## 4.0.0 (2022-05-18)
|
|
10
|
+
|
|
11
|
+
### Breaking Change
|
|
12
|
+
|
|
13
|
+
- The `onError` now always receives the `message` as a string ([#39448](https://github.com/WordPress/gutenberg/pull/39448)).
|
|
14
|
+
|
|
5
15
|
## 3.6.0 (2022-05-04)
|
|
6
16
|
|
|
7
17
|
## 3.5.0 (2022-04-21)
|
|
@@ -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"));
|
|
@@ -25,7 +23,7 @@ var _i18n = require("@wordpress/i18n");
|
|
|
25
23
|
/**
|
|
26
24
|
* WordPress dependencies
|
|
27
25
|
*/
|
|
28
|
-
|
|
26
|
+
const noop = () => {};
|
|
29
27
|
/**
|
|
30
28
|
* Browsers may use unexpected mime types, and they differ from browser to browser.
|
|
31
29
|
* This function computes a flexible array of mime types from the mime type structured provided by the server.
|
|
@@ -39,6 +37,8 @@ var _i18n = require("@wordpress/i18n");
|
|
|
39
37
|
*
|
|
40
38
|
* @return {?Array} An array of mime types or the parameter passed if it was "falsy".
|
|
41
39
|
*/
|
|
40
|
+
|
|
41
|
+
|
|
42
42
|
function getMimeTypesArray(wpMimeTypesObject) {
|
|
43
43
|
if (!wpMimeTypesObject) {
|
|
44
44
|
return wpMimeTypesObject;
|
|
@@ -73,7 +73,7 @@ async function uploadMedia(_ref) {
|
|
|
73
73
|
additionalData = {},
|
|
74
74
|
filesList,
|
|
75
75
|
maxUploadFileSize,
|
|
76
|
-
onError =
|
|
76
|
+
onError = noop,
|
|
77
77
|
onFileChange,
|
|
78
78
|
wpAllowedMimeTypes = null
|
|
79
79
|
} = _ref;
|
|
@@ -109,14 +109,6 @@ async function uploadMedia(_ref) {
|
|
|
109
109
|
|
|
110
110
|
const isAllowedMimeTypeForUser = fileType => {
|
|
111
111
|
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
112
|
};
|
|
121
113
|
|
|
122
114
|
const validFiles = [];
|
|
@@ -125,9 +117,10 @@ async function uploadMedia(_ref) {
|
|
|
125
117
|
// Verify if user is allowed to upload this mime type.
|
|
126
118
|
// Defer to the server when type not detected.
|
|
127
119
|
if (allowedMimeTypesForUser && mediaFile.type && !isAllowedMimeTypeForUser(mediaFile.type)) {
|
|
128
|
-
|
|
120
|
+
onError({
|
|
129
121
|
code: 'MIME_TYPE_NOT_ALLOWED_FOR_USER',
|
|
130
|
-
message: (0, _i18n.
|
|
122
|
+
message: (0, _i18n.sprintf)( // translators: %s: file name.
|
|
123
|
+
(0, _i18n.__)('%s: Sorry, you are not allowed to upload this file type.'), mediaFile.name),
|
|
131
124
|
file: mediaFile
|
|
132
125
|
});
|
|
133
126
|
continue;
|
|
@@ -136,9 +129,10 @@ async function uploadMedia(_ref) {
|
|
|
136
129
|
|
|
137
130
|
|
|
138
131
|
if (mediaFile.type && !isAllowedType(mediaFile.type)) {
|
|
139
|
-
|
|
132
|
+
onError({
|
|
140
133
|
code: 'MIME_TYPE_NOT_SUPPORTED',
|
|
141
|
-
message: (0, _i18n.
|
|
134
|
+
message: (0, _i18n.sprintf)( // translators: %s: file name.
|
|
135
|
+
(0, _i18n.__)('%s: Sorry, this file type is not supported here.'), mediaFile.name),
|
|
142
136
|
file: mediaFile
|
|
143
137
|
});
|
|
144
138
|
continue;
|
|
@@ -146,9 +140,10 @@ async function uploadMedia(_ref) {
|
|
|
146
140
|
|
|
147
141
|
|
|
148
142
|
if (maxUploadFileSize && mediaFile.size > maxUploadFileSize) {
|
|
149
|
-
|
|
143
|
+
onError({
|
|
150
144
|
code: 'SIZE_ABOVE_LIMIT',
|
|
151
|
-
message: (0, _i18n.
|
|
145
|
+
message: (0, _i18n.sprintf)( // translators: %s: file name.
|
|
146
|
+
(0, _i18n.__)('%s: This file exceeds the maximum upload size for this site.'), mediaFile.name),
|
|
152
147
|
file: mediaFile
|
|
153
148
|
});
|
|
154
149
|
continue;
|
|
@@ -156,9 +151,10 @@ async function uploadMedia(_ref) {
|
|
|
156
151
|
|
|
157
152
|
|
|
158
153
|
if (mediaFile.size <= 0) {
|
|
159
|
-
|
|
154
|
+
onError({
|
|
160
155
|
code: 'EMPTY_FILE',
|
|
161
|
-
message: (0, _i18n.
|
|
156
|
+
message: (0, _i18n.sprintf)( // translators: %s: file name.
|
|
157
|
+
(0, _i18n.__)('%s: This file is empty.'), mediaFile.name),
|
|
162
158
|
file: mediaFile
|
|
163
159
|
});
|
|
164
160
|
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;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,eAAeC,WAAf,OAQH;AAAA,MAR+B;AAClCC,IAAAA,YADkC;AAElCC,IAAAA,cAAc,GAAG,EAFiB;AAGlCC,IAAAA,SAHkC;AAIlCC,IAAAA,iBAJkC;AAKlCC,IAAAA,OAAO,GAAGC,YALwB;AAMlCC,IAAAA,YANkC;AAOlCC,IAAAA,kBAAkB,GAAG;AAPa,GAQ/B;AACH;AACA,Q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;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":["noop","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":";;;;;;;;;;AAGA;;AAgBA;;AACA;;AACA;;AArBA;AACA;AACA;;AAcA;AACA;AACA;AAKA,MAAMA,IAAI,GAAG,MAAM,CAAE,CAArB;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,SAASC,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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E,SAAF,CAAZ,CAAV;AACA,OAFD,MAEO;AACNhB,QAAAA,OAAO,GAAG,oBACT;AACA,sBAAI,qDAAJ,CAFS,EAGTF,SAAS,CAACG,IAHD,CAAV;AAKA;;AACDjB,MAAAA,OAAO,CAAE;AACRe,QAAAA,IAAI,EAAE,SADE;AAERC,QAAAA,OAFQ;AAGRE,QAAAA,IAAI,EAAEJ;AAHE,OAAF,CAAP;AAKA;AACD;AACD;AAED;AACA;AACA;AACA;AACA;AACA;;;AACA,SAASU,mBAAT,CAA8BN,IAA9B,EAAoCrB,cAApC,EAAqD;AACpD;AACA,QAAMoC,IAAI,GAAG,IAAIC,MAAM,CAACC,QAAX,EAAb;AACAF,EAAAA,IAAI,CAACG,MAAL,CAAa,MAAb,EAAqBlB,IAArB,EAA2BA,IAAI,CAACD,IAAL,IAAaC,IAAI,CAAC3B,IAAL,CAAU8C,OAAV,CAAmB,GAAnB,EAAwB,GAAxB,CAAxC;AACA,uBAASxC,cAAT,EAAyB,CAAEU,KAAF,EAAS+B,GAAT,KAAkBL,IAAI,CAACG,MAAL,CAAaE,GAAb,EAAkB/B,KAAlB,CAA3C;AACA,SAAO,uBAAU;AAChBgC,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\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\nconst noop = () => {};\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,9 +1,7 @@
|
|
|
1
|
-
import { createElement } from "@wordpress/element";
|
|
2
|
-
|
|
3
1
|
/**
|
|
4
2
|
* External dependencies
|
|
5
3
|
*/
|
|
6
|
-
import { compact, flatMap, forEach, get, has, includes, map,
|
|
4
|
+
import { compact, flatMap, forEach, get, has, includes, map, omit, some, startsWith } from 'lodash';
|
|
7
5
|
/**
|
|
8
6
|
* WordPress dependencies
|
|
9
7
|
*/
|
|
@@ -11,6 +9,8 @@ import { compact, flatMap, forEach, get, has, includes, map, noop, omit, some, s
|
|
|
11
9
|
import apiFetch from '@wordpress/api-fetch';
|
|
12
10
|
import { createBlobURL, revokeBlobURL } from '@wordpress/blob';
|
|
13
11
|
import { __, sprintf } from '@wordpress/i18n';
|
|
12
|
+
|
|
13
|
+
const noop = () => {};
|
|
14
14
|
/**
|
|
15
15
|
* Browsers may use unexpected mime types, and they differ from browser to browser.
|
|
16
16
|
* This function computes a flexible array of mime types from the mime type structured provided by the server.
|
|
@@ -25,6 +25,7 @@ import { __, sprintf } from '@wordpress/i18n';
|
|
|
25
25
|
* @return {?Array} An array of mime types or the parameter passed if it was "falsy".
|
|
26
26
|
*/
|
|
27
27
|
|
|
28
|
+
|
|
28
29
|
export function getMimeTypesArray(wpMimeTypesObject) {
|
|
29
30
|
if (!wpMimeTypesObject) {
|
|
30
31
|
return wpMimeTypesObject;
|
|
@@ -94,14 +95,6 @@ export async function uploadMedia(_ref) {
|
|
|
94
95
|
|
|
95
96
|
const isAllowedMimeTypeForUser = fileType => {
|
|
96
97
|
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
98
|
};
|
|
106
99
|
|
|
107
100
|
const validFiles = [];
|
|
@@ -110,9 +103,10 @@ export async function uploadMedia(_ref) {
|
|
|
110
103
|
// Verify if user is allowed to upload this mime type.
|
|
111
104
|
// Defer to the server when type not detected.
|
|
112
105
|
if (allowedMimeTypesForUser && mediaFile.type && !isAllowedMimeTypeForUser(mediaFile.type)) {
|
|
113
|
-
|
|
106
|
+
onError({
|
|
114
107
|
code: 'MIME_TYPE_NOT_ALLOWED_FOR_USER',
|
|
115
|
-
message:
|
|
108
|
+
message: sprintf( // translators: %s: file name.
|
|
109
|
+
__('%s: Sorry, you are not allowed to upload this file type.'), mediaFile.name),
|
|
116
110
|
file: mediaFile
|
|
117
111
|
});
|
|
118
112
|
continue;
|
|
@@ -121,9 +115,10 @@ export async function uploadMedia(_ref) {
|
|
|
121
115
|
|
|
122
116
|
|
|
123
117
|
if (mediaFile.type && !isAllowedType(mediaFile.type)) {
|
|
124
|
-
|
|
118
|
+
onError({
|
|
125
119
|
code: 'MIME_TYPE_NOT_SUPPORTED',
|
|
126
|
-
message:
|
|
120
|
+
message: sprintf( // translators: %s: file name.
|
|
121
|
+
__('%s: Sorry, this file type is not supported here.'), mediaFile.name),
|
|
127
122
|
file: mediaFile
|
|
128
123
|
});
|
|
129
124
|
continue;
|
|
@@ -131,9 +126,10 @@ export async function uploadMedia(_ref) {
|
|
|
131
126
|
|
|
132
127
|
|
|
133
128
|
if (maxUploadFileSize && mediaFile.size > maxUploadFileSize) {
|
|
134
|
-
|
|
129
|
+
onError({
|
|
135
130
|
code: 'SIZE_ABOVE_LIMIT',
|
|
136
|
-
message:
|
|
131
|
+
message: sprintf( // translators: %s: file name.
|
|
132
|
+
__('%s: This file exceeds the maximum upload size for this site.'), mediaFile.name),
|
|
137
133
|
file: mediaFile
|
|
138
134
|
});
|
|
139
135
|
continue;
|
|
@@ -141,9 +137,10 @@ export async function uploadMedia(_ref) {
|
|
|
141
137
|
|
|
142
138
|
|
|
143
139
|
if (mediaFile.size <= 0) {
|
|
144
|
-
|
|
140
|
+
onError({
|
|
145
141
|
code: 'EMPTY_FILE',
|
|
146
|
-
message:
|
|
142
|
+
message: sprintf( // translators: %s: file name.
|
|
143
|
+
__('%s: This file is empty.'), mediaFile.name),
|
|
147
144
|
file: mediaFile
|
|
148
145
|
});
|
|
149
146
|
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,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,WAAf,OAQH;AAAA,MAR+B;AAClCC,IAAAA,YADkC;AAElCC,IAAAA,cAAc,GAAG,E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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"]}
|
|
1
|
+
{"version":3,"sources":["@wordpress/media-utils/src/utils/upload-media.js"],"names":["compact","flatMap","forEach","get","has","includes","map","omit","some","startsWith","apiFetch","createBlobURL","revokeBlobURL","__","sprintf","noop","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,EAQ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AApC,EAAqD;AACpD;AACA,QAAMoC,IAAI,GAAG,IAAIC,MAAM,CAACC,QAAX,EAAb;AACAF,EAAAA,IAAI,CAACG,MAAL,CAAa,MAAb,EAAqBlB,IAArB,EAA2BA,IAAI,CAACD,IAAL,IAAaC,IAAI,CAAC3B,IAAL,CAAU8C,OAAV,CAAmB,GAAnB,EAAwB,GAAxB,CAAxC;AACAhE,EAAAA,OAAO,CAAEwB,cAAF,EAAkB,CAAEU,KAAF,EAAS+B,GAAT,KAAkBL,IAAI,CAACG,MAAL,CAAaE,GAAb,EAAkB/B,KAAlB,CAApC,CAAP;AACA,SAAO1B,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\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\nconst noop = () => {};\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.2.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.8.0",
|
|
30
|
+
"@wordpress/blob": "^3.11.0",
|
|
31
|
+
"@wordpress/element": "^4.9.0",
|
|
32
|
+
"@wordpress/i18n": "^4.11.0",
|
|
33
33
|
"lodash": "^4.17.21"
|
|
34
34
|
},
|
|
35
35
|
"publishConfig": {
|
|
36
36
|
"access": "public"
|
|
37
37
|
},
|
|
38
|
-
"gitHead": "
|
|
38
|
+
"gitHead": "48d5f37dfb52d2e77c8eeb662f9874cf141b8c6b"
|
|
39
39
|
}
|
|
@@ -209,9 +209,9 @@ describe( 'getMimeTypesArray', () => {
|
|
|
209
209
|
} );
|
|
210
210
|
|
|
211
211
|
it( 'should return the mime type passed and an additional mime type per extension supported', () => {
|
|
212
|
-
expect(
|
|
213
|
-
|
|
214
|
-
)
|
|
212
|
+
expect( getMimeTypesArray( { 'jpg|jpeg|jpe': 'image/jpeg' } ) ).toEqual(
|
|
213
|
+
[ 'image/jpeg', 'image/jpg', 'image/jpeg', 'image/jpe' ]
|
|
214
|
+
);
|
|
215
215
|
} );
|
|
216
216
|
|
|
217
217
|
it( 'should handle multiple mime types', () => {
|
|
@@ -9,7 +9,6 @@ import {
|
|
|
9
9
|
has,
|
|
10
10
|
includes,
|
|
11
11
|
map,
|
|
12
|
-
noop,
|
|
13
12
|
omit,
|
|
14
13
|
some,
|
|
15
14
|
startsWith,
|
|
@@ -22,6 +21,8 @@ import apiFetch from '@wordpress/api-fetch';
|
|
|
22
21
|
import { createBlobURL, revokeBlobURL } from '@wordpress/blob';
|
|
23
22
|
import { __, sprintf } from '@wordpress/i18n';
|
|
24
23
|
|
|
24
|
+
const noop = () => {};
|
|
25
|
+
|
|
25
26
|
/**
|
|
26
27
|
* Browsers may use unexpected mime types, and they differ from browser to browser.
|
|
27
28
|
* This function computes a flexible array of mime types from the mime type structured provided by the server.
|
|
@@ -104,17 +105,6 @@ export async function uploadMedia( {
|
|
|
104
105
|
return includes( allowedMimeTypesForUser, fileType );
|
|
105
106
|
};
|
|
106
107
|
|
|
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
108
|
const validFiles = [];
|
|
119
109
|
|
|
120
110
|
for ( const mediaFile of files ) {
|
|
@@ -125,10 +115,14 @@ export async function uploadMedia( {
|
|
|
125
115
|
mediaFile.type &&
|
|
126
116
|
! isAllowedMimeTypeForUser( mediaFile.type )
|
|
127
117
|
) {
|
|
128
|
-
|
|
118
|
+
onError( {
|
|
129
119
|
code: 'MIME_TYPE_NOT_ALLOWED_FOR_USER',
|
|
130
|
-
message:
|
|
131
|
-
|
|
120
|
+
message: sprintf(
|
|
121
|
+
// translators: %s: file name.
|
|
122
|
+
__(
|
|
123
|
+
'%s: Sorry, you are not allowed to upload this file type.'
|
|
124
|
+
),
|
|
125
|
+
mediaFile.name
|
|
132
126
|
),
|
|
133
127
|
file: mediaFile,
|
|
134
128
|
} );
|
|
@@ -138,9 +132,13 @@ export async function uploadMedia( {
|
|
|
138
132
|
// Check if the block supports this mime type.
|
|
139
133
|
// Defer to the server when type not detected.
|
|
140
134
|
if ( mediaFile.type && ! isAllowedType( mediaFile.type ) ) {
|
|
141
|
-
|
|
135
|
+
onError( {
|
|
142
136
|
code: 'MIME_TYPE_NOT_SUPPORTED',
|
|
143
|
-
message:
|
|
137
|
+
message: sprintf(
|
|
138
|
+
// translators: %s: file name.
|
|
139
|
+
__( '%s: Sorry, this file type is not supported here.' ),
|
|
140
|
+
mediaFile.name
|
|
141
|
+
),
|
|
144
142
|
file: mediaFile,
|
|
145
143
|
} );
|
|
146
144
|
continue;
|
|
@@ -148,10 +146,14 @@ export async function uploadMedia( {
|
|
|
148
146
|
|
|
149
147
|
// Verify if file is greater than the maximum file upload size allowed for the site.
|
|
150
148
|
if ( maxUploadFileSize && mediaFile.size > maxUploadFileSize ) {
|
|
151
|
-
|
|
149
|
+
onError( {
|
|
152
150
|
code: 'SIZE_ABOVE_LIMIT',
|
|
153
|
-
message:
|
|
154
|
-
|
|
151
|
+
message: sprintf(
|
|
152
|
+
// translators: %s: file name.
|
|
153
|
+
__(
|
|
154
|
+
'%s: This file exceeds the maximum upload size for this site.'
|
|
155
|
+
),
|
|
156
|
+
mediaFile.name
|
|
155
157
|
),
|
|
156
158
|
file: mediaFile,
|
|
157
159
|
} );
|
|
@@ -160,9 +162,13 @@ export async function uploadMedia( {
|
|
|
160
162
|
|
|
161
163
|
// Don't allow empty files to be uploaded.
|
|
162
164
|
if ( mediaFile.size <= 0 ) {
|
|
163
|
-
|
|
165
|
+
onError( {
|
|
164
166
|
code: 'EMPTY_FILE',
|
|
165
|
-
message:
|
|
167
|
+
message: sprintf(
|
|
168
|
+
// translators: %s: file name.
|
|
169
|
+
__( '%s: This file is empty.' ),
|
|
170
|
+
mediaFile.name
|
|
171
|
+
),
|
|
166
172
|
file: mediaFile,
|
|
167
173
|
} );
|
|
168
174
|
continue;
|