You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1014 B
39 lines
1014 B
"use strict";
|
|
|
|
const DATA_URL_PATTERN = /^data:([^/]+\/[^,;]+(?:[^,]*?))(;base64)?,([\s\S]*)$/;
|
|
|
|
function dataURItoBlob(dataURI, opts, toFile) {
|
|
var _ref, _opts$mimeType;
|
|
|
|
// get the base64 data
|
|
const dataURIData = DATA_URL_PATTERN.exec(dataURI); // user may provide mime type, if not get it from data URI
|
|
|
|
const mimeType = (_ref = (_opts$mimeType = opts.mimeType) != null ? _opts$mimeType : dataURIData == null ? void 0 : dataURIData[1]) != null ? _ref : 'plain/text';
|
|
let data;
|
|
|
|
if (dataURIData[2] != null) {
|
|
const binary = atob(decodeURIComponent(dataURIData[3]));
|
|
const bytes = new Uint8Array(binary.length);
|
|
|
|
for (let i = 0; i < binary.length; i++) {
|
|
bytes[i] = binary.charCodeAt(i);
|
|
}
|
|
|
|
data = [bytes];
|
|
} else {
|
|
data = [decodeURIComponent(dataURIData[3])];
|
|
} // Convert to a File?
|
|
|
|
|
|
if (toFile) {
|
|
return new File(data, opts.name || '', {
|
|
type: mimeType
|
|
});
|
|
}
|
|
|
|
return new Blob(data, {
|
|
type: mimeType
|
|
});
|
|
}
|
|
|
|
module.exports = dataURItoBlob;
|