media-utils.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. /******/ (function() { // webpackBootstrap
  2. /******/ "use strict";
  3. /******/ // The require scope
  4. /******/ var __webpack_require__ = {};
  5. /******/
  6. /************************************************************************/
  7. /******/ /* webpack/runtime/compat get default export */
  8. /******/ !function() {
  9. /******/ // getDefaultExport function for compatibility with non-harmony modules
  10. /******/ __webpack_require__.n = function(module) {
  11. /******/ var getter = module && module.__esModule ?
  12. /******/ function() { return module['default']; } :
  13. /******/ function() { return module; };
  14. /******/ __webpack_require__.d(getter, { a: getter });
  15. /******/ return getter;
  16. /******/ };
  17. /******/ }();
  18. /******/
  19. /******/ /* webpack/runtime/define property getters */
  20. /******/ !function() {
  21. /******/ // define getter functions for harmony exports
  22. /******/ __webpack_require__.d = function(exports, definition) {
  23. /******/ for(var key in definition) {
  24. /******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
  25. /******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
  26. /******/ }
  27. /******/ }
  28. /******/ };
  29. /******/ }();
  30. /******/
  31. /******/ /* webpack/runtime/hasOwnProperty shorthand */
  32. /******/ !function() {
  33. /******/ __webpack_require__.o = function(obj, prop) { return Object.prototype.hasOwnProperty.call(obj, prop); }
  34. /******/ }();
  35. /******/
  36. /******/ /* webpack/runtime/make namespace object */
  37. /******/ !function() {
  38. /******/ // define __esModule on exports
  39. /******/ __webpack_require__.r = function(exports) {
  40. /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
  41. /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  42. /******/ }
  43. /******/ Object.defineProperty(exports, '__esModule', { value: true });
  44. /******/ };
  45. /******/ }();
  46. /******/
  47. /************************************************************************/
  48. var __webpack_exports__ = {};
  49. // ESM COMPAT FLAG
  50. __webpack_require__.r(__webpack_exports__);
  51. // EXPORTS
  52. __webpack_require__.d(__webpack_exports__, {
  53. "MediaUpload": function() { return /* reexport */ media_upload; },
  54. "uploadMedia": function() { return /* reexport */ uploadMedia; }
  55. });
  56. ;// CONCATENATED MODULE: external "lodash"
  57. var external_lodash_namespaceObject = window["lodash"];
  58. ;// CONCATENATED MODULE: external ["wp","element"]
  59. var external_wp_element_namespaceObject = window["wp"]["element"];
  60. ;// CONCATENATED MODULE: external ["wp","i18n"]
  61. var external_wp_i18n_namespaceObject = window["wp"]["i18n"];
  62. ;// CONCATENATED MODULE: ./node_modules/@wordpress/media-utils/build-module/components/media-upload/index.js
  63. /**
  64. * External dependencies
  65. */
  66. /**
  67. * WordPress dependencies
  68. */
  69. const {
  70. wp
  71. } = window;
  72. const DEFAULT_EMPTY_GALLERY = [];
  73. /**
  74. * Prepares the Featured Image toolbars and frames.
  75. *
  76. * @return {wp.media.view.MediaFrame.Select} The default media workflow.
  77. */
  78. const getFeaturedImageMediaFrame = () => {
  79. return wp.media.view.MediaFrame.Select.extend({
  80. /**
  81. * Enables the Set Featured Image Button.
  82. *
  83. * @param {Object} toolbar toolbar for featured image state
  84. * @return {void}
  85. */
  86. featuredImageToolbar(toolbar) {
  87. this.createSelectToolbar(toolbar, {
  88. text: wp.media.view.l10n.setFeaturedImage,
  89. state: this.options.state
  90. });
  91. },
  92. /**
  93. * Handle the edit state requirements of selected media item.
  94. *
  95. * @return {void}
  96. */
  97. editState() {
  98. const selection = this.state('featured-image').get('selection');
  99. const view = new wp.media.view.EditImage({
  100. model: selection.single(),
  101. controller: this
  102. }).render(); // Set the view to the EditImage frame using the selected image.
  103. this.content.set(view); // After bringing in the frame, load the actual editor via an ajax call.
  104. view.loadEditor();
  105. },
  106. /**
  107. * Create the default states.
  108. *
  109. * @return {void}
  110. */
  111. createStates: function createStates() {
  112. this.on('toolbar:create:featured-image', this.featuredImageToolbar, this);
  113. this.on('content:render:edit-image', this.editState, this);
  114. this.states.add([new wp.media.controller.FeaturedImage(), new wp.media.controller.EditImage({
  115. model: this.options.editImage
  116. })]);
  117. }
  118. });
  119. };
  120. /**
  121. * Prepares the Gallery toolbars and frames.
  122. *
  123. * @return {wp.media.view.MediaFrame.Post} The default media workflow.
  124. */
  125. const getGalleryDetailsMediaFrame = () => {
  126. /**
  127. * Custom gallery details frame.
  128. *
  129. * @see https://github.com/xwp/wp-core-media-widgets/blob/905edbccfc2a623b73a93dac803c5335519d7837/wp-admin/js/widgets/media-gallery-widget.js
  130. * @class GalleryDetailsMediaFrame
  131. * @class
  132. */
  133. return wp.media.view.MediaFrame.Post.extend({
  134. /**
  135. * Set up gallery toolbar.
  136. *
  137. * @return {void}
  138. */
  139. galleryToolbar() {
  140. const editing = this.state().get('editing');
  141. this.toolbar.set(new wp.media.view.Toolbar({
  142. controller: this,
  143. items: {
  144. insert: {
  145. style: 'primary',
  146. text: editing ? wp.media.view.l10n.updateGallery : wp.media.view.l10n.insertGallery,
  147. priority: 80,
  148. requires: {
  149. library: true
  150. },
  151. /**
  152. * @fires wp.media.controller.State#update
  153. */
  154. click() {
  155. const controller = this.controller,
  156. state = controller.state();
  157. controller.close();
  158. state.trigger('update', state.get('library')); // Restore and reset the default state.
  159. controller.setState(controller.options.state);
  160. controller.reset();
  161. }
  162. }
  163. }
  164. }));
  165. },
  166. /**
  167. * Handle the edit state requirements of selected media item.
  168. *
  169. * @return {void}
  170. */
  171. editState() {
  172. const selection = this.state('gallery').get('selection');
  173. const view = new wp.media.view.EditImage({
  174. model: selection.single(),
  175. controller: this
  176. }).render(); // Set the view to the EditImage frame using the selected image.
  177. this.content.set(view); // After bringing in the frame, load the actual editor via an ajax call.
  178. view.loadEditor();
  179. },
  180. /**
  181. * Create the default states.
  182. *
  183. * @return {void}
  184. */
  185. createStates: function createStates() {
  186. this.on('toolbar:create:main-gallery', this.galleryToolbar, this);
  187. this.on('content:render:edit-image', this.editState, this);
  188. this.states.add([new wp.media.controller.Library({
  189. id: 'gallery',
  190. title: wp.media.view.l10n.createGalleryTitle,
  191. priority: 40,
  192. toolbar: 'main-gallery',
  193. filterable: 'uploaded',
  194. multiple: 'add',
  195. editable: false,
  196. library: wp.media.query((0,external_lodash_namespaceObject.defaults)({
  197. type: 'image'
  198. }, this.options.library))
  199. }), new wp.media.controller.EditImage({
  200. model: this.options.editImage
  201. }), new wp.media.controller.GalleryEdit({
  202. library: this.options.selection,
  203. editing: this.options.editing,
  204. menu: 'gallery',
  205. displaySettings: false,
  206. multiple: true
  207. }), new wp.media.controller.GalleryAdd()]);
  208. }
  209. });
  210. }; // The media library image object contains numerous attributes
  211. // we only need this set to display the image in the library.
  212. const slimImageObject = img => {
  213. const attrSet = ['sizes', 'mime', 'type', 'subtype', 'id', 'url', 'alt', 'link', 'caption'];
  214. return (0,external_lodash_namespaceObject.pick)(img, attrSet);
  215. };
  216. const getAttachmentsCollection = ids => {
  217. return wp.media.query({
  218. order: 'ASC',
  219. orderby: 'post__in',
  220. post__in: ids,
  221. posts_per_page: -1,
  222. query: true,
  223. type: 'image'
  224. });
  225. };
  226. class MediaUpload extends external_wp_element_namespaceObject.Component {
  227. constructor(_ref) {
  228. let {
  229. allowedTypes,
  230. gallery = false,
  231. unstableFeaturedImageFlow = false,
  232. modalClass,
  233. multiple = false,
  234. title = (0,external_wp_i18n_namespaceObject.__)('Select or Upload Media')
  235. } = _ref;
  236. super(...arguments);
  237. this.openModal = this.openModal.bind(this);
  238. this.onOpen = this.onOpen.bind(this);
  239. this.onSelect = this.onSelect.bind(this);
  240. this.onUpdate = this.onUpdate.bind(this);
  241. this.onClose = this.onClose.bind(this);
  242. if (gallery) {
  243. this.buildAndSetGalleryFrame();
  244. } else {
  245. const frameConfig = {
  246. title,
  247. multiple
  248. };
  249. if (!!allowedTypes) {
  250. frameConfig.library = {
  251. type: allowedTypes
  252. };
  253. }
  254. this.frame = wp.media(frameConfig);
  255. }
  256. if (modalClass) {
  257. this.frame.$el.addClass(modalClass);
  258. }
  259. if (unstableFeaturedImageFlow) {
  260. this.buildAndSetFeatureImageFrame();
  261. }
  262. this.initializeListeners();
  263. }
  264. initializeListeners() {
  265. // When an image is selected in the media frame...
  266. this.frame.on('select', this.onSelect);
  267. this.frame.on('update', this.onUpdate);
  268. this.frame.on('open', this.onOpen);
  269. this.frame.on('close', this.onClose);
  270. }
  271. /**
  272. * Sets the Gallery frame and initializes listeners.
  273. *
  274. * @return {void}
  275. */
  276. buildAndSetGalleryFrame() {
  277. const {
  278. addToGallery = false,
  279. allowedTypes,
  280. multiple = false,
  281. value = DEFAULT_EMPTY_GALLERY
  282. } = this.props; // If the value did not changed there is no need to rebuild the frame,
  283. // we can continue to use the existing one.
  284. if (value === this.lastGalleryValue) {
  285. return;
  286. }
  287. this.lastGalleryValue = value; // If a frame already existed remove it.
  288. if (this.frame) {
  289. this.frame.remove();
  290. }
  291. let currentState;
  292. if (addToGallery) {
  293. currentState = 'gallery-library';
  294. } else {
  295. currentState = value && value.length ? 'gallery-edit' : 'gallery';
  296. }
  297. if (!this.GalleryDetailsMediaFrame) {
  298. this.GalleryDetailsMediaFrame = getGalleryDetailsMediaFrame();
  299. }
  300. const attachments = getAttachmentsCollection(value);
  301. const selection = new wp.media.model.Selection(attachments.models, {
  302. props: attachments.props.toJSON(),
  303. multiple
  304. });
  305. this.frame = new this.GalleryDetailsMediaFrame({
  306. mimeType: allowedTypes,
  307. state: currentState,
  308. multiple,
  309. selection,
  310. editing: value && value.length ? true : false
  311. });
  312. wp.media.frame = this.frame;
  313. this.initializeListeners();
  314. }
  315. /**
  316. * Initializes the Media Library requirements for the featured image flow.
  317. *
  318. * @return {void}
  319. */
  320. buildAndSetFeatureImageFrame() {
  321. const featuredImageFrame = getFeaturedImageMediaFrame();
  322. const attachments = getAttachmentsCollection(this.props.value);
  323. const selection = new wp.media.model.Selection(attachments.models, {
  324. props: attachments.props.toJSON()
  325. });
  326. this.frame = new featuredImageFrame({
  327. mimeType: this.props.allowedTypes,
  328. state: 'featured-image',
  329. multiple: this.props.multiple,
  330. selection,
  331. editing: this.props.value ? true : false
  332. });
  333. wp.media.frame = this.frame;
  334. }
  335. componentWillUnmount() {
  336. this.frame.remove();
  337. }
  338. onUpdate(selections) {
  339. const {
  340. onSelect,
  341. multiple = false
  342. } = this.props;
  343. const state = this.frame.state();
  344. const selectedImages = selections || state.get('selection');
  345. if (!selectedImages || !selectedImages.models.length) {
  346. return;
  347. }
  348. if (multiple) {
  349. onSelect(selectedImages.models.map(model => slimImageObject(model.toJSON())));
  350. } else {
  351. onSelect(slimImageObject(selectedImages.models[0].toJSON()));
  352. }
  353. }
  354. onSelect() {
  355. const {
  356. onSelect,
  357. multiple = false
  358. } = this.props; // Get media attachment details from the frame state.
  359. const attachment = this.frame.state().get('selection').toJSON();
  360. onSelect(multiple ? attachment : attachment[0]);
  361. }
  362. onOpen() {
  363. var _this$props$value;
  364. this.updateCollection(); // Handle both this.props.value being either (number[]) multiple ids
  365. // (for galleries) or a (number) singular id (e.g. image block).
  366. const hasMedia = Array.isArray(this.props.value) ? !!((_this$props$value = this.props.value) !== null && _this$props$value !== void 0 && _this$props$value.length) : !!this.props.value;
  367. if (!hasMedia) {
  368. return;
  369. }
  370. const isGallery = this.props.gallery;
  371. const selection = this.frame.state().get('selection');
  372. if (!isGallery) {
  373. (0,external_lodash_namespaceObject.castArray)(this.props.value).forEach(id => {
  374. selection.add(wp.media.attachment(id));
  375. });
  376. } // Load the images so they are available in the media modal.
  377. const attachments = getAttachmentsCollection((0,external_lodash_namespaceObject.castArray)(this.props.value)); // Once attachments are loaded, set the current selection.
  378. attachments.more().done(function () {
  379. var _attachments$models;
  380. if (isGallery && attachments !== null && attachments !== void 0 && (_attachments$models = attachments.models) !== null && _attachments$models !== void 0 && _attachments$models.length) {
  381. selection.add(attachments.models);
  382. }
  383. });
  384. }
  385. onClose() {
  386. const {
  387. onClose
  388. } = this.props;
  389. if (onClose) {
  390. onClose();
  391. }
  392. }
  393. updateCollection() {
  394. const frameContent = this.frame.content.get();
  395. if (frameContent && frameContent.collection) {
  396. const collection = frameContent.collection; // Clean all attachments we have in memory.
  397. collection.toArray().forEach(model => model.trigger('destroy', model)); // Reset has more flag, if library had small amount of items all items may have been loaded before.
  398. collection.mirroring._hasMore = true; // Request items.
  399. collection.more();
  400. }
  401. }
  402. openModal() {
  403. if (this.props.gallery) {
  404. this.buildAndSetGalleryFrame();
  405. }
  406. this.frame.open();
  407. }
  408. render() {
  409. return this.props.render({
  410. open: this.openModal
  411. });
  412. }
  413. }
  414. /* harmony default export */ var media_upload = (MediaUpload);
  415. ;// CONCATENATED MODULE: ./node_modules/@wordpress/media-utils/build-module/components/index.js
  416. ;// CONCATENATED MODULE: external ["wp","apiFetch"]
  417. var external_wp_apiFetch_namespaceObject = window["wp"]["apiFetch"];
  418. var external_wp_apiFetch_default = /*#__PURE__*/__webpack_require__.n(external_wp_apiFetch_namespaceObject);
  419. ;// CONCATENATED MODULE: external ["wp","blob"]
  420. var external_wp_blob_namespaceObject = window["wp"]["blob"];
  421. ;// CONCATENATED MODULE: ./node_modules/@wordpress/media-utils/build-module/utils/upload-media.js
  422. /**
  423. * External dependencies
  424. */
  425. /**
  426. * WordPress dependencies
  427. */
  428. /**
  429. * Browsers may use unexpected mime types, and they differ from browser to browser.
  430. * This function computes a flexible array of mime types from the mime type structured provided by the server.
  431. * Converts { jpg|jpeg|jpe: "image/jpeg" } into [ "image/jpeg", "image/jpg", "image/jpeg", "image/jpe" ]
  432. * The computation of this array instead of directly using the object,
  433. * solves the problem in chrome where mp3 files have audio/mp3 as mime type instead of audio/mpeg.
  434. * https://bugs.chromium.org/p/chromium/issues/detail?id=227004
  435. *
  436. * @param {?Object} wpMimeTypesObject Mime type object received from the server.
  437. * Extensions are keys separated by '|' and values are mime types associated with an extension.
  438. *
  439. * @return {?Array} An array of mime types or the parameter passed if it was "falsy".
  440. */
  441. function getMimeTypesArray(wpMimeTypesObject) {
  442. if (!wpMimeTypesObject) {
  443. return wpMimeTypesObject;
  444. }
  445. return (0,external_lodash_namespaceObject.flatMap)(wpMimeTypesObject, (mime, extensionsString) => {
  446. const [type] = mime.split('/');
  447. const extensions = extensionsString.split('|');
  448. return [mime, ...(0,external_lodash_namespaceObject.map)(extensions, extension => `${type}/${extension}`)];
  449. });
  450. }
  451. /**
  452. * Media Upload is used by audio, image, gallery, video, and file blocks to
  453. * handle uploading a media file when a file upload button is activated.
  454. *
  455. * TODO: future enhancement to add an upload indicator.
  456. *
  457. * @param {Object} $0 Parameters object passed to the function.
  458. * @param {?Array} $0.allowedTypes Array with the types of media that can be uploaded, if unset all types are allowed.
  459. * @param {?Object} $0.additionalData Additional data to include in the request.
  460. * @param {Array} $0.filesList List of files.
  461. * @param {?number} $0.maxUploadFileSize Maximum upload size in bytes allowed for the site.
  462. * @param {Function} $0.onError Function called when an error happens.
  463. * @param {Function} $0.onFileChange Function called each time a file or a temporary representation of the file is available.
  464. * @param {?Object} $0.wpAllowedMimeTypes List of allowed mime types and file extensions.
  465. */
  466. async function uploadMedia(_ref) {
  467. let {
  468. allowedTypes,
  469. additionalData = {},
  470. filesList,
  471. maxUploadFileSize,
  472. onError = external_lodash_namespaceObject.noop,
  473. onFileChange,
  474. wpAllowedMimeTypes = null
  475. } = _ref;
  476. // Cast filesList to array.
  477. const files = [...filesList];
  478. const filesSet = [];
  479. const setAndUpdateFiles = (idx, value) => {
  480. (0,external_wp_blob_namespaceObject.revokeBlobURL)((0,external_lodash_namespaceObject.get)(filesSet, [idx, 'url']));
  481. filesSet[idx] = value;
  482. onFileChange((0,external_lodash_namespaceObject.compact)(filesSet));
  483. }; // Allowed type specified by consumer.
  484. const isAllowedType = fileType => {
  485. if (!allowedTypes) {
  486. return true;
  487. }
  488. return (0,external_lodash_namespaceObject.some)(allowedTypes, allowedType => {
  489. // If a complete mimetype is specified verify if it matches exactly the mime type of the file.
  490. if ((0,external_lodash_namespaceObject.includes)(allowedType, '/')) {
  491. return allowedType === fileType;
  492. } // Otherwise a general mime type is used and we should verify if the file mimetype starts with it.
  493. return (0,external_lodash_namespaceObject.startsWith)(fileType, `${allowedType}/`);
  494. });
  495. }; // Allowed types for the current WP_User.
  496. const allowedMimeTypesForUser = getMimeTypesArray(wpAllowedMimeTypes);
  497. const isAllowedMimeTypeForUser = fileType => {
  498. return (0,external_lodash_namespaceObject.includes)(allowedMimeTypesForUser, fileType);
  499. }; // Build the error message including the filename.
  500. const triggerError = error => {
  501. error.message = [(0,external_wp_element_namespaceObject.createElement)("strong", {
  502. key: "filename"
  503. }, error.file.name), ': ', error.message];
  504. onError(error);
  505. };
  506. const validFiles = [];
  507. for (const mediaFile of files) {
  508. // Verify if user is allowed to upload this mime type.
  509. // Defer to the server when type not detected.
  510. if (allowedMimeTypesForUser && mediaFile.type && !isAllowedMimeTypeForUser(mediaFile.type)) {
  511. triggerError({
  512. code: 'MIME_TYPE_NOT_ALLOWED_FOR_USER',
  513. message: (0,external_wp_i18n_namespaceObject.__)('Sorry, you are not allowed to upload this file type.'),
  514. file: mediaFile
  515. });
  516. continue;
  517. } // Check if the block supports this mime type.
  518. // Defer to the server when type not detected.
  519. if (mediaFile.type && !isAllowedType(mediaFile.type)) {
  520. triggerError({
  521. code: 'MIME_TYPE_NOT_SUPPORTED',
  522. message: (0,external_wp_i18n_namespaceObject.__)('Sorry, this file type is not supported here.'),
  523. file: mediaFile
  524. });
  525. continue;
  526. } // Verify if file is greater than the maximum file upload size allowed for the site.
  527. if (maxUploadFileSize && mediaFile.size > maxUploadFileSize) {
  528. triggerError({
  529. code: 'SIZE_ABOVE_LIMIT',
  530. message: (0,external_wp_i18n_namespaceObject.__)('This file exceeds the maximum upload size for this site.'),
  531. file: mediaFile
  532. });
  533. continue;
  534. } // Don't allow empty files to be uploaded.
  535. if (mediaFile.size <= 0) {
  536. triggerError({
  537. code: 'EMPTY_FILE',
  538. message: (0,external_wp_i18n_namespaceObject.__)('This file is empty.'),
  539. file: mediaFile
  540. });
  541. continue;
  542. }
  543. validFiles.push(mediaFile); // Set temporary URL to create placeholder media file, this is replaced
  544. // with final file from media gallery when upload is `done` below.
  545. filesSet.push({
  546. url: (0,external_wp_blob_namespaceObject.createBlobURL)(mediaFile)
  547. });
  548. onFileChange(filesSet);
  549. }
  550. for (let idx = 0; idx < validFiles.length; ++idx) {
  551. const mediaFile = validFiles[idx];
  552. try {
  553. const savedMedia = await createMediaFromFile(mediaFile, additionalData);
  554. const mediaObject = { ...(0,external_lodash_namespaceObject.omit)(savedMedia, ['alt_text', 'source_url']),
  555. alt: savedMedia.alt_text,
  556. caption: (0,external_lodash_namespaceObject.get)(savedMedia, ['caption', 'raw'], ''),
  557. title: savedMedia.title.raw,
  558. url: savedMedia.source_url
  559. };
  560. setAndUpdateFiles(idx, mediaObject);
  561. } catch (error) {
  562. // Reset to empty on failure.
  563. setAndUpdateFiles(idx, null);
  564. let message;
  565. if ((0,external_lodash_namespaceObject.has)(error, ['message'])) {
  566. message = (0,external_lodash_namespaceObject.get)(error, ['message']);
  567. } else {
  568. message = (0,external_wp_i18n_namespaceObject.sprintf)( // translators: %s: file name
  569. (0,external_wp_i18n_namespaceObject.__)('Error while uploading file %s to the media library.'), mediaFile.name);
  570. }
  571. onError({
  572. code: 'GENERAL',
  573. message,
  574. file: mediaFile
  575. });
  576. }
  577. }
  578. }
  579. /**
  580. * @param {File} file Media File to Save.
  581. * @param {?Object} additionalData Additional data to include in the request.
  582. *
  583. * @return {Promise} Media Object Promise.
  584. */
  585. function createMediaFromFile(file, additionalData) {
  586. // Create upload payload.
  587. const data = new window.FormData();
  588. data.append('file', file, file.name || file.type.replace('/', '.'));
  589. (0,external_lodash_namespaceObject.forEach)(additionalData, (value, key) => data.append(key, value));
  590. return external_wp_apiFetch_default()({
  591. path: '/wp/v2/media',
  592. body: data,
  593. method: 'POST'
  594. });
  595. }
  596. ;// CONCATENATED MODULE: ./node_modules/@wordpress/media-utils/build-module/utils/index.js
  597. ;// CONCATENATED MODULE: ./node_modules/@wordpress/media-utils/build-module/index.js
  598. (window.wp = window.wp || {}).mediaUtils = __webpack_exports__;
  599. /******/ })()
  600. ;