π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Localize export suppressions, harden error handling
Manav Rathi1455a0a75f3d
diff --git a/web/apps/photos/src/components/Export.tsx b/web/apps/photos/src/components/Export.tsxindex bc339541ca..e0b79299fa 100644--- a/web/apps/photos/src/components/Export.tsx+++ b/web/apps/photos/src/components/Export.tsx@@ -1,4 +1,3 @@-/* eslint-disable @typescript-eslint/ban-ts-comment */import exportService, {CustomError,ExportStage,@@ -76,31 +75,38 @@ export const Export: React.FC<ExportProps> = ({total: 0,});const [pendingFiles, setPendingFiles] = useState<EnteFile[]>([]);- const [lastExportTime, setLastExportTime] = useState(0);-- const syncExportRecord = useCallback(async (exportFolder: string) => {- try {- if (!(await exportService.exportFolderExists(exportFolder))) {- setPendingFiles(await exportService.pendingFiles());- }- const exportRecord =- await exportService.getExportRecord(exportFolder);- const currentStage = exportService.getCurrentExportStage();- const isRunning = Boolean(exportService.isExportInProgress());- const effectiveStage =- isRunning && currentStage !== ExportStage.init- ? currentStage- : exportRecord.stage;- setExportStage(effectiveStage);- setLastExportTime(exportRecord.lastAttemptTimestamp);- setPendingFiles(await exportService.pendingFiles(exportRecord));- } catch (e) {- // @ts-ignore- if (e.message !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST) {- log.error("syncExportRecord failed", e);+ const [lastExportTime, setLastExportTime] = useState<number | null>(0);++ const syncExportRecord = useCallback(+ async (exportFolder: string | undefined) => {+ try {+ if (!(await exportService.exportFolderExists(exportFolder))) {+ setPendingFiles(await exportService.pendingFiles());+ }+ const exportRecord =+ await exportService.getExportRecord(exportFolder);+ const currentStage = exportService.getCurrentExportStage();+ const isRunning = Boolean(exportService.isExportInProgress());+ const effectiveStage =+ isRunning && currentStage !== ExportStage.init+ ? currentStage+ : exportRecord.stage;+ setExportStage(effectiveStage);+ setLastExportTime(exportRecord.lastAttemptTimestamp);+ setPendingFiles(await exportService.pendingFiles(exportRecord));+ } catch (e) {+ if (+ !(+ e instanceof Error &&+ e.message === CustomError.EXPORT_FOLDER_DOES_NOT_EXIST+ )+ ) {+ log.error("syncExportRecord failed", e);+ }}- }- }, []);+ },+ [],+ );useEffect(() => {if (!isDesktop) return;@@ -114,9 +120,6 @@ export const Export: React.FC<ExportProps> = ({const exportSettings = exportService.getExportSettings();setExportFolder(exportSettings?.folder ?? "");setContinuousExport(exportSettings?.continuousExport ?? false);- // TODO: The type of syncExportRecord is wrong. It can work with an- // undefined value, but the type prohibits that.- // @ts-ignorevoid syncExportRecord(exportSettings?.folder);}, [syncExportRecord]);@@ -461,7 +464,7 @@ const ExportInProgressDialogContent: React.FC<interface ExportFinishedDialogContentProps {pendingFiles: EnteFile[];- lastExportTime: number;+ lastExportTime: number | null;collectionNameByID: Map<number, string>;onClose: () => void;onResyncExport: () => void;diff --git a/web/apps/photos/src/services/export.ts b/web/apps/photos/src/services/export.tsindex 1bb87faa41..b9d2c4997c 100644--- a/web/apps/photos/src/services/export.ts+++ b/web/apps/photos/src/services/export.ts@@ -1,10 +1,6 @@-// TODO: Audit this file-/* eslint-disable @typescript-eslint/no-empty-function */-/* eslint-disable @typescript-eslint/no-unsafe-return */-/* eslint-disable @typescript-eslint/no-unsafe-assignment */-/* eslint-disable @typescript-eslint/ban-ts-comment */+// Export records are unvalidated disk input, so runtime guards which the+// types deem unnecessary are load-bearing./* eslint-disable @typescript-eslint/no-unnecessary-condition */-/* eslint-disable @typescript-eslint/no-floating-promises */import { safeDirectoryName, safeFileName } from "@/utils/native-fs";import {ensureElectron,@@ -47,6 +43,9 @@ export const CustomError = {EXPORT_FOLDER_DOES_NOT_EXIST: "export folder does not exist",};+const errorMessage = (e: unknown) =>+ e instanceof Error ? e.message : undefined;+const exportRecordFileName = "export_status.json";const exportDirectoryName = "Ente Photos";@@ -97,7 +96,7 @@ type FileExportNames = Record<string, string>;interface ExportRecord {version: number;stage: ExportStage;- lastAttemptTimestamp: number;+ lastAttemptTimestamp: number | null;collectionExportNames: CollectionExportNames;fileExportNames: FileExportNames;}@@ -105,7 +104,6 @@ interface ExportRecord {const NULL_EXPORT_RECORD: ExportRecord = {version: 5,stage: ExportStage.init,- // @ts-ignorelastAttemptTimestamp: null,collectionExportNames: {},fileExportNames: {},@@ -140,10 +138,10 @@ class ExportService {private exportRecordUpdater = new PromiseQueue<ExportRecord>();private continuousExportEventHandler: (() => void) | null = null;private uiUpdater: ExportUIUpdaters = {- setExportProgress: () => {},- setExportStage: () => {},- setLastExportTime: () => {},- setPendingFiles: () => {},+ setExportProgress: () => undefined,+ setExportStage: () => undefined,+ setLastExportTime: () => undefined,+ setPendingFiles: () => undefined,};private currentExportProgress: ExportProgress = {total: 0,@@ -216,7 +214,7 @@ class ExportService {}resumeExport() {- this.scheduleExport({ resync: this.resyncOnce() });+ void this.scheduleExport({ resync: this.resyncOnce() });}enableContinuousExport() {@@ -225,7 +223,7 @@ class ExportService {return;}this.continuousExportEventHandler = () => {- this.scheduleExport({ resync: this.resyncOnce() });+ void this.scheduleExport({ resync: this.resyncOnce() });};this.continuousExportEventHandler();}@@ -338,10 +336,8 @@ class ExportService {}} catch (e) {if (- // @ts-ignore- e.message !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST &&- // @ts-ignore- e.message !== CustomError.EXPORT_STOPPED+ errorMessage(e) !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST &&+ errorMessage(e) !== CustomError.EXPORT_STOPPED) {log.error("scheduleExport failed", e);}@@ -420,7 +416,9 @@ class ExportService {});};if (renamedCollections?.length > 0) {- this.updateExportStage(ExportStage.renamingCollectionFolders);+ await this.updateExportStage(+ ExportStage.renamingCollectionFolders,+ );log.info(`renaming ${renamedCollections.length} collections`);await this.collectionRenamer(exportFolder,@@ -431,7 +429,7 @@ class ExportService {}if (removedFileUIDs?.length > 0) {- this.updateExportStage(ExportStage.trashingDeletedFiles);+ await this.updateExportStage(ExportStage.trashingDeletedFiles);log.info(`trashing ${removedFileUIDs.length} files`);await this.fileTrasher(exportFolder,@@ -441,7 +439,7 @@ class ExportService {);}if (filesToExport?.length > 0) {- this.updateExportStage(ExportStage.exportingFiles);+ await this.updateExportStage(ExportStage.exportingFiles);log.info(`exporting ${filesToExport.length} files`);await this.fileExporter(filesToExport,@@ -454,7 +452,9 @@ class ExportService {);}if (deletedExportedCollections?.length > 0) {- this.updateExportStage(ExportStage.trashingDeletedCollections);+ await this.updateExportStage(+ ExportStage.trashingDeletedCollections,+ );log.info(`removing ${deletedExportedCollections.length} collections`,);@@ -466,10 +466,8 @@ class ExportService {}} catch (e) {if (- // @ts-ignore- e.message !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST &&- // @ts-ignore- e.message !== CustomError.EXPORT_STOPPED+ errorMessage(e) !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST &&+ errorMessage(e) !== CustomError.EXPORT_STOPPED) {log.error("runExport failed", e);}@@ -492,10 +490,9 @@ class ExportService {}await this.verifyExportFolderExists(exportFolder);const oldCollectionExportName =- collectionIDExportNameMap.get(collection.id);+ collectionIDExportNameMap.get(collection.id)!;const oldCollectionExportPath = joinPath(exportFolder,- // @ts-ignoreoldCollectionExportName,);const newCollectionExportName = await safeDirectoryName(@@ -528,12 +525,10 @@ class ExportService {await this.addCollectionExportedRecord(exportFolder,collection.id,- // @ts-ignoreoldCollectionExportName,);collectionIDExportNameMap.set(collection.id,- // @ts-ignoreoldCollectionExportName,);throw e;@@ -544,14 +539,11 @@ class ExportService {} catch (e) {log.error("collectionRenamer failed a collection", e);if (- // @ts-ignore- e.message ===+ errorMessage(e) ===CustomError.UPDATE_EXPORTED_RECORD_FAILED ||- // @ts-ignore- e.message ===+ errorMessage(e) ===CustomError.EXPORT_FOLDER_DOES_NOT_EXIST ||- // @ts-ignore- e.message === CustomError.EXPORT_STOPPED+ errorMessage(e) === CustomError.EXPORT_STOPPED) {throw e;}@@ -559,10 +551,8 @@ class ExportService {}} catch (e) {if (- // @ts-ignore- e.message !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST &&- // @ts-ignore- e.message !== CustomError.EXPORT_STOPPED+ errorMessage(e) !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST &&+ errorMessage(e) !== CustomError.EXPORT_STOPPED) {log.error("collectionRenamer failed", e);}@@ -595,7 +585,7 @@ class ExportService {`removing collection with id ${collectionID} from export folder`,);const collectionExportName =- collectionIDPathMap.get(collectionID);+ collectionIDPathMap.get(collectionID)!;const collectionExportedFiles = getCollectionExportedFiles(exportRecord,collectionID,@@ -607,7 +597,6 @@ class ExportService {}const collectionExportPath = joinPath(exportFolder,- // @ts-ignorecollectionExportName,);await this.removeCollectionExportedRecord(@@ -623,7 +612,6 @@ class ExportService {await this.addCollectionExportedRecord(exportFolder,collectionID,- // @ts-ignorecollectionExportName,);throw e;@@ -634,14 +622,11 @@ class ExportService {} catch (e) {log.error("collectionRemover failed a collection", e);if (- // @ts-ignore- e.message ===+ errorMessage(e) ===CustomError.UPDATE_EXPORTED_RECORD_FAILED ||- // @ts-ignore- e.message ===+ errorMessage(e) ===CustomError.EXPORT_FOLDER_DOES_NOT_EXIST ||- // @ts-ignore- e.message === CustomError.EXPORT_STOPPED+ errorMessage(e) === CustomError.EXPORT_STOPPED) {throw e;}@@ -649,10 +634,8 @@ class ExportService {}} catch (e) {if (- // @ts-ignore- e.message !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST &&- // @ts-ignore- e.message !== CustomError.EXPORT_STOPPED+ errorMessage(e) !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST &&+ errorMessage(e) !== CustomError.EXPORT_STOPPED) {log.error("collectionRemover failed", e);}@@ -725,14 +708,11 @@ class ExportService {incrementFailed();log.error(`export failed for a ${fileLogID(file)}`, e);if (- // @ts-ignore- e.message ===+ errorMessage(e) ===CustomError.UPDATE_EXPORTED_RECORD_FAILED ||- // @ts-ignore- e.message ===+ errorMessage(e) ===CustomError.EXPORT_FOLDER_DOES_NOT_EXIST ||- // @ts-ignore- e.message === CustomError.EXPORT_STOPPED+ errorMessage(e) === CustomError.EXPORT_STOPPED) {throw e;}@@ -740,10 +720,8 @@ class ExportService {}} catch (e) {if (- // @ts-ignore- e.message !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST &&- // @ts-ignore- e.message !== CustomError.EXPORT_STOPPED+ errorMessage(e) !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST &&+ errorMessage(e) !== CustomError.EXPORT_STOPPED) {log.error("fileExporter failed", e);}@@ -769,34 +747,29 @@ class ExportService {throw Error(CustomError.EXPORT_STOPPED);}try {- const fileExportName = fileIDExportNameMap.get(fileUID);+ const fileExportName = fileIDExportNameMap.get(fileUID)!;const collectionID = getCollectionIDFromFileUID(fileUID);const collectionExportName =- collectionIDExportNameMap.get(collectionID);+ collectionIDExportNameMap.get(collectionID)!;- // @ts-ignoreif (isLivePhotoExportName(fileExportName)) {const { image, video } =- // @ts-ignoreparseLivePhotoExportName(fileExportName);await moveToFSTrash(exportDir,- // @ts-ignorecollectionExportName,image,);await moveToFSTrash(exportDir,- // @ts-ignorecollectionExportName,video,);} else {await moveToFSTrash(exportDir,- // @ts-ignorecollectionExportName,fileExportName,);@@ -808,14 +781,11 @@ class ExportService {} catch (e) {log.error("trashing failed for a file", e);if (- // @ts-ignore- e.message ===+ errorMessage(e) ===CustomError.UPDATE_EXPORTED_RECORD_FAILED ||- // @ts-ignore- e.message ===+ errorMessage(e) ===CustomError.EXPORT_FOLDER_DOES_NOT_EXIST ||- // @ts-ignore- e.message === CustomError.EXPORT_STOPPED+ errorMessage(e) === CustomError.EXPORT_STOPPED) {throw e;}@@ -823,10 +793,8 @@ class ExportService {}} catch (e) {if (- // @ts-ignore- e.message !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST &&- // @ts-ignore- e.message !== CustomError.EXPORT_STOPPED+ errorMessage(e) !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST &&+ errorMessage(e) !== CustomError.EXPORT_STOPPED) {log.error("fileTrasher failed", e);}@@ -850,8 +818,7 @@ class ExportService {};await this.updateExportRecord(folder, { fileExportNames });} catch (e) {- // @ts-ignore- if (e.message !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST) {+ if (errorMessage(e) !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST) {log.error("addFileExportedRecord failed", e);}throw e;@@ -875,8 +842,7 @@ class ExportService {await this.updateExportRecord(folder, { collectionExportNames });} catch (e) {- // @ts-ignore- if (e.message !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST) {+ if (errorMessage(e) !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST) {log.error("addCollectionExportedRecord failed", e);}throw e;@@ -895,8 +861,7 @@ class ExportService {await this.updateExportRecord(folder, { collectionExportNames });} catch (e) {- // @ts-ignore- if (e.message !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST) {+ if (errorMessage(e) !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST) {log.error("removeCollectionExportedRecord failed", e);}throw e;@@ -913,8 +878,7 @@ class ExportService {);await this.updateExportRecord(folder, { fileExportNames });} catch (e) {- // @ts-ignore- if (e.message !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST) {+ if (errorMessage(e) !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST) {log.error("removeFileExportedRecord failed", e);}throw e;@@ -940,8 +904,7 @@ class ExportService {);return newRecord;} catch (e) {- // @ts-ignore- if (e.message === CustomError.EXPORT_FOLDER_DOES_NOT_EXIST) {+ if (errorMessage(e) === CustomError.EXPORT_FOLDER_DOES_NOT_EXIST) {throw e;}log.error("error updating Export Record", e);@@ -951,20 +914,23 @@ class ExportService {}}- async getExportRecord(folder: string): Promise<ExportRecord> {+ async getExportRecord(folder: string | undefined): Promise<ExportRecord> {const electron = ensureElectron();const fs = electron.fs;try {await this.verifyExportFolderExists(folder);- const exportRecordJSONPath = joinPath(folder, exportRecordFileName);+ const exportRecordJSONPath = joinPath(+ folder!,+ exportRecordFileName,+ );if (!(await fs.exists(exportRecordJSONPath))) {return await this.createEmptyExportRecord(exportRecordJSONPath);}const recordFile = await fs.readTextFile(exportRecordJSONPath);- return JSON.parse(recordFile);+ // TODO: Zod-validate instead of casting.+ return JSON.parse(recordFile) as ExportRecord;} catch (e) {- // @ts-ignore- if (e.message !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST) {+ if (errorMessage(e) !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST) {log.error("export Record JSON parsing failed", e);}throw e;@@ -978,10 +944,9 @@ class ExportService {) {const fs = ensureElectron().fs;await this.verifyExportFolderExists(exportFolder);- const collectionName = collectionIDNameMap.get(collectionID);+ const collectionName = collectionIDNameMap.get(collectionID)!;const collectionExportName = await safeDirectoryName(exportFolder,- // @ts-ignorecollectionName,fs.exists,);@@ -1013,8 +978,7 @@ class ExportService {exportDir,fileUID,collectionExportPath,- // @ts-ignore- originalFileStream,+ originalFileStream!,file,);} else {@@ -1031,7 +995,6 @@ class ExportService {await writeStream(electron,joinPath(collectionExportPath, fileExportName),- // @ts-ignoreoriginalFileStream,);await this.addFileExportedRecord(@@ -1150,14 +1113,15 @@ class ExportService {return exportFolder && (await ensureElectron().fs.exists(exportFolder));};- private verifyExportFolderExists = async (exportFolder: string) => {+ private verifyExportFolderExists = async (+ exportFolder: string | undefined,+ ) => {try {if (!(await this.exportFolderExists(exportFolder))) {throw Error(CustomError.EXPORT_FOLDER_DOES_NOT_EXIST);}} catch (e) {- // @ts-ignore- if (e.message !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST) {+ if (errorMessage(e) !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST) {log.error("verifyExportFolderExists failed", e);}throw e;@@ -1184,8 +1148,7 @@ export const resumeExportsIfNeeded = async () => {return;}const exportRecord = await exportService.getExportRecord(- // TODO: Retain existing behaviour of code. Needs rework.- exportSettings!.folder!,+ exportSettings!.folder,);if (exportSettings!.continuousExport) {exportService.enableContinuousExport();@@ -1272,18 +1235,16 @@ const getRenamedExportedCollections = (if (collectionIDExportNameMap.has(collection.id)) {const currentExportName = collectionIDExportNameMap.get(collection.id,- );+ )!;const collectionExportName = collectionUserFacingName(collection);if (currentExportName === collectionExportName) {return false;}- // @ts-ignoreconst hasNumberedSuffix = /\(\d+\)$/.exec(currentExportName);const currentExportNameWithoutNumberedSuffix = hasNumberedSuffix- ? // @ts-ignore- currentExportName.replace(/\(\d+\)$/, "")+ ? currentExportName.replace(/\(\d+\)$/, ""): currentExportName;return (@@ -1484,10 +1445,14 @@ const isLivePhotoExportName = (exportName: string) => {}};+// TODO: Zod-validate instead of casting.const parseLivePhotoExportName = (livePhotoExportName: string,): { image: string; video: string } => {- const { image, video } = JSON.parse(livePhotoExportName);+ const { image, video } = JSON.parse(livePhotoExportName) as {+ image: string;+ video: string;+ };return { image, video };};