dataURItoBlob.js 1014 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. "use strict";
  2. const DATA_URL_PATTERN = /^data:([^/]+\/[^,;]+(?:[^,]*?))(;base64)?,([\s\S]*)$/;
  3. function dataURItoBlob(dataURI, opts, toFile) {
  4. var _ref, _opts$mimeType;
  5. // get the base64 data
  6. const dataURIData = DATA_URL_PATTERN.exec(dataURI); // user may provide mime type, if not get it from data URI
  7. const mimeType = (_ref = (_opts$mimeType = opts.mimeType) != null ? _opts$mimeType : dataURIData == null ? void 0 : dataURIData[1]) != null ? _ref : 'plain/text';
  8. let data;
  9. if (dataURIData[2] != null) {
  10. const binary = atob(decodeURIComponent(dataURIData[3]));
  11. const bytes = new Uint8Array(binary.length);
  12. for (let i = 0; i < binary.length; i++) {
  13. bytes[i] = binary.charCodeAt(i);
  14. }
  15. data = [bytes];
  16. } else {
  17. data = [decodeURIComponent(dataURIData[3])];
  18. } // Convert to a File?
  19. if (toFile) {
  20. return new File(data, opts.name || '', {
  21. type: mimeType
  22. });
  23. }
  24. return new Blob(data, {
  25. type: mimeType
  26. });
  27. }
  28. module.exports = dataURItoBlob;