From 572963208556d4d1f87f21d816767f80cefb3556 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Mon, 17 Oct 2022 18:39:53 +0530 Subject: [PATCH 1/3] directly pass blob instead of making url and passing then reading the url to blob again --- src/services/upload/exifService.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/services/upload/exifService.ts b/src/services/upload/exifService.ts index ee6a3c906..9cbb11fac 100644 --- a/src/services/upload/exifService.ts +++ b/src/services/upload/exifService.ts @@ -62,8 +62,7 @@ export async function updateFileCreationDateInEXIF( updatedDate: Date ) { try { - const fileURL = URL.createObjectURL(fileBlob); - let imageDataURL = await convertImageToDataURL(reader, fileURL); + let imageDataURL = await convertImageToDataURL(reader, fileBlob); imageDataURL = 'data:image/jpeg;base64' + imageDataURL.slice(imageDataURL.indexOf(',')); @@ -83,8 +82,7 @@ export async function updateFileCreationDateInEXIF( } } -async function convertImageToDataURL(reader: FileReader, url: string) { - const blob = await fetch(url).then((r) => r.blob()); +async function convertImageToDataURL(reader: FileReader, blob: Blob) { const dataURL = await new Promise((resolve) => { reader.onload = () => resolve(reader.result as string); reader.readAsDataURL(blob); From 60c218497a2eb9e97ba3e717fb04c41b645b5a38 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Mon, 17 Oct 2022 18:41:05 +0530 Subject: [PATCH 2/3] the object URL can be revoked after the image is loaded --- src/services/upload/thumbnailService.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/services/upload/thumbnailService.ts b/src/services/upload/thumbnailService.ts index c88006438..49924ff9c 100644 --- a/src/services/upload/thumbnailService.ts +++ b/src/services/upload/thumbnailService.ts @@ -117,6 +117,7 @@ export async function generateImageThumbnail(file: File, isHEIC: boolean) { await new Promise((resolve, reject) => { image.onload = () => { try { + URL.revokeObjectURL(imageURL); const imageDimension = { width: image.width, height: image.height, @@ -165,6 +166,7 @@ export async function generateVideoThumbnail(file: File) { videoURL = URL.createObjectURL(file); video.addEventListener('loadeddata', function () { try { + URL.revokeObjectURL(videoURL); if (!video) { throw Error('video load failed'); } From 312cfa3069874a4529b8f3516e372da2f89aaffe Mon Sep 17 00:00:00 2001 From: Abhinav Date: Mon, 17 Oct 2022 18:57:25 +0530 Subject: [PATCH 3/3] remove object URL after download --- src/utils/file/index.ts | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/src/utils/file/index.ts b/src/utils/file/index.ts index 82f9b9089..73972c89a 100644 --- a/src/utils/file/index.ts +++ b/src/utils/file/index.ts @@ -32,16 +32,8 @@ export function downloadAsFile(filename: string, content: string) { const file = new Blob([content], { type: 'text/plain', }); - const a = document.createElement('a'); - a.href = URL.createObjectURL(file); - a.download = filename; - - a.style.display = 'none'; - document.body.appendChild(a); - - a.click(); - - a.remove(); + const fileURL = URL.createObjectURL(file); + downloadUsingAnchor(fileURL, filename); } export async function downloadFile( @@ -116,10 +108,6 @@ export async function downloadFile( tempURL = URL.createObjectURL(fileBlob); downloadUsingAnchor(tempURL, file.metadata.title); } - - tempURL && URL.revokeObjectURL(tempURL); - tempImageURL && URL.revokeObjectURL(tempImageURL); - tempVideoURL && URL.revokeObjectURL(tempVideoURL); } function downloadUsingAnchor(link: string, name: string) { @@ -129,6 +117,7 @@ function downloadUsingAnchor(link: string, name: string) { a.download = name; document.body.appendChild(a); a.click(); + URL.revokeObjectURL(link); a.remove(); }