@wordpress/media-utils 5.6.0 → 5.8.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 +8 -0
- package/README.md +70 -1
- package/build/index.js +43 -11
- package/build/index.js.map +1 -1
- package/build/utils/flatten-form-data.js +32 -0
- package/build/utils/flatten-form-data.js.map +1 -0
- package/build/utils/get-mime-types-array.js +27 -0
- package/build/utils/get-mime-types-array.js.map +1 -0
- package/build/utils/transform-attachment.js +33 -0
- package/build/utils/transform-attachment.js.map +1 -0
- package/build/utils/types.js +6 -0
- package/build/utils/types.js.map +1 -0
- package/build/utils/upload-error.js +29 -0
- package/build/utils/upload-error.js.map +1 -0
- package/build/utils/upload-media.js +53 -152
- package/build/utils/upload-media.js.map +1 -1
- package/build/utils/upload-to-server.js +34 -0
- package/build/utils/upload-to-server.js.map +1 -0
- package/build/utils/validate-file-size.js +44 -0
- package/build/utils/validate-file-size.js.map +1 -0
- package/build/utils/validate-mime-type-for-user.js +41 -0
- package/build/utils/validate-mime-type-for-user.js.map +1 -0
- package/build/utils/validate-mime-type.js +47 -0
- package/build/utils/validate-mime-type.js.map +1 -0
- package/build-module/index.js +5 -1
- package/build-module/index.js.map +1 -1
- package/build-module/utils/flatten-form-data.js +26 -0
- package/build-module/utils/flatten-form-data.js.map +1 -0
- package/build-module/utils/get-mime-types-array.js +21 -0
- package/build-module/utils/get-mime-types-array.js.map +1 -0
- package/build-module/utils/transform-attachment.js +27 -0
- package/build-module/utils/transform-attachment.js.map +1 -0
- package/build-module/utils/types.js +2 -0
- package/build-module/utils/types.js.map +1 -0
- package/build-module/utils/upload-error.js +22 -0
- package/build-module/utils/upload-error.js.map +1 -0
- package/build-module/utils/upload-media.js +53 -149
- package/build-module/utils/upload-media.js.map +1 -1
- package/build-module/utils/upload-to-server.js +26 -0
- package/build-module/utils/upload-to-server.js.map +1 -0
- package/build-module/utils/validate-file-size.js +38 -0
- package/build-module/utils/validate-file-size.js.map +1 -0
- package/build-module/utils/validate-mime-type-for-user.js +35 -0
- package/build-module/utils/validate-mime-type-for-user.js.map +1 -0
- package/build-module/utils/validate-mime-type.js +41 -0
- package/build-module/utils/validate-mime-type.js.map +1 -0
- package/build-types/components/index.d.ts +2 -0
- package/build-types/components/index.d.ts.map +1 -0
- package/build-types/components/media-upload/index.d.ts +30 -0
- package/build-types/components/media-upload/index.d.ts.map +1 -0
- package/build-types/index.d.ts +8 -0
- package/build-types/index.d.ts.map +1 -0
- package/build-types/utils/flatten-form-data.d.ts +9 -0
- package/build-types/utils/flatten-form-data.d.ts.map +1 -0
- package/build-types/utils/get-mime-types-array.d.ts +12 -0
- package/build-types/utils/get-mime-types-array.d.ts.map +1 -0
- package/build-types/utils/transform-attachment.d.ts +11 -0
- package/build-types/utils/transform-attachment.d.ts.map +1 -0
- package/build-types/utils/types.d.ts +199 -0
- package/build-types/utils/types.d.ts.map +1 -0
- package/build-types/utils/upload-error.d.ts +19 -0
- package/build-types/utils/upload-error.d.ts.map +1 -0
- package/build-types/utils/upload-media.d.ts +32 -0
- package/build-types/utils/upload-media.d.ts.map +1 -0
- package/build-types/utils/upload-to-server.d.ts +3 -0
- package/build-types/utils/upload-to-server.d.ts.map +1 -0
- package/build-types/utils/validate-file-size.d.ts +8 -0
- package/build-types/utils/validate-file-size.d.ts.map +1 -0
- package/build-types/utils/validate-mime-type-for-user.d.ts +8 -0
- package/build-types/utils/validate-mime-type-for-user.d.ts.map +1 -0
- package/build-types/utils/validate-mime-type.d.ts +8 -0
- package/build-types/utils/validate-mime-type.d.ts.map +1 -0
- package/package.json +8 -7
- package/src/index.ts +9 -0
- package/src/utils/flatten-form-data.ts +33 -0
- package/src/utils/get-mime-types-array.ts +29 -0
- package/src/utils/test/flatten-form-data.ts +49 -0
- package/src/utils/test/get-mime-types-array.ts +47 -0
- package/src/utils/test/upload-error.ts +24 -0
- package/src/utils/test/{upload-media.test.js → upload-media.ts} +47 -76
- package/src/utils/test/validate-file-size.ts +70 -0
- package/src/utils/test/validate-mime-type-for-user.ts +37 -0
- package/src/utils/test/validate-mime-type.ts +57 -0
- package/src/utils/transform-attachment.ts +24 -0
- package/src/utils/types.ts +207 -0
- package/src/utils/upload-error.ts +26 -0
- package/src/utils/upload-media.ts +149 -0
- package/src/utils/upload-to-server.ts +38 -0
- package/src/utils/validate-file-size.ts +44 -0
- package/src/utils/validate-mime-type-for-user.ts +46 -0
- package/src/utils/validate-mime-type.ts +43 -0
- package/tsconfig.json +17 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/build/utils/index.js +0 -13
- package/build/utils/index.js.map +0 -1
- package/build-module/utils/index.js +0 -2
- package/build-module/utils/index.js.map +0 -1
- package/src/index.js +0 -2
- package/src/utils/index.js +0 -1
- package/src/utils/upload-media.js +0 -232
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -13,6 +13,75 @@ npm install @wordpress/media-utils --save
|
|
|
13
13
|
|
|
14
14
|
_This package assumes that your code will run in an **ES2015+** environment. If you're using an environment that has limited or no support for such language features and APIs, you should include [the polyfill shipped in `@wordpress/babel-preset-default`](https://github.com/WordPress/gutenberg/tree/HEAD/packages/babel-preset-default#polyfill) in your code._
|
|
15
15
|
|
|
16
|
+
## API
|
|
17
|
+
|
|
18
|
+
<!-- START TOKEN(Autogenerated API docs) -->
|
|
19
|
+
|
|
20
|
+
### Attachment
|
|
21
|
+
|
|
22
|
+
Undocumented declaration.
|
|
23
|
+
|
|
24
|
+
### MediaUpload
|
|
25
|
+
|
|
26
|
+
Undocumented declaration.
|
|
27
|
+
|
|
28
|
+
### RestAttachment
|
|
29
|
+
|
|
30
|
+
Undocumented declaration.
|
|
31
|
+
|
|
32
|
+
### transformAttachment
|
|
33
|
+
|
|
34
|
+
Transforms an attachment object from the REST API shape into the shape expected by the block editor and other consumers.
|
|
35
|
+
|
|
36
|
+
_Parameters_
|
|
37
|
+
|
|
38
|
+
- _attachment_ `RestAttachment`: REST API attachment object.
|
|
39
|
+
|
|
40
|
+
### uploadMedia
|
|
41
|
+
|
|
42
|
+
Upload a media file when the file upload button is activated or when adding a file to the editor via drag & drop.
|
|
43
|
+
|
|
44
|
+
_Parameters_
|
|
45
|
+
|
|
46
|
+
- _$0_ `UploadMediaArgs`: Parameters object passed to the function.
|
|
47
|
+
- _$0.allowedTypes_ `UploadMediaArgs[ 'allowedTypes' ]`: Array with the types of media that can be uploaded, if unset all types are allowed.
|
|
48
|
+
- _$0.additionalData_ `UploadMediaArgs[ 'additionalData' ]`: Additional data to include in the request.
|
|
49
|
+
- _$0.filesList_ `UploadMediaArgs[ 'filesList' ]`: List of files.
|
|
50
|
+
- _$0.maxUploadFileSize_ `UploadMediaArgs[ 'maxUploadFileSize' ]`: Maximum upload size in bytes allowed for the site.
|
|
51
|
+
- _$0.onError_ `UploadMediaArgs[ 'onError' ]`: Function called when an error happens.
|
|
52
|
+
- _$0.onFileChange_ `UploadMediaArgs[ 'onFileChange' ]`: Function called each time a file or a temporary representation of the file is available.
|
|
53
|
+
- _$0.wpAllowedMimeTypes_ `UploadMediaArgs[ 'wpAllowedMimeTypes' ]`: List of allowed mime types and file extensions.
|
|
54
|
+
- _$0.signal_ `UploadMediaArgs[ 'signal' ]`: Abort signal.
|
|
55
|
+
|
|
56
|
+
### validateFileSize
|
|
57
|
+
|
|
58
|
+
Verifies whether the file is within the file upload size limits for the site.
|
|
59
|
+
|
|
60
|
+
_Parameters_
|
|
61
|
+
|
|
62
|
+
- _file_ `File`: File object.
|
|
63
|
+
- _maxUploadFileSize_ `number`: Maximum upload size in bytes allowed for the site.
|
|
64
|
+
|
|
65
|
+
### validateMimeType
|
|
66
|
+
|
|
67
|
+
Verifies if the caller (e.g. a block) supports this mime type.
|
|
68
|
+
|
|
69
|
+
_Parameters_
|
|
70
|
+
|
|
71
|
+
- _file_ `File`: File object.
|
|
72
|
+
- _allowedTypes_ `string[]`: List of allowed mime types.
|
|
73
|
+
|
|
74
|
+
### validateMimeTypeForUser
|
|
75
|
+
|
|
76
|
+
Verifies if the user is allowed to upload this mime type.
|
|
77
|
+
|
|
78
|
+
_Parameters_
|
|
79
|
+
|
|
80
|
+
- _file_ `File`: File object.
|
|
81
|
+
- _wpAllowedMimeTypes_ `Record< string, string > | null`: List of allowed mime types and file extensions.
|
|
82
|
+
|
|
83
|
+
<!-- END TOKEN(Autogenerated API docs) -->
|
|
84
|
+
|
|
16
85
|
## Usage
|
|
17
86
|
|
|
18
87
|
### uploadMedia
|
|
@@ -43,7 +112,7 @@ Beware that first onFileChange is called with temporary blob URLs and then with
|
|
|
43
112
|
### MediaUpload
|
|
44
113
|
|
|
45
114
|
Media upload component provides a UI button that allows users to open the WordPress media library. It is normally used in conjunction with the filter `editor.MediaUpload`.
|
|
46
|
-
The component follows the interface specified in
|
|
115
|
+
The component follows the interface specified in <https://github.com/WordPress/gutenberg/blob/HEAD/packages/block-editor/src/components/media-upload/README.md>, and more details regarding its usage can be checked there.
|
|
47
116
|
|
|
48
117
|
## Contributing to this package
|
|
49
118
|
|
package/build/index.js
CHANGED
|
@@ -3,9 +3,47 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
+
var _exportNames = {
|
|
7
|
+
uploadMedia: true,
|
|
8
|
+
transformAttachment: true,
|
|
9
|
+
validateFileSize: true,
|
|
10
|
+
validateMimeType: true,
|
|
11
|
+
validateMimeTypeForUser: true
|
|
12
|
+
};
|
|
13
|
+
Object.defineProperty(exports, "transformAttachment", {
|
|
14
|
+
enumerable: true,
|
|
15
|
+
get: function () {
|
|
16
|
+
return _transformAttachment.transformAttachment;
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
Object.defineProperty(exports, "uploadMedia", {
|
|
20
|
+
enumerable: true,
|
|
21
|
+
get: function () {
|
|
22
|
+
return _uploadMedia.uploadMedia;
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
Object.defineProperty(exports, "validateFileSize", {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
get: function () {
|
|
28
|
+
return _validateFileSize.validateFileSize;
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
Object.defineProperty(exports, "validateMimeType", {
|
|
32
|
+
enumerable: true,
|
|
33
|
+
get: function () {
|
|
34
|
+
return _validateMimeType.validateMimeType;
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
Object.defineProperty(exports, "validateMimeTypeForUser", {
|
|
38
|
+
enumerable: true,
|
|
39
|
+
get: function () {
|
|
40
|
+
return _validateMimeTypeForUser.validateMimeTypeForUser;
|
|
41
|
+
}
|
|
42
|
+
});
|
|
6
43
|
var _components = require("./components");
|
|
7
44
|
Object.keys(_components).forEach(function (key) {
|
|
8
45
|
if (key === "default" || key === "__esModule") return;
|
|
46
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
9
47
|
if (key in exports && exports[key] === _components[key]) return;
|
|
10
48
|
Object.defineProperty(exports, key, {
|
|
11
49
|
enumerable: true,
|
|
@@ -14,15 +52,9 @@ Object.keys(_components).forEach(function (key) {
|
|
|
14
52
|
}
|
|
15
53
|
});
|
|
16
54
|
});
|
|
17
|
-
var
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
enumerable: true,
|
|
23
|
-
get: function () {
|
|
24
|
-
return _utils[key];
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
});
|
|
55
|
+
var _uploadMedia = require("./utils/upload-media");
|
|
56
|
+
var _transformAttachment = require("./utils/transform-attachment");
|
|
57
|
+
var _validateFileSize = require("./utils/validate-file-size");
|
|
58
|
+
var _validateMimeType = require("./utils/validate-mime-type");
|
|
59
|
+
var _validateMimeTypeForUser = require("./utils/validate-mime-type-for-user");
|
|
28
60
|
//# sourceMappingURL=index.js.map
|
package/build/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_components","require","Object","keys","forEach","key","exports","defineProperty","enumerable","get","
|
|
1
|
+
{"version":3,"names":["_components","require","Object","keys","forEach","key","prototype","hasOwnProperty","call","_exportNames","exports","defineProperty","enumerable","get","_uploadMedia","_transformAttachment","_validateFileSize","_validateMimeType","_validateMimeTypeForUser"],"sources":["@wordpress/media-utils/src/index.ts"],"sourcesContent":["export * from './components';\n\nexport { uploadMedia } from './utils/upload-media';\nexport { transformAttachment } from './utils/transform-attachment';\nexport { validateFileSize } from './utils/validate-file-size';\nexport { validateMimeType } from './utils/validate-mime-type';\nexport { validateMimeTypeForUser } from './utils/validate-mime-type-for-user';\n\nexport type { Attachment, RestAttachment } from './utils/types';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,IAAAA,WAAA,GAAAC,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAH,WAAA,EAAAI,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAH,MAAA,CAAAI,SAAA,CAAAC,cAAA,CAAAC,IAAA,CAAAC,YAAA,EAAAJ,GAAA;EAAA,IAAAA,GAAA,IAAAK,OAAA,IAAAA,OAAA,CAAAL,GAAA,MAAAL,WAAA,CAAAK,GAAA;EAAAH,MAAA,CAAAS,cAAA,CAAAD,OAAA,EAAAL,GAAA;IAAAO,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAb,WAAA,CAAAK,GAAA;IAAA;EAAA;AAAA;AAEA,IAAAS,YAAA,GAAAb,OAAA;AACA,IAAAc,oBAAA,GAAAd,OAAA;AACA,IAAAe,iBAAA,GAAAf,OAAA;AACA,IAAAgB,iBAAA,GAAAhB,OAAA;AACA,IAAAiB,wBAAA,GAAAjB,OAAA","ignoreList":[]}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.flattenFormData = flattenFormData;
|
|
7
|
+
/**
|
|
8
|
+
* Determines whether the passed argument appears to be a plain object.
|
|
9
|
+
*
|
|
10
|
+
* @param data The object to inspect.
|
|
11
|
+
*/
|
|
12
|
+
function isPlainObject(data) {
|
|
13
|
+
return data !== null && typeof data === 'object' && Object.getPrototypeOf(data) === Object.prototype;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Recursively flatten data passed to form data, to allow using multi-level objects.
|
|
18
|
+
*
|
|
19
|
+
* @param {FormData} formData Form data object.
|
|
20
|
+
* @param {string} key Key to amend to form data object
|
|
21
|
+
* @param {string|Object} data Data to be amended to form data.
|
|
22
|
+
*/
|
|
23
|
+
function flattenFormData(formData, key, data) {
|
|
24
|
+
if (isPlainObject(data)) {
|
|
25
|
+
for (const [name, value] of Object.entries(data)) {
|
|
26
|
+
flattenFormData(formData, `${key}[${name}]`, value);
|
|
27
|
+
}
|
|
28
|
+
} else if (data !== undefined) {
|
|
29
|
+
formData.append(key, String(data));
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=flatten-form-data.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["isPlainObject","data","Object","getPrototypeOf","prototype","flattenFormData","formData","key","name","value","entries","undefined","append","String"],"sources":["@wordpress/media-utils/src/utils/flatten-form-data.ts"],"sourcesContent":["/**\n * Determines whether the passed argument appears to be a plain object.\n *\n * @param data The object to inspect.\n */\nfunction isPlainObject( data: unknown ): data is Record< string, unknown > {\n\treturn (\n\t\tdata !== null &&\n\t\ttypeof data === 'object' &&\n\t\tObject.getPrototypeOf( data ) === Object.prototype\n\t);\n}\n\n/**\n * Recursively flatten data passed to form data, to allow using multi-level objects.\n *\n * @param {FormData} formData Form data object.\n * @param {string} key Key to amend to form data object\n * @param {string|Object} data Data to be amended to form data.\n */\nexport function flattenFormData(\n\tformData: FormData,\n\tkey: string,\n\tdata: string | undefined | Record< string, string >\n) {\n\tif ( isPlainObject( data ) ) {\n\t\tfor ( const [ name, value ] of Object.entries( data ) ) {\n\t\t\tflattenFormData( formData, `${ key }[${ name }]`, value );\n\t\t}\n\t} else if ( data !== undefined ) {\n\t\tformData.append( key, String( data ) );\n\t}\n}\n"],"mappings":";;;;;;AAAA;AACA;AACA;AACA;AACA;AACA,SAASA,aAAaA,CAAEC,IAAa,EAAsC;EAC1E,OACCA,IAAI,KAAK,IAAI,IACb,OAAOA,IAAI,KAAK,QAAQ,IACxBC,MAAM,CAACC,cAAc,CAAEF,IAAK,CAAC,KAAKC,MAAM,CAACE,SAAS;AAEpD;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASC,eAAeA,CAC9BC,QAAkB,EAClBC,GAAW,EACXN,IAAmD,EAClD;EACD,IAAKD,aAAa,CAAEC,IAAK,CAAC,EAAG;IAC5B,KAAM,MAAM,CAAEO,IAAI,EAAEC,KAAK,CAAE,IAAIP,MAAM,CAACQ,OAAO,CAAET,IAAK,CAAC,EAAG;MACvDI,eAAe,CAAEC,QAAQ,EAAG,GAAGC,GAAK,IAAIC,IAAM,GAAE,EAAEC,KAAM,CAAC;IAC1D;EACD,CAAC,MAAM,IAAKR,IAAI,KAAKU,SAAS,EAAG;IAChCL,QAAQ,CAACM,MAAM,CAAEL,GAAG,EAAEM,MAAM,CAAEZ,IAAK,CAAE,CAAC;EACvC;AACD","ignoreList":[]}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.getMimeTypesArray = getMimeTypesArray;
|
|
7
|
+
/**
|
|
8
|
+
* Browsers may use unexpected mime types, and they differ from browser to browser.
|
|
9
|
+
* This function computes a flexible array of mime types from the mime type structured provided by the server.
|
|
10
|
+
* Converts { jpg|jpeg|jpe: "image/jpeg" } into [ "image/jpeg", "image/jpg", "image/jpeg", "image/jpe" ]
|
|
11
|
+
*
|
|
12
|
+
* @param {?Object} wpMimeTypesObject Mime type object received from the server.
|
|
13
|
+
* Extensions are keys separated by '|' and values are mime types associated with an extension.
|
|
14
|
+
*
|
|
15
|
+
* @return An array of mime types or null
|
|
16
|
+
*/
|
|
17
|
+
function getMimeTypesArray(wpMimeTypesObject) {
|
|
18
|
+
if (!wpMimeTypesObject) {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
return Object.entries(wpMimeTypesObject).flatMap(([extensionsString, mime]) => {
|
|
22
|
+
const [type] = mime.split('/');
|
|
23
|
+
const extensions = extensionsString.split('|');
|
|
24
|
+
return [mime, ...extensions.map(extension => `${type}/${extension}`)];
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=get-mime-types-array.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["getMimeTypesArray","wpMimeTypesObject","Object","entries","flatMap","extensionsString","mime","type","split","extensions","map","extension"],"sources":["@wordpress/media-utils/src/utils/get-mime-types-array.ts"],"sourcesContent":["/**\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 *\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 An array of mime types or null\n */\nexport function getMimeTypesArray(\n\twpMimeTypesObject?: Record< string, string > | null\n) {\n\tif ( ! wpMimeTypesObject ) {\n\t\treturn null;\n\t}\n\treturn Object.entries( wpMimeTypesObject ).flatMap(\n\t\t( [ extensionsString, mime ] ) => {\n\t\t\tconst [ type ] = mime.split( '/' );\n\t\t\tconst extensions = extensionsString.split( '|' );\n\t\t\treturn [\n\t\t\t\tmime,\n\t\t\t\t...extensions.map(\n\t\t\t\t\t( extension ) => `${ type }/${ extension }`\n\t\t\t\t),\n\t\t\t];\n\t\t}\n\t);\n}\n"],"mappings":";;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASA,iBAAiBA,CAChCC,iBAAmD,EAClD;EACD,IAAK,CAAEA,iBAAiB,EAAG;IAC1B,OAAO,IAAI;EACZ;EACA,OAAOC,MAAM,CAACC,OAAO,CAAEF,iBAAkB,CAAC,CAACG,OAAO,CACjD,CAAE,CAAEC,gBAAgB,EAAEC,IAAI,CAAE,KAAM;IACjC,MAAM,CAAEC,IAAI,CAAE,GAAGD,IAAI,CAACE,KAAK,CAAE,GAAI,CAAC;IAClC,MAAMC,UAAU,GAAGJ,gBAAgB,CAACG,KAAK,CAAE,GAAI,CAAC;IAChD,OAAO,CACNF,IAAI,EACJ,GAAGG,UAAU,CAACC,GAAG,CACdC,SAAS,IAAO,GAAGJ,IAAM,IAAII,SAAW,EAC3C,CAAC,CACD;EACF,CACD,CAAC;AACF","ignoreList":[]}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.transformAttachment = transformAttachment;
|
|
7
|
+
/**
|
|
8
|
+
* Internal dependencies
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Transforms an attachment object from the REST API shape into the shape expected by the block editor and other consumers.
|
|
13
|
+
*
|
|
14
|
+
* @param attachment REST API attachment object.
|
|
15
|
+
*/
|
|
16
|
+
function transformAttachment(attachment) {
|
|
17
|
+
var _attachment$caption$r;
|
|
18
|
+
// eslint-disable-next-line camelcase
|
|
19
|
+
const {
|
|
20
|
+
alt_text,
|
|
21
|
+
source_url,
|
|
22
|
+
...savedMediaProps
|
|
23
|
+
} = attachment;
|
|
24
|
+
return {
|
|
25
|
+
...savedMediaProps,
|
|
26
|
+
alt: attachment.alt_text,
|
|
27
|
+
caption: (_attachment$caption$r = attachment.caption?.raw) !== null && _attachment$caption$r !== void 0 ? _attachment$caption$r : '',
|
|
28
|
+
title: attachment.title.raw,
|
|
29
|
+
url: attachment.source_url,
|
|
30
|
+
poster: attachment._embedded?.['wp:featuredmedia']?.[0]?.source_url || undefined
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=transform-attachment.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["transformAttachment","attachment","_attachment$caption$r","alt_text","source_url","savedMediaProps","alt","caption","raw","title","url","poster","_embedded","undefined"],"sources":["@wordpress/media-utils/src/utils/transform-attachment.ts"],"sourcesContent":["/**\n * Internal dependencies\n */\nimport type { Attachment, RestAttachment } from './types';\n\n/**\n * Transforms an attachment object from the REST API shape into the shape expected by the block editor and other consumers.\n *\n * @param attachment REST API attachment object.\n */\nexport function transformAttachment( attachment: RestAttachment ): Attachment {\n\t// eslint-disable-next-line camelcase\n\tconst { alt_text, source_url, ...savedMediaProps } = attachment;\n\treturn {\n\t\t...savedMediaProps,\n\t\talt: attachment.alt_text,\n\t\tcaption: attachment.caption?.raw ?? '',\n\t\ttitle: attachment.title.raw,\n\t\turl: attachment.source_url,\n\t\tposter:\n\t\t\tattachment._embedded?.[ 'wp:featuredmedia' ]?.[ 0 ]?.source_url ||\n\t\t\tundefined,\n\t};\n}\n"],"mappings":";;;;;;AAAA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACO,SAASA,mBAAmBA,CAAEC,UAA0B,EAAe;EAAA,IAAAC,qBAAA;EAC7E;EACA,MAAM;IAAEC,QAAQ;IAAEC,UAAU;IAAE,GAAGC;EAAgB,CAAC,GAAGJ,UAAU;EAC/D,OAAO;IACN,GAAGI,eAAe;IAClBC,GAAG,EAAEL,UAAU,CAACE,QAAQ;IACxBI,OAAO,GAAAL,qBAAA,GAAED,UAAU,CAACM,OAAO,EAAEC,GAAG,cAAAN,qBAAA,cAAAA,qBAAA,GAAI,EAAE;IACtCO,KAAK,EAAER,UAAU,CAACQ,KAAK,CAACD,GAAG;IAC3BE,GAAG,EAAET,UAAU,CAACG,UAAU;IAC1BO,MAAM,EACLV,UAAU,CAACW,SAAS,GAAI,kBAAkB,CAAE,GAAI,CAAC,CAAE,EAAER,UAAU,IAC/DS;EACF,CAAC;AACF","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":[],"sources":["@wordpress/media-utils/src/utils/types.ts"],"sourcesContent":["/**\n * A media attachment object in a REST API context.\n *\n * Simplified version of what's defined in the wp-types package.\n *\n * @see https://www.npmjs.com/package/wp-types\n */\ninterface WP_REST_API_Attachment {\n\t/**\n\t * Unique identifier for the attachment.\n\t */\n\tid: number;\n\t/**\n\t * The ID of the featured media for the post.\n\t */\n\tfeatured_media: number;\n\t/**\n\t * URL to the attachment.\n\t */\n\tlink: string;\n\t/**\n\t * The date the attachment was published, in the site's timezone.\n\t */\n\tdate: string;\n\t/**\n\t * The date the attachment was published, as GMT.\n\t */\n\tdate_gmt: string;\n\t/**\n\t * The date the attachment was last modified, in the site's timezone.\n\t */\n\tmodified: string;\n\t/**\n\t * The date the attachment was last modified, as GMT.\n\t */\n\tmodified_gmt: string;\n\t/**\n\t * An alphanumeric identifier for the attachment unique to its type.\n\t */\n\tslug: string;\n\t/**\n\t * A named status for the attachment.\n\t */\n\tstatus: string;\n\t/**\n\t * Type of Post for the attachment.\n\t */\n\ttype: 'attachment';\n\t/**\n\t * Alternative text to display when attachment is not displayed.\n\t */\n\talt_text: string;\n\t/**\n\t * The attachment caption.\n\t */\n\tcaption: {\n\t\t/**\n\t\t * Caption for the attachment, as it exists in the database. Only present when using the 'edit' context.\n\t\t */\n\t\traw?: string;\n\t\t/**\n\t\t * HTML caption for the attachment, transformed for display.\n\t\t */\n\t\trendered: string;\n\t};\n\t/**\n\t * The attachment description.\n\t */\n\tdescription: {\n\t\t/**\n\t\t * Description for the attachment, as it exists in the database. Only present when using the 'edit' context.\n\t\t */\n\t\traw?: string;\n\t\t/**\n\t\t * HTML description for the attachment, transformed for display.\n\t\t */\n\t\trendered: string;\n\t};\n\t/**\n\t * Attachment type.\n\t */\n\tmedia_type: 'image' | 'file';\n\t/**\n\t * The attachment MIME type.\n\t */\n\tmime_type: string;\n\t/**\n\t * Details about the media file, specific to its type.\n\t */\n\tmedia_details: {\n\t\t[ k: string ]: unknown;\n\t};\n\t/**\n\t * The ID for the associated post of the attachment.\n\t */\n\tpost: number | null;\n\t/**\n\t * URL to the original attachment file.\n\t */\n\tsource_url: string;\n\t/**\n\t * List of the missing image sizes of the attachment. Only present when using the 'edit' context.\n\t */\n\tmissing_image_sizes?: string[];\n\t/**\n\t * Permalink template for the attachment. Only present when using the 'edit' context and the post type is public.\n\t */\n\tpermalink_template?: string;\n\t/**\n\t * Slug automatically generated from the attachment title. Only present when using the 'edit' context and the post type is public.\n\t */\n\tgenerated_slug?: string;\n\t/**\n\t * An array of the class names for the post container element.\n\t */\n\tclass_list: string[];\n\t/**\n\t * The title for the attachment.\n\t */\n\ttitle: {\n\t\t/**\n\t\t * Title for the attachment, as it exists in the database. Only present when using the 'edit' context.\n\t\t */\n\t\traw?: string;\n\t\t/**\n\t\t * HTML title for the attachment, transformed for display.\n\t\t */\n\t\trendered: string;\n\t};\n\t/**\n\t * The ID for the author of the attachment.\n\t */\n\tauthor: number;\n\t/**\n\t * Whether or not comments are open on the attachment.\n\t */\n\tcomment_status: string;\n\t/**\n\t * Whether or not the attachment can be pinged.\n\t */\n\tping_status: string;\n\t/**\n\t * Meta fields.\n\t */\n\tmeta:\n\t\t| []\n\t\t| {\n\t\t\t\t[ k: string ]: unknown;\n\t\t };\n\t/**\n\t * The theme file to use to display the attachment.\n\t */\n\ttemplate: string;\n\t_links: {\n\t\t[ k: string ]: {\n\t\t\thref: string;\n\t\t\tembeddable?: boolean;\n\t\t\t[ k: string ]: unknown;\n\t\t}[];\n\t};\n\t/**\n\t * The embedded representation of relations. Only present when the '_embed' query parameter is set.\n\t */\n\t_embedded?: {\n\t\t/**\n\t\t * The author of the post.\n\t\t */\n\t\tauthor: unknown[];\n\t\t/**\n\t\t * The featured image post.\n\t\t */\n\t\t'wp:featuredmedia'?: WP_REST_API_Attachment[];\n\t\t[ k: string ]: unknown;\n\t};\n\t[ k: string ]: unknown;\n}\n\n/**\n * REST API attachment object with additional fields added by this project.\n */\nexport interface RestAttachment extends WP_REST_API_Attachment {}\n\ntype BetterOmit< T, K extends PropertyKey > = {\n\t[ P in keyof T as P extends K ? never : P ]: T[ P ];\n};\n\n/**\n * Transformed attachment object.\n */\nexport type Attachment = BetterOmit<\n\tRestAttachment,\n\t'alt_text' | 'source_url' | 'caption' | 'title'\n> & {\n\talt: WP_REST_API_Attachment[ 'alt_text' ];\n\tcaption: WP_REST_API_Attachment[ 'caption' ][ 'raw' ] & string;\n\ttitle: WP_REST_API_Attachment[ 'title' ][ 'raw' ];\n\turl: WP_REST_API_Attachment[ 'source_url' ];\n\tposter?: WP_REST_API_Attachment[ 'source_url' ];\n};\n\nexport type OnChangeHandler = ( attachments: Partial< Attachment >[] ) => void;\nexport type OnSuccessHandler = ( attachments: Partial< Attachment >[] ) => void;\nexport type OnErrorHandler = ( error: Error ) => void;\n\nexport type CreateRestAttachment = Partial< RestAttachment >;\n\nexport type AdditionalData = BetterOmit< CreateRestAttachment, 'meta' >;\n"],"mappings":"","ignoreList":[]}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.UploadError = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* MediaError class.
|
|
9
|
+
*
|
|
10
|
+
* Small wrapper around the `Error` class
|
|
11
|
+
* to hold an error code and a reference to a file object.
|
|
12
|
+
*/
|
|
13
|
+
class UploadError extends Error {
|
|
14
|
+
constructor({
|
|
15
|
+
code,
|
|
16
|
+
message,
|
|
17
|
+
file,
|
|
18
|
+
cause
|
|
19
|
+
}) {
|
|
20
|
+
super(message, {
|
|
21
|
+
cause
|
|
22
|
+
});
|
|
23
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
24
|
+
this.code = code;
|
|
25
|
+
this.file = file;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.UploadError = UploadError;
|
|
29
|
+
//# sourceMappingURL=upload-error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["UploadError","Error","constructor","code","message","file","cause","Object","setPrototypeOf","new","target","prototype","exports"],"sources":["@wordpress/media-utils/src/utils/upload-error.ts"],"sourcesContent":["interface UploadErrorArgs {\n\tcode: string;\n\tmessage: string;\n\tfile: File;\n\tcause?: Error;\n}\n\n/**\n * MediaError class.\n *\n * Small wrapper around the `Error` class\n * to hold an error code and a reference to a file object.\n */\nexport class UploadError extends Error {\n\tcode: string;\n\tfile: File;\n\n\tconstructor( { code, message, file, cause }: UploadErrorArgs ) {\n\t\tsuper( message, { cause } );\n\n\t\tObject.setPrototypeOf( this, new.target.prototype );\n\n\t\tthis.code = code;\n\t\tthis.file = file;\n\t}\n}\n"],"mappings":";;;;;;AAOA;AACA;AACA;AACA;AACA;AACA;AACO,MAAMA,WAAW,SAASC,KAAK,CAAC;EAItCC,WAAWA,CAAE;IAAEC,IAAI;IAAEC,OAAO;IAAEC,IAAI;IAAEC;EAAuB,CAAC,EAAG;IAC9D,KAAK,CAAEF,OAAO,EAAE;MAAEE;IAAM,CAAE,CAAC;IAE3BC,MAAM,CAACC,cAAc,CAAE,IAAI,EAAEC,GAAG,CAACC,MAAM,CAACC,SAAU,CAAC;IAEnD,IAAI,CAACR,IAAI,GAAGA,IAAI;IAChB,IAAI,CAACE,IAAI,GAAGA,IAAI;EACjB;AACD;AAACO,OAAA,CAAAZ,WAAA,GAAAA,WAAA","ignoreList":[]}
|
|
@@ -1,146 +1,81 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
3
|
Object.defineProperty(exports, "__esModule", {
|
|
5
4
|
value: true
|
|
6
5
|
});
|
|
7
|
-
exports.getMimeTypesArray = getMimeTypesArray;
|
|
8
6
|
exports.uploadMedia = uploadMedia;
|
|
9
|
-
var _apiFetch = _interopRequireDefault(require("@wordpress/api-fetch"));
|
|
10
|
-
var _blob = require("@wordpress/blob");
|
|
11
7
|
var _i18n = require("@wordpress/i18n");
|
|
8
|
+
var _blob = require("@wordpress/blob");
|
|
9
|
+
var _uploadToServer = require("./upload-to-server");
|
|
10
|
+
var _validateMimeType = require("./validate-mime-type");
|
|
11
|
+
var _validateMimeTypeForUser = require("./validate-mime-type-for-user");
|
|
12
|
+
var _validateFileSize = require("./validate-file-size");
|
|
13
|
+
var _uploadError = require("./upload-error");
|
|
12
14
|
/**
|
|
13
15
|
* WordPress dependencies
|
|
14
16
|
*/
|
|
15
17
|
|
|
16
|
-
const noop = () => {};
|
|
17
|
-
|
|
18
18
|
/**
|
|
19
|
-
*
|
|
20
|
-
* This function computes a flexible array of mime types from the mime type structured provided by the server.
|
|
21
|
-
* Converts { jpg|jpeg|jpe: "image/jpeg" } into [ "image/jpeg", "image/jpg", "image/jpeg", "image/jpe" ]
|
|
22
|
-
* The computation of this array instead of directly using the object,
|
|
23
|
-
* solves the problem in chrome where mp3 files have audio/mp3 as mime type instead of audio/mpeg.
|
|
24
|
-
* https://bugs.chromium.org/p/chromium/issues/detail?id=227004
|
|
25
|
-
*
|
|
26
|
-
* @param {?Object} wpMimeTypesObject Mime type object received from the server.
|
|
27
|
-
* Extensions are keys separated by '|' and values are mime types associated with an extension.
|
|
28
|
-
*
|
|
29
|
-
* @return {?Array} An array of mime types or the parameter passed if it was "falsy".
|
|
19
|
+
* Internal dependencies
|
|
30
20
|
*/
|
|
31
|
-
function getMimeTypesArray(wpMimeTypesObject) {
|
|
32
|
-
if (!wpMimeTypesObject) {
|
|
33
|
-
return wpMimeTypesObject;
|
|
34
|
-
}
|
|
35
|
-
return Object.entries(wpMimeTypesObject).map(([extensionsString, mime]) => {
|
|
36
|
-
const [type] = mime.split('/');
|
|
37
|
-
const extensions = extensionsString.split('|');
|
|
38
|
-
return [mime, ...extensions.map(extension => `${type}/${extension}`)];
|
|
39
|
-
}).flat();
|
|
40
|
-
}
|
|
41
21
|
|
|
42
22
|
/**
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
* TODO: future enhancement to add an upload indicator.
|
|
23
|
+
* Upload a media file when the file upload button is activated
|
|
24
|
+
* or when adding a file to the editor via drag & drop.
|
|
47
25
|
*
|
|
48
|
-
* @param
|
|
49
|
-
* @param
|
|
50
|
-
* @param
|
|
51
|
-
* @param
|
|
52
|
-
* @param
|
|
53
|
-
* @param
|
|
54
|
-
* @param
|
|
55
|
-
* @param
|
|
26
|
+
* @param $0 Parameters object passed to the function.
|
|
27
|
+
* @param $0.allowedTypes Array with the types of media that can be uploaded, if unset all types are allowed.
|
|
28
|
+
* @param $0.additionalData Additional data to include in the request.
|
|
29
|
+
* @param $0.filesList List of files.
|
|
30
|
+
* @param $0.maxUploadFileSize Maximum upload size in bytes allowed for the site.
|
|
31
|
+
* @param $0.onError Function called when an error happens.
|
|
32
|
+
* @param $0.onFileChange Function called each time a file or a temporary representation of the file is available.
|
|
33
|
+
* @param $0.wpAllowedMimeTypes List of allowed mime types and file extensions.
|
|
34
|
+
* @param $0.signal Abort signal.
|
|
56
35
|
*/
|
|
57
|
-
|
|
36
|
+
function uploadMedia({
|
|
37
|
+
wpAllowedMimeTypes,
|
|
58
38
|
allowedTypes,
|
|
59
39
|
additionalData = {},
|
|
60
40
|
filesList,
|
|
61
41
|
maxUploadFileSize,
|
|
62
|
-
onError
|
|
42
|
+
onError,
|
|
63
43
|
onFileChange,
|
|
64
|
-
|
|
44
|
+
signal
|
|
65
45
|
}) {
|
|
66
|
-
|
|
67
|
-
const files = [...filesList];
|
|
46
|
+
const validFiles = [];
|
|
68
47
|
const filesSet = [];
|
|
69
|
-
const setAndUpdateFiles = (
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
onFileChange(filesSet.filter(Boolean));
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
// Allowed type specified by consumer.
|
|
76
|
-
const isAllowedType = fileType => {
|
|
77
|
-
if (!allowedTypes) {
|
|
78
|
-
return true;
|
|
48
|
+
const setAndUpdateFiles = (index, value) => {
|
|
49
|
+
if (filesSet[index]?.url) {
|
|
50
|
+
(0, _blob.revokeBlobURL)(filesSet[index].url);
|
|
79
51
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
if (allowedType.includes('/')) {
|
|
83
|
-
return allowedType === fileType;
|
|
84
|
-
}
|
|
85
|
-
// Otherwise a general mime type is used and we should verify if the file mimetype starts with it.
|
|
86
|
-
return fileType.startsWith(`${allowedType}/`);
|
|
87
|
-
});
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
// Allowed types for the current WP_User.
|
|
91
|
-
const allowedMimeTypesForUser = getMimeTypesArray(wpAllowedMimeTypes);
|
|
92
|
-
const isAllowedMimeTypeForUser = fileType => {
|
|
93
|
-
return allowedMimeTypesForUser.includes(fileType);
|
|
52
|
+
filesSet[index] = value;
|
|
53
|
+
onFileChange?.(filesSet.filter(attachment => attachment !== null));
|
|
94
54
|
};
|
|
95
|
-
const
|
|
96
|
-
for (const mediaFile of files) {
|
|
55
|
+
for (const mediaFile of filesList) {
|
|
97
56
|
// Verify if user is allowed to upload this mime type.
|
|
98
57
|
// Defer to the server when type not detected.
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
// translators: %s: file name.
|
|
104
|
-
(0, _i18n.__)('%s: Sorry, you are not allowed to upload this file type.'), mediaFile.name),
|
|
105
|
-
file: mediaFile
|
|
106
|
-
});
|
|
58
|
+
try {
|
|
59
|
+
(0, _validateMimeTypeForUser.validateMimeTypeForUser)(mediaFile, wpAllowedMimeTypes);
|
|
60
|
+
} catch (error) {
|
|
61
|
+
onError?.(error);
|
|
107
62
|
continue;
|
|
108
63
|
}
|
|
109
64
|
|
|
110
|
-
// Check if the block supports this mime type.
|
|
65
|
+
// Check if the caller (e.g. a block) supports this mime type.
|
|
111
66
|
// Defer to the server when type not detected.
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
// translators: %s: file name.
|
|
117
|
-
(0, _i18n.__)('%s: Sorry, this file type is not supported here.'), mediaFile.name),
|
|
118
|
-
file: mediaFile
|
|
119
|
-
});
|
|
67
|
+
try {
|
|
68
|
+
(0, _validateMimeType.validateMimeType)(mediaFile, allowedTypes);
|
|
69
|
+
} catch (error) {
|
|
70
|
+
onError?.(error);
|
|
120
71
|
continue;
|
|
121
72
|
}
|
|
122
73
|
|
|
123
74
|
// Verify if file is greater than the maximum file upload size allowed for the site.
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
// translators: %s: file name.
|
|
129
|
-
(0, _i18n.__)('%s: This file exceeds the maximum upload size for this site.'), mediaFile.name),
|
|
130
|
-
file: mediaFile
|
|
131
|
-
});
|
|
132
|
-
continue;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// Don't allow empty files to be uploaded.
|
|
136
|
-
if (mediaFile.size <= 0) {
|
|
137
|
-
onError({
|
|
138
|
-
code: 'EMPTY_FILE',
|
|
139
|
-
message: (0, _i18n.sprintf)(
|
|
140
|
-
// translators: %s: file name.
|
|
141
|
-
(0, _i18n.__)('%s: This file is empty.'), mediaFile.name),
|
|
142
|
-
file: mediaFile
|
|
143
|
-
});
|
|
75
|
+
try {
|
|
76
|
+
(0, _validateFileSize.validateFileSize)(mediaFile, maxUploadFileSize);
|
|
77
|
+
} catch (error) {
|
|
78
|
+
onError?.(error);
|
|
144
79
|
continue;
|
|
145
80
|
}
|
|
146
81
|
validFiles.push(mediaFile);
|
|
@@ -150,64 +85,30 @@ async function uploadMedia({
|
|
|
150
85
|
filesSet.push({
|
|
151
86
|
url: (0, _blob.createBlobURL)(mediaFile)
|
|
152
87
|
});
|
|
153
|
-
onFileChange(filesSet);
|
|
88
|
+
onFileChange?.(filesSet);
|
|
154
89
|
}
|
|
155
|
-
|
|
156
|
-
const mediaFile = validFiles[idx];
|
|
90
|
+
validFiles.map(async (file, index) => {
|
|
157
91
|
try {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
// eslint-disable-next-line camelcase
|
|
161
|
-
const {
|
|
162
|
-
alt_text,
|
|
163
|
-
source_url,
|
|
164
|
-
...savedMediaProps
|
|
165
|
-
} = savedMedia;
|
|
166
|
-
const mediaObject = {
|
|
167
|
-
...savedMediaProps,
|
|
168
|
-
alt: savedMedia.alt_text,
|
|
169
|
-
caption: (_savedMedia$caption$r = savedMedia.caption?.raw) !== null && _savedMedia$caption$r !== void 0 ? _savedMedia$caption$r : '',
|
|
170
|
-
title: savedMedia.title.raw,
|
|
171
|
-
url: savedMedia.source_url
|
|
172
|
-
};
|
|
173
|
-
setAndUpdateFiles(idx, mediaObject);
|
|
92
|
+
const attachment = await (0, _uploadToServer.uploadToServer)(file, additionalData, signal);
|
|
93
|
+
setAndUpdateFiles(index, attachment);
|
|
174
94
|
} catch (error) {
|
|
175
95
|
// Reset to empty on failure.
|
|
176
|
-
setAndUpdateFiles(
|
|
96
|
+
setAndUpdateFiles(index, null);
|
|
177
97
|
let message;
|
|
178
|
-
if (error
|
|
98
|
+
if (error instanceof Error) {
|
|
179
99
|
message = error.message;
|
|
180
100
|
} else {
|
|
181
101
|
message = (0, _i18n.sprintf)(
|
|
182
102
|
// translators: %s: file name
|
|
183
|
-
(0, _i18n.__)('Error while uploading file %s to the media library.'),
|
|
103
|
+
(0, _i18n.__)('Error while uploading file %s to the media library.'), file.name);
|
|
184
104
|
}
|
|
185
|
-
onError({
|
|
105
|
+
onError?.(new _uploadError.UploadError({
|
|
186
106
|
code: 'GENERAL',
|
|
187
107
|
message,
|
|
188
|
-
file
|
|
189
|
-
|
|
108
|
+
file,
|
|
109
|
+
cause: error instanceof Error ? error : undefined
|
|
110
|
+
}));
|
|
190
111
|
}
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
/**
|
|
195
|
-
* @param {File} file Media File to Save.
|
|
196
|
-
* @param {?Object} additionalData Additional data to include in the request.
|
|
197
|
-
*
|
|
198
|
-
* @return {Promise} Media Object Promise.
|
|
199
|
-
*/
|
|
200
|
-
function createMediaFromFile(file, additionalData) {
|
|
201
|
-
// Create upload payload.
|
|
202
|
-
const data = new window.FormData();
|
|
203
|
-
data.append('file', file, file.name || file.type.replace('/', '.'));
|
|
204
|
-
if (additionalData) {
|
|
205
|
-
Object.entries(additionalData).forEach(([key, value]) => data.append(key, value));
|
|
206
|
-
}
|
|
207
|
-
return (0, _apiFetch.default)({
|
|
208
|
-
path: '/wp/v2/media',
|
|
209
|
-
body: data,
|
|
210
|
-
method: 'POST'
|
|
211
112
|
});
|
|
212
113
|
}
|
|
213
114
|
//# sourceMappingURL=upload-media.js.map
|