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.
This commit is contained in:
Manav Rathi
2024-07-22 11:37:36 +05:30
parent 1972239bb0
commit f97d5b19d9
4 changed files with 10 additions and 39 deletions

View File

@@ -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<string>((resolve) => {
reader.onload = () => resolve(reader.result as string);
reader.readAsDataURL(blob);

View File

@@ -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,
);

View File

@@ -1,15 +0,0 @@
export const readAsDataURL = (blob) =>
new Promise<string>((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<string>((resolve, reject) => {
const fileReader = new FileReader();
fileReader.onload = () => resolve(fileReader.result as string);
fileReader.onerror = () => reject(fileReader.error);
fileReader.readAsText(blob);
});

View File

@@ -50,7 +50,6 @@ export enum FILE_OPS_TYPE {
}
export async function getUpdatedEXIFFileForDownload(
fileReader: FileReader,
file: EnteFile,
fileStream: ReadableStream<Uint8Array>,
): Promise<ReadableStream<Uint8Array>> {
@@ -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<Uint8Array>;
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();