From f97d5b19d9647ef0c73caeef00828e4713ffabda Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Mon, 22 Jul 2024 11:37:36 +0530 Subject: [PATCH] Inline I did try and search both in git history and on the internet if caching the FileReader itself has any performance benefits, but I didn't find anything. --- web/apps/photos/src/services/exif.ts | 11 +++++++---- web/apps/photos/src/services/export/index.ts | 4 ---- web/apps/photos/src/utils/file/blob.ts | 15 --------------- web/apps/photos/src/utils/file/index.ts | 19 +++---------------- 4 files changed, 10 insertions(+), 39 deletions(-) delete mode 100644 web/apps/photos/src/utils/file/blob.ts diff --git a/web/apps/photos/src/services/exif.ts b/web/apps/photos/src/services/exif.ts index 9ccaa0f344..c606f72517 100644 --- a/web/apps/photos/src/services/exif.ts +++ b/web/apps/photos/src/services/exif.ts @@ -334,12 +334,11 @@ export function getEXIFTime(exifData: ParsedEXIFData): number { } export async function updateFileCreationDateInEXIF( - reader: FileReader, fileBlob: Blob, updatedDate: Date, ) { try { - let imageDataURL = await convertImageToDataURL(reader, fileBlob); + let imageDataURL = await convertImageToDataURL(fileBlob); imageDataURL = "data:image/jpeg;base64" + imageDataURL.slice(imageDataURL.indexOf(",")); @@ -349,7 +348,10 @@ export async function updateFileCreationDateInEXIF( } exifObj["Exif"][piexif.ExifIFD.DateTimeOriginal] = convertToExifDateFormat(updatedDate); - + log.debug(() => [ + "updateFileCreationDateInEXIF", + { updatedDate, exifObj }, + ]); const exifBytes = piexif.dump(exifObj); const exifInsertedFile = piexif.insert(exifBytes, imageDataURL); return dataURIToBlob(exifInsertedFile); @@ -359,7 +361,8 @@ export async function updateFileCreationDateInEXIF( } } -async function convertImageToDataURL(reader: FileReader, blob: Blob) { +async function convertImageToDataURL(blob: Blob) { + const reader = new FileReader(); const dataURL = await new Promise((resolve) => { reader.onload = () => resolve(reader.result as string); reader.readAsDataURL(blob); diff --git a/web/apps/photos/src/services/export/index.ts b/web/apps/photos/src/services/export/index.ts index 394dbf8805..e30402a316 100644 --- a/web/apps/photos/src/services/export/index.ts +++ b/web/apps/photos/src/services/export/index.ts @@ -970,11 +970,7 @@ class ExportService { try { const fileUID = getExportRecordFileUID(file); const originalFileStream = await downloadManager.getFile(file); - if (!this.fileReader) { - this.fileReader = new FileReader(); - } const updatedFileStream = await getUpdatedEXIFFileForDownload( - this.fileReader, file, originalFileStream, ); diff --git a/web/apps/photos/src/utils/file/blob.ts b/web/apps/photos/src/utils/file/blob.ts deleted file mode 100644 index cb2e8c7a22..0000000000 --- a/web/apps/photos/src/utils/file/blob.ts +++ /dev/null @@ -1,15 +0,0 @@ -export const readAsDataURL = (blob) => - new Promise((resolve, reject) => { - const fileReader = new FileReader(); - fileReader.onload = () => resolve(fileReader.result as string); - fileReader.onerror = () => reject(fileReader.error); - fileReader.readAsDataURL(blob); - }); - -export const readAsText = (blob) => - new Promise((resolve, reject) => { - const fileReader = new FileReader(); - fileReader.onload = () => resolve(fileReader.result as string); - fileReader.onerror = () => reject(fileReader.error); - fileReader.readAsText(blob); - }); diff --git a/web/apps/photos/src/utils/file/index.ts b/web/apps/photos/src/utils/file/index.ts index 363361307a..630859c751 100644 --- a/web/apps/photos/src/utils/file/index.ts +++ b/web/apps/photos/src/utils/file/index.ts @@ -50,7 +50,6 @@ export enum FILE_OPS_TYPE { } export async function getUpdatedEXIFFileForDownload( - fileReader: FileReader, file: EnteFile, fileStream: ReadableStream, ): Promise> { @@ -62,7 +61,6 @@ export async function getUpdatedEXIFFileForDownload( ) { const fileBlob = await new Response(fileStream).blob(); const updatedFileBlob = await updateFileCreationDateInEXIF( - fileReader, fileBlob, new Date(file.pubMagicMetadata.data.editedTime / 1000), ); @@ -74,7 +72,6 @@ export async function getUpdatedEXIFFileForDownload( export async function downloadFile(file: EnteFile) { try { - const fileReader = new FileReader(); let fileBlob = await new Response( await DownloadManager.getFile(file), ).blob(); @@ -98,11 +95,7 @@ export async function downloadFile(file: EnteFile) { new File([fileBlob], file.metadata.title), ); fileBlob = await new Response( - await getUpdatedEXIFFileForDownload( - fileReader, - file, - fileBlob.stream(), - ), + await getUpdatedEXIFFileForDownload(file, fileBlob.stream()), ).blob(); fileBlob = new Blob([fileBlob], { type: fileType.mimeType }); const tempURL = URL.createObjectURL(fileBlob); @@ -455,13 +448,12 @@ async function downloadFilesDesktop( }, downloadPath: string, ) { - const fileReader = new FileReader(); for (const file of files) { try { if (progressBarUpdater?.isCancelled()) { return; } - await downloadFileDesktop(electron, fileReader, file, downloadPath); + await downloadFileDesktop(electron, file, downloadPath); progressBarUpdater?.increaseSuccess(); } catch (e) { log.error("download fail for file", e); @@ -472,7 +464,6 @@ async function downloadFilesDesktop( async function downloadFileDesktop( electron: Electron, - fileReader: FileReader, file: EnteFile, downloadDir: string, ) { @@ -480,11 +471,7 @@ async function downloadFileDesktop( const stream = (await DownloadManager.getFile( file, )) as ReadableStream; - const updatedStream = await getUpdatedEXIFFileForDownload( - fileReader, - file, - stream, - ); + const updatedStream = await getUpdatedEXIFFileForDownload(file, stream); if (file.metadata.fileType === FILE_TYPE.LIVE_PHOTO) { const fileBlob = await new Response(updatedStream).blob();