Skip to content

Commit

Permalink
Step 20.23: Add dataURLToBlob helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamil Kisiela authored and Dotan Simha committed Nov 22, 2016
1 parent 7f5c13d commit bcd4800
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions imports/api/images/helpers.js
@@ -0,0 +1,31 @@
/**
* Converts DataURL to Blob object
*
* https://github.com/ebidel/filer.js/blob/master/src/filer.js#L137
*
* @param {String} dataURL
* @return {Blob}
*/
export function dataURLToBlob(dataURL) {
const BASE64_MARKER = ';base64,';

if (dataURL.indexOf(BASE64_MARKER) === -1) {
const parts = dataURL.split(',');
const contentType = parts[0].split(':')[1];
const raw = decodeURIComponent(parts[1]);

return new Blob([raw], {type: contentType});
}

const parts = dataURL.split(BASE64_MARKER);
const contentType = parts[0].split(':')[1];
const raw = window.atob(parts[1]);
const rawLength = raw.length;
const uInt8Array = new Uint8Array(rawLength);

for (let i = 0; i < rawLength; ++i) {
uInt8Array[i] = raw.charCodeAt(i);
}

return new Blob([uInt8Array], {type: contentType});
}

0 comments on commit bcd4800

Please sign in to comment.