ai-stories-factory / src /lib /base64 /dataUriToBlob.ts
jbilcke-hf's picture
jbilcke-hf HF staff
making progress on the AI Stories Factory
6821e3e
raw
history blame contribute delete
518 Bytes
export function dataUriToBlob(dataURI = "", defaultContentType = ""): Blob {
dataURI = dataURI.replace(/^data:/, '');
const type = dataURI.match(/(?:image|application|video|audio|text)\/[^;]+/)?.[0] || defaultContentType;
const base64 = dataURI.replace(/^[^,]+,/, '');
const arrayBuffer = new ArrayBuffer(base64.length);
const typedArray = new Uint8Array(arrayBuffer);
for (let i = 0; i < base64.length; i++) {
typedArray[i] = base64.charCodeAt(i);
}
return new Blob([arrayBuffer], { type });
}