mirror of
https://github.com/makeplane/plane.git
synced 2026-05-08 00:49:36 -05:00
43 lines
1019 B
TypeScript
43 lines
1019 B
TypeScript
/**
|
|
* @description encode image via URL to base64
|
|
* @param {string} url
|
|
* @returns
|
|
*/
|
|
export const getBase64Image = async (url: string): Promise<string> => {
|
|
if (!url || typeof url !== "string") {
|
|
throw new Error("Invalid URL provided");
|
|
}
|
|
|
|
// Try to create a URL object to validate the URL
|
|
try {
|
|
new URL(url);
|
|
} catch (error) {
|
|
throw new Error("Invalid URL format");
|
|
}
|
|
|
|
const response = await fetch(url);
|
|
// check if the response is OK
|
|
if (!response.ok) {
|
|
throw new Error(`Failed to fetch image: ${response.statusText}`);
|
|
}
|
|
|
|
const blob = await response.blob();
|
|
return new Promise((resolve, reject) => {
|
|
const reader = new FileReader();
|
|
|
|
reader.onloadend = () => {
|
|
if (reader.result) {
|
|
resolve(reader.result as string);
|
|
} else {
|
|
reject(new Error("Failed to convert image to base64."));
|
|
}
|
|
};
|
|
|
|
reader.onerror = () => {
|
|
reject(new Error("Failed to read the image file."));
|
|
};
|
|
|
|
reader.readAsDataURL(blob);
|
|
});
|
|
};
|