add missing await and improve error handling

This commit is contained in:
Abhinav 2023-02-21 19:58:30 +05:30
parent da2b7ecfa4
commit e65ff3635d
2 changed files with 77 additions and 61 deletions

View file

@ -104,7 +104,7 @@ export default function ExportModal(props: Props) {
failed: exportInfo?.failedFiles?.length ?? 0,
});
if (exportInfo?.stage === ExportStage.INPROGRESS) {
resumeExport();
await resumeExport();
}
} catch (e) {
logError(
@ -113,7 +113,7 @@ export default function ExportModal(props: Props) {
);
}
};
main();
void main();
}, [exportFolder]);
useEffect(() => {
@ -133,7 +133,7 @@ export default function ExportModal(props: Props) {
const failedFilesCnt = exportRecord.failedFiles?.length;
const syncedFilesCnt = userPersonalFiles.length;
if (syncedFilesCnt > exportedFileCnt + failedFilesCnt) {
updateExportProgress({
await updateExportProgress({
current: exportedFileCnt + failedFilesCnt,
total: syncedFilesCnt,
});
@ -147,11 +147,11 @@ export default function ExportModal(props: Props) {
getExportRecordFileUID(file)
)
);
exportService.addFilesQueuedRecord(
await exportService.addFilesQueuedRecord(
exportFolder,
unExportedFiles
);
updateExportStage(ExportStage.PAUSED);
await updateExportStage(ExportStage.PAUSED);
}
} catch (e) {
setExportStage(ExportStage.INIT);
@ -159,7 +159,7 @@ export default function ExportModal(props: Props) {
}
}
};
main();
void main();
}, [props.show]);
useEffect(() => {
@ -174,19 +174,21 @@ export default function ExportModal(props: Props) {
setData(LS_KEYS.EXPORT, { folder: newFolder });
};
const updateExportStage = (newStage: ExportStage) => {
const updateExportStage = async (newStage: ExportStage) => {
setExportStage(newStage);
exportService.updateExportRecord({ stage: newStage });
await exportService.updateExportRecord({ stage: newStage });
};
const updateExportTime = (newTime: number) => {
const updateExportTime = async (newTime: number) => {
setLastExportTime(newTime);
exportService.updateExportRecord({ lastAttemptTimestamp: newTime });
await exportService.updateExportRecord({
lastAttemptTimestamp: newTime,
});
};
const updateExportProgress = (newProgress: ExportProgress) => {
const updateExportProgress = async (newProgress: ExportProgress) => {
setExportProgress(newProgress);
exportService.updateExportRecord({ progress: newProgress });
await exportService.updateExportRecord({ progress: newProgress });
};
// ======================
@ -198,16 +200,16 @@ export default function ExportModal(props: Props) {
if (!exportFolder) {
await selectExportDirectory();
}
updateExportStage(ExportStage.INPROGRESS);
await updateExportStage(ExportStage.INPROGRESS);
await sleep(100);
};
const postExportRun = async (exportResult?: { paused?: boolean }) => {
if (!exportResult?.paused) {
updateExportStage(ExportStage.FINISHED);
await updateExportStage(ExportStage.FINISHED);
await sleep(100);
updateExportTime(Date.now());
syncExportStatsWithRecord();
await updateExportTime(Date.now());
await syncExportStatsWithRecord();
}
};
@ -236,7 +238,7 @@ export default function ExportModal(props: Props) {
const startExport = async () => {
try {
await preExportRun();
updateExportProgress({ current: 0, total: 0 });
await updateExportProgress({ current: 0, total: 0 });
const exportResult = await exportService.exportFiles(
ExportType.NEW
);
@ -251,7 +253,7 @@ export default function ExportModal(props: Props) {
const stopExport = async () => {
try {
exportService.stopRunningExport();
postExportRun();
await postExportRun();
} catch (e) {
if (e.message !== CustomError.REQUEST_CANCELLED) {
logError(e, 'stopExport failed');
@ -259,11 +261,11 @@ export default function ExportModal(props: Props) {
}
};
const pauseExport = () => {
const pauseExport = async () => {
try {
updateExportStage(ExportStage.PAUSED);
await updateExportStage(ExportStage.PAUSED);
exportService.pauseRunningExport();
postExportRun({ paused: true });
await postExportRun({ paused: true });
} catch (e) {
if (e.message !== CustomError.REQUEST_CANCELLED) {
logError(e, 'pauseExport failed');
@ -300,7 +302,10 @@ export default function ExportModal(props: Props) {
const retryFailedExport = async () => {
try {
await preExportRun();
updateExportProgress({ current: 0, total: exportStats.failed });
await updateExportProgress({
current: 0,
total: exportStats.failed,
});
const exportResult = await exportService.exportFiles(
ExportType.RETRY_FAILED

View file

@ -208,7 +208,7 @@ class ExportService {
}
this.stopExport = false;
this.pauseExport = false;
this.addFilesQueuedRecord(exportDir, files);
await this.addFilesQueuedRecord(exportDir, files);
const failedFileCount = 0;
this.electronAPIs.showOnTray({
@ -241,16 +241,12 @@ class ExportService {
RecordType.SUCCESS
);
} catch (e) {
logError(e, 'failed to export file');
await this.addFileExportedRecord(
exportDir,
file,
RecordType.FAILED
);
logError(
e,
'download and save failed for file during export'
);
}
this.electronAPIs.showOnTray({
export_progress: `${index + 1} / ${
@ -345,6 +341,7 @@ class ExportService {
);
await response.promise;
}
async updateExportRecordHelper(folder: string, newData: ExportRecord) {
try {
if (!folder) {
@ -358,6 +355,7 @@ class ExportService {
);
} catch (e) {
logError(e, 'error updating Export Record');
throw e;
}
}
@ -376,6 +374,7 @@ class ExportService {
}
} catch (e) {
logError(e, 'export Record JSON parsing failed ');
throw e;
}
}
@ -432,36 +431,45 @@ class ExportService {
}
async downloadAndSave(file: EnteFile, collectionPath: string) {
file.metadata = mergeMetadata([file])[0].metadata;
const fileSaveName = getUniqueFileSaveName(
collectionPath,
file.metadata.title,
file.id
);
let fileStream = await retryAsyncFunction(() =>
downloadManager.downloadFile(file)
);
const fileType = getFileExtension(file.metadata.title);
if (
file.pubMagicMetadata?.data.editedTime &&
(fileType === TYPE_JPEG || fileType === TYPE_JPG)
) {
const fileBlob = await new Response(fileStream).blob();
if (!this.fileReader) {
this.fileReader = new FileReader();
}
const updatedFileBlob = await updateFileCreationDateInEXIF(
this.fileReader,
fileBlob,
new Date(file.pubMagicMetadata.data.editedTime / 1000)
try {
file.metadata = mergeMetadata([file])[0].metadata;
const fileSaveName = getUniqueFileSaveName(
collectionPath,
file.metadata.title,
file.id
);
fileStream = updatedFileBlob.stream();
}
if (file.metadata.fileType === FILE_TYPE.LIVE_PHOTO) {
await this.exportMotionPhoto(fileStream, file, collectionPath);
} else {
this.saveMediaFile(collectionPath, fileSaveName, fileStream);
await this.saveMetadataFile(collectionPath, fileSaveName, file);
let fileStream = await retryAsyncFunction(() =>
downloadManager.downloadFile(file)
);
const fileType = getFileExtension(file.metadata.title);
if (
file.pubMagicMetadata?.data.editedTime &&
(fileType === TYPE_JPEG || fileType === TYPE_JPG)
) {
const fileBlob = await new Response(fileStream).blob();
if (!this.fileReader) {
this.fileReader = new FileReader();
}
const updatedFileBlob = await updateFileCreationDateInEXIF(
this.fileReader,
fileBlob,
new Date(file.pubMagicMetadata.data.editedTime / 1000)
);
fileStream = updatedFileBlob.stream();
}
if (file.metadata.fileType === FILE_TYPE.LIVE_PHOTO) {
await this.exportMotionPhoto(fileStream, file, collectionPath);
} else {
await this.saveMediaFile(
collectionPath,
fileSaveName,
fileStream
);
await this.saveMetadataFile(collectionPath, fileSaveName, file);
}
} catch (e) {
logError(e, 'download and save failed for file during export');
throw e;
}
}
@ -478,7 +486,7 @@ class ExportService {
motionPhoto.imageNameTitle,
file.id
);
this.saveMediaFile(collectionPath, imageSaveName, imageStream);
await this.saveMediaFile(collectionPath, imageSaveName, imageStream);
await this.saveMetadataFile(collectionPath, imageSaveName, file);
const videoStream = generateStreamFromArrayBuffer(motionPhoto.video);
@ -549,9 +557,12 @@ class ExportService {
collectionIDPathMap
);
await this.updateExportRecord({
version: LATEST_EXPORT_VERSION,
});
await this.updateExportRecord(
{
version: LATEST_EXPORT_VERSION,
},
exportDir
);
}
}