From 89a32d2104d8541fc92abc183e034f441a656462 Mon Sep 17 00:00:00 2001 From: Waqas Ibrahim Date: Sat, 2 Sep 2023 01:22:47 +0500 Subject: [PATCH] fix setImage cache --- src/util/image-helper.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/util/image-helper.ts b/src/util/image-helper.ts index 9bddab5..9e74370 100644 --- a/src/util/image-helper.ts +++ b/src/util/image-helper.ts @@ -8,16 +8,13 @@ const base64Image = async (file: string) => { // read binary data const bitmap = await readFile(file); // convert binary data to base64 encoded string - cache[file] = Buffer.from(bitmap).toString('base64'); - return cache[file]; + return Buffer.from(bitmap).toString('base64'); }; const downloadBase64Image = async (url: string) => { // convert to base64 encoded string const res = await axios.get(url, { responseType: 'arraybuffer' }); - // cache the result - cache[url] = Buffer.from(res.data).toString('base64'); - return cache[url]; + return Buffer.from(res.data).toString('base64'); }; // Convert url or file image to Base64 String @@ -25,11 +22,15 @@ export const imageToBase64 = async (image: string, useCache = false) => { if (useCache && cache[image]) { return cache[image]; } + + let imageBase64: string; const [, type] = (mime.lookup(image.replace(/\?.+$/, '')) || '').split('/'); if (image.startsWith('http')) { - image = await downloadBase64Image(image); + imageBase64 = await downloadBase64Image(image); } else { - image = await base64Image(image); + imageBase64 = await base64Image(image); } - return `data:image/${type};base64, ${image}`; + + cache[image] = `data:image/${type};base64, ${imageBase64}`; + return cache[image]; };