blob.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /******/ (function() { // webpackBootstrap
  2. /******/ "use strict";
  3. /******/ // The require scope
  4. /******/ var __webpack_require__ = {};
  5. /******/
  6. /************************************************************************/
  7. /******/ /* webpack/runtime/define property getters */
  8. /******/ !function() {
  9. /******/ // define getter functions for harmony exports
  10. /******/ __webpack_require__.d = function(exports, definition) {
  11. /******/ for(var key in definition) {
  12. /******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
  13. /******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
  14. /******/ }
  15. /******/ }
  16. /******/ };
  17. /******/ }();
  18. /******/
  19. /******/ /* webpack/runtime/hasOwnProperty shorthand */
  20. /******/ !function() {
  21. /******/ __webpack_require__.o = function(obj, prop) { return Object.prototype.hasOwnProperty.call(obj, prop); }
  22. /******/ }();
  23. /******/
  24. /******/ /* webpack/runtime/make namespace object */
  25. /******/ !function() {
  26. /******/ // define __esModule on exports
  27. /******/ __webpack_require__.r = function(exports) {
  28. /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
  29. /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  30. /******/ }
  31. /******/ Object.defineProperty(exports, '__esModule', { value: true });
  32. /******/ };
  33. /******/ }();
  34. /******/
  35. /************************************************************************/
  36. var __webpack_exports__ = {};
  37. __webpack_require__.r(__webpack_exports__);
  38. /* harmony export */ __webpack_require__.d(__webpack_exports__, {
  39. /* harmony export */ "createBlobURL": function() { return /* binding */ createBlobURL; },
  40. /* harmony export */ "getBlobByURL": function() { return /* binding */ getBlobByURL; },
  41. /* harmony export */ "getBlobTypeByURL": function() { return /* binding */ getBlobTypeByURL; },
  42. /* harmony export */ "isBlobURL": function() { return /* binding */ isBlobURL; },
  43. /* harmony export */ "revokeBlobURL": function() { return /* binding */ revokeBlobURL; }
  44. /* harmony export */ });
  45. /**
  46. * Browser dependencies
  47. */
  48. const {
  49. createObjectURL,
  50. revokeObjectURL
  51. } = window.URL;
  52. /**
  53. * @type {Record<string, File|undefined>}
  54. */
  55. const cache = {};
  56. /**
  57. * Create a blob URL from a file.
  58. *
  59. * @param {File} file The file to create a blob URL for.
  60. *
  61. * @return {string} The blob URL.
  62. */
  63. function createBlobURL(file) {
  64. const url = createObjectURL(file);
  65. cache[url] = file;
  66. return url;
  67. }
  68. /**
  69. * Retrieve a file based on a blob URL. The file must have been created by
  70. * `createBlobURL` and not removed by `revokeBlobURL`, otherwise it will return
  71. * `undefined`.
  72. *
  73. * @param {string} url The blob URL.
  74. *
  75. * @return {File|undefined} The file for the blob URL.
  76. */
  77. function getBlobByURL(url) {
  78. return cache[url];
  79. }
  80. /**
  81. * Retrieve a blob type based on URL. The file must have been created by
  82. * `createBlobURL` and not removed by `revokeBlobURL`, otherwise it will return
  83. * `undefined`.
  84. *
  85. * @param {string} url The blob URL.
  86. *
  87. * @return {string|undefined} The blob type.
  88. */
  89. function getBlobTypeByURL(url) {
  90. var _getBlobByURL;
  91. return (_getBlobByURL = getBlobByURL(url)) === null || _getBlobByURL === void 0 ? void 0 : _getBlobByURL.type.split('/')[0]; // 0: media type , 1: file extension eg ( type: 'image/jpeg' ).
  92. }
  93. /**
  94. * Remove the resource and file cache from memory.
  95. *
  96. * @param {string} url The blob URL.
  97. */
  98. function revokeBlobURL(url) {
  99. if (cache[url]) {
  100. revokeObjectURL(url);
  101. }
  102. delete cache[url];
  103. }
  104. /**
  105. * Check whether a url is a blob url.
  106. *
  107. * @param {string} url The URL.
  108. *
  109. * @return {boolean} Is the url a blob url?
  110. */
  111. function isBlobURL(url) {
  112. if (!url || !url.indexOf) {
  113. return false;
  114. }
  115. return url.indexOf('blob:') === 0;
  116. }
  117. (window.wp = window.wp || {}).blob = __webpack_exports__;
  118. /******/ })()
  119. ;