Skip to content

Commit

Permalink
fix: Handle invalid dataURL or InvalidCharacterException
Browse files Browse the repository at this point in the history
  • Loading branch information
surajshetty3416 committed Jan 24, 2025
1 parent 8c6a098 commit f61d240
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
12 changes: 7 additions & 5 deletions frontend/src/utils/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,14 @@ class Block implements BlockOptions {
// if src is base64, convert it to a file
const src = this.getAttribute("src") as string;
if (src && src.startsWith("data:image")) {
this.setAttribute("src", "");
options.src = "";
const file = dataURLtoFile(src, "image.png");
uploadImage(file, true).then((obj) => {
this.setAttribute("src", obj.fileURL);
});
if (file) {
this.setAttribute("src", "");
options.src = "";
uploadImage(file, true).then((obj) => {
this.setAttribute("src", obj.fileURL);
});
}
}
}
}
Expand Down
21 changes: 13 additions & 8 deletions frontend/src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,15 +383,20 @@ async function uploadImage(file: File, silent = false) {
}

function dataURLtoFile(dataurl: string, filename: string) {
let arr = dataurl.split(","),
mime = arr[0].match(/:(.*?);/)?.[1],
bstr = atob(arr[1]),
n = bstr.length,
u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
try {
let arr = dataurl.split(","),
mime = arr[0].match(/:(.*?);/)?.[1],
bstr = atob(arr[1]),
n = bstr.length,
u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new File([u8arr], filename, { type: mime });
} catch (error) {
console.error(`Failed to convert dataURL ${dataurl} to file.`);
return null;
}
return new File([u8arr], filename, { type: mime });
}

declare global {
Expand Down

0 comments on commit f61d240

Please sign in to comment.