Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Move

Manav Rathi482b175324e1
diff --git a/web/apps/photos/src/services/export/index.ts b/web/apps/photos/src/services/export/index.ts
deleted file mode 100644
index a7b6cdd9e5..0000000000
--- a/web/apps/photos/src/services/export/index.ts
+++ /dev/null
@@ -1,1516 +0,0 @@
-import { ensureElectron } from "ente-base/electron";
-import { joinPath } from "ente-base/file-name";
-import log from "ente-base/log";
-import { downloadManager } from "ente-gallery/services/download";
-import { writeStream } from "ente-gallery/utils/native-stream";
-import type { Collection } from "ente-media/collection";
-import { mergeMetadata, type EnteFile } from "ente-media/file";
-import { fileLocation, type Metadata } from "ente-media/file-metadata";
-import { FileType } from "ente-media/file-type";
-import { decodeLivePhoto } from "ente-media/live-photo";
-import {
- createCollectionNameByID,
- getCollectionUserFacingName,
-} from "ente-new/photos/services/collection";
-import { getAllLocalCollections } from "ente-new/photos/services/collections";
-import {
- exportMetadataDirectoryName,
- exportTrashDirectoryName,
-} from "ente-new/photos/services/export";
-import { getAllLocalFiles } from "ente-new/photos/services/files";
-import {
- safeDirectoryName,
- safeFileName,
-} from "ente-new/photos/utils/native-fs";
-import { CustomError } from "ente-shared/error";
-import { getData, setData } from "ente-shared/storage/localStorage";
-import { PromiseQueue } from "ente-utils/promise";
-import i18n from "i18next";
-import { migrateExport, type ExportRecord } from "./migration";
-
-/** Name of the JSON file in which we keep the state of the export. */
-const exportRecordFileName = "export_status.json";
-
-/**
- * Name of the top level directory which we create underneath the selected
- * directory when the user starts an export to the file system.
- */
-const exportDirectoryName = "Ente Photos";
-
-export const ExportStage = {
- init: 0,
- migration: 1,
- starting: 2,
- exportingFiles: 3,
- trashingDeletedFiles: 4,
- renamingCollectionFolders: 5,
- trashingDeletedCollections: 6,
- finished: 7,
-} as const;
-
-export type ExportStage = (typeof ExportStage)[keyof typeof ExportStage];
-
-export interface ExportProgress {
- success: number;
- failed: number;
- total: number;
-}
-
-export interface ExportSettings {
- folder: string;
- continuousExport: boolean;
-}
-
-export type CollectionExportNames = Record<number, string>;
-
-export type FileExportNames = Record<string, string>;
-
-export const NULL_EXPORT_RECORD: ExportRecord = {
- version: 3,
- lastAttemptTimestamp: null,
- stage: ExportStage.init,
- fileExportNames: {},
- collectionExportNames: {},
-};
-
-export interface ExportOpts {
- /**
- * If true, perform an additional on-disk check to determine which files
- * need to be exported.
- *
- * This has performance implications for huge libraries, so we only do this:
- * - For the first export after an app start
- * - If the user explicitly presses the "Resync" button.
- */
- resync?: boolean;
-}
-
-interface ExportUIUpdaters {
- setExportStage: (stage: ExportStage) => void;
- setExportProgress: (progress: ExportProgress) => void;
- setLastExportTime: (exportTime: number) => void;
- setPendingFiles: (pendingFiles: EnteFile[]) => void;
-}
-
-interface RequestCanceller {
- exec: () => void;
-}
-
-interface CancellationStatus {
- status: boolean;
-}
-
-class ExportService {
- private exportSettings: ExportSettings;
- private exportInProgress: RequestCanceller = null;
- private resync = true;
- private reRunNeeded = false;
- private exportRecordUpdater = new PromiseQueue<ExportRecord>();
- private continuousExportEventHandler: () => void;
- private uiUpdater: ExportUIUpdaters = {
- setExportProgress: () => {},
- setExportStage: () => {},
- setLastExportTime: () => {},
- setPendingFiles: () => {},
- };
- private currentExportProgress: ExportProgress = {
- total: 0,
- success: 0,
- failed: 0,
- };
- private cachedMetadataDateTimeFormatter: Intl.DateTimeFormat;
-
- getExportSettings(): ExportSettings {
- try {
- if (this.exportSettings) {
- return this.exportSettings;
- }
- const exportSettings = getData("export");
- this.exportSettings = exportSettings;
- return exportSettings;
- } catch (e) {
- log.error("getExportSettings failed", e);
- throw e;
- }
- }
-
- updateExportSettings(newData: Partial<ExportSettings>) {
- try {
- const exportSettings = this.getExportSettings();
- const newSettings = { ...exportSettings, ...newData };
- this.exportSettings = newSettings;
- setData("export", newSettings);
- } catch (e) {
- log.error("updateExportSettings failed", e);
- throw e;
- }
- }
-
- async runMigration(
- exportDir: string,
- exportRecord: ExportRecord,
- updateProgress: (progress: ExportProgress) => void,
- ) {
- try {
- log.info("running migration");
- await migrateExport(exportDir, exportRecord, updateProgress);
- log.info("migration completed");
- } catch (e) {
- log.error("migration failed", e);
- throw e;
- }
- }
-
- setUIUpdaters(uiUpdater: ExportUIUpdaters) {
- this.uiUpdater = uiUpdater;
- this.uiUpdater.setExportProgress(this.currentExportProgress);
- }
-
- private updateExportProgress(exportProgress: ExportProgress) {
- this.currentExportProgress = exportProgress;
- this.uiUpdater.setExportProgress(exportProgress);
- }
-
- private async updateExportStage(stage: ExportStage) {
- const exportFolder = this.getExportSettings()?.folder;
- await this.updateExportRecord(exportFolder, { stage });
- this.uiUpdater.setExportStage(stage);
- }
-
- private async updateLastExportTime(exportTime: number) {
- const exportFolder = this.getExportSettings()?.folder;
- await this.updateExportRecord(exportFolder, {
- lastAttemptTimestamp: exportTime,
- });
- this.uiUpdater.setLastExportTime(exportTime);
- }
-
- private resyncOnce() {
- const resync = this.resync;
- this.resync = false;
- return resync;
- }
-
- resumeExport() {
- this.scheduleExport({ resync: this.resyncOnce() });
- }
-
- enableContinuousExport() {
- if (this.continuousExportEventHandler) {
- log.warn("Continuous export already enabled");
- return;
- }
- this.continuousExportEventHandler = () => {
- this.scheduleExport({ resync: this.resyncOnce() });
- };
- this.continuousExportEventHandler();
- }
-
- disableContinuousExport() {
- if (!this.continuousExportEventHandler) {
- log.warn("Continuous export already disabled");
- return;
- }
- this.continuousExportEventHandler = null;
- }
-
- /**
- * Called when the local database of files changes.
- */
- onLocalFilesUpdated() {
- if (this.continuousExportEventHandler) {
- this.continuousExportEventHandler();
- }
- }
-
- /**
- * Return the list of files that have not yet been exported.
- *
- * @param exportRecord The export record containing information about the
- * export, including files which've been exported. If an export record is
- * not specified (e.g. if the user has not exported anything yet and we just
- * wish to show a preview of what will be exported), then the function will
- * return the list of all files that will be exported if an export were to
- * happen.
- */
- pendingFiles = async (exportRecord?: ExportRecord): Promise<EnteFile[]> => {
- return getUnExportedFiles(
- await getAllLocalFiles(),
- exportRecord,
- undefined,
- );
- };
-
- async preExport(exportFolder: string) {
- await this.verifyExportFolderExists(exportFolder);
- const exportRecord = await this.getExportRecord(exportFolder);
- await this.updateExportStage(ExportStage.migration);
- await this.runMigration(
- exportFolder,
- exportRecord,
- this.updateExportProgress.bind(this),
- );
- await this.updateExportStage(ExportStage.starting);
- }
-
- async postExport() {
- try {
- const exportFolder = this.getExportSettings()?.folder;
- if (!(await this.exportFolderExists(exportFolder))) {
- this.uiUpdater.setExportStage(ExportStage.init);
- return;
- }
- await this.updateExportStage(ExportStage.finished);
- await this.updateLastExportTime(Date.now());
- this.uiUpdater.setPendingFiles(
- await this.pendingFiles(
- await this.getExportRecord(exportFolder),
- ),
- );
- } catch (e) {
- log.error("postExport failed", e);
- }
- }
-
- async stopRunningExport() {
- try {
- log.info("user requested export cancellation");
- this.exportInProgress.exec();
- this.exportInProgress = null;
- this.reRunNeeded = false;
- await this.postExport();
- } catch (e) {
- log.error("stopRunningExport failed", e);
- }
- }
-
- scheduleExport = async (exportOpts: ExportOpts) => {
- try {
- if (this.exportInProgress) {
- log.info("export in progress, scheduling re-run");
- this.reRunNeeded = true;
- return;
- } else {
- log.info("export not in progress, starting export");
- }
-
- const isCanceled: CancellationStatus = { status: false };
- const canceller: RequestCanceller = {
- exec: () => {
- isCanceled.status = true;
- },
- };
- this.exportInProgress = canceller;
- try {
- const exportFolder = this.getExportSettings()?.folder;
- await this.preExport(exportFolder);
- log.info("export started");
- await this.runExport(exportFolder, isCanceled, exportOpts);
- log.info("export completed");
- } finally {
- if (isCanceled.status) {
- log.info("export cancellation done");
- if (!this.exportInProgress) {
- await this.postExport();
- }
- } else {
- await this.postExport();
- log.info("resetting export in progress after completion");
- this.exportInProgress = null;
- if (this.reRunNeeded) {
- this.reRunNeeded = false;
- log.info("re-running export");
- setTimeout(() => this.scheduleExport(exportOpts), 0);
- }
- }
- }
- } catch (e) {
- if (
- e.message !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST &&
- e.message !== CustomError.EXPORT_STOPPED
- ) {
- log.error("scheduleExport failed", e);
- }
- }
- };
-
- private async runExport(
- exportFolder: string,
- isCanceled: CancellationStatus,
- { resync }: ExportOpts,
- ) {
- try {
- const files = mergeMetadata(await getAllLocalFiles());
- const collections = await getAllLocalCollections();
-
- const exportRecord = await this.getExportRecord(exportFolder);
- const collectionIDExportNameMap =
- convertCollectionIDExportNameObjectToMap(
- exportRecord.collectionExportNames,
- );
- const collectionIDNameMap = createCollectionNameByID(collections);
-
- const renamedCollections = getRenamedExportedCollections(
- collections,
- exportRecord,
- );
-
- const removedFileUIDs = getDeletedExportedFiles(
- files,
- exportRecord,
- );
-
- const diskFileRecordIDs = resync
- ? await readOnDiskFileExportRecordIDs(
- files,
- collectionIDExportNameMap,
- exportFolder,
- exportRecord,
- isCanceled,
- )
- : undefined;
-
- const filesToExport = getUnExportedFiles(
- files,
- exportRecord,
- diskFileRecordIDs,
- );
-
- const deletedExportedCollections = getDeletedExportedCollections(
- collections,
- exportRecord,
- );
-
- log.info(
- `[export] files: ${files.length}, disk files: ${diskFileRecordIDs?.size ?? "<na>"}, unexported files: ${filesToExport.length}, deleted exported files: ${removedFileUIDs.length}, renamed collections: ${renamedCollections.length}, deleted collections: ${deletedExportedCollections.length}`,
- );
- let success = 0;
- let failed = 0;
- this.uiUpdater.setExportProgress({
- success: success,
- failed: failed,
- total: filesToExport.length,
- });
- const incrementSuccess = () => {
- this.updateExportProgress({
- success: ++success,
- failed: failed,
- total: filesToExport.length,
- });
- };
- const incrementFailed = () => {
- this.updateExportProgress({
- success: success,
- failed: ++failed,
- total: filesToExport.length,
- });
- };
- if (renamedCollections?.length > 0) {
- this.updateExportStage(ExportStage.renamingCollectionFolders);
- log.info(`renaming ${renamedCollections.length} collections`);
- await this.collectionRenamer(
- exportFolder,
- collectionIDExportNameMap,
- renamedCollections,
- isCanceled,
- );
- }
-
- if (removedFileUIDs?.length > 0) {
- this.updateExportStage(ExportStage.trashingDeletedFiles);
- log.info(`trashing ${removedFileUIDs.length} files`);
- await this.fileTrasher(
- exportFolder,
- collectionIDExportNameMap,
- removedFileUIDs,
- isCanceled,
- );
- }
- if (filesToExport?.length > 0) {
- this.updateExportStage(ExportStage.exportingFiles);
- log.info(`exporting ${filesToExport.length} files`);
- await this.fileExporter(
- filesToExport,
- collectionIDNameMap,
- collectionIDExportNameMap,
- exportFolder,
- incrementSuccess,
- incrementFailed,
- isCanceled,
- );
- }
- if (deletedExportedCollections?.length > 0) {
- this.updateExportStage(ExportStage.trashingDeletedCollections);
- log.info(
- `removing ${deletedExportedCollections.length} collections`,
- );
- await this.collectionRemover(
- deletedExportedCollections,
- exportFolder,
- isCanceled,
- );
- }
- } catch (e) {
- if (
- e.message !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST &&
- e.message !== CustomError.EXPORT_STOPPED
- ) {
- log.error("runExport failed", e);
- }
- throw e;
- }
- }
-
- async collectionRenamer(
- exportFolder: string,
- collectionIDExportNameMap: Map<number, string>,
- renamedCollections: Collection[],
- isCanceled: CancellationStatus,
- ) {
- const fs = ensureElectron().fs;
- try {
- for (const collection of renamedCollections) {
- try {
- if (isCanceled.status) {
- throw Error(CustomError.EXPORT_STOPPED);
- }
- await this.verifyExportFolderExists(exportFolder);
- const oldCollectionExportName =
- collectionIDExportNameMap.get(collection.id);
- const oldCollectionExportPath = joinPath(
- exportFolder,
- oldCollectionExportName,
- );
- const newCollectionExportName = await safeDirectoryName(
- exportFolder,
- getCollectionUserFacingName(collection),
- fs.exists,
- );
- log.info(
- `renaming collection with id ${collection.id} from ${oldCollectionExportName} to ${newCollectionExportName}`,
- );
- const newCollectionExportPath = joinPath(
- exportFolder,
- newCollectionExportName,
- );
- await this.addCollectionExportedRecord(
- exportFolder,
- collection.id,
- newCollectionExportName,
- );
- collectionIDExportNameMap.set(
- collection.id,
- newCollectionExportName,
- );
- try {
- await fs.rename(
- oldCollectionExportPath,
- newCollectionExportPath,
- );
- } catch (e) {
- await this.addCollectionExportedRecord(
- exportFolder,
- collection.id,
- oldCollectionExportName,
- );
- collectionIDExportNameMap.set(
- collection.id,
- oldCollectionExportName,
- );
- throw e;
- }
- log.info(
- `renaming collection with id ${collection.id} from ${oldCollectionExportName} to ${newCollectionExportName} successful`,
- );
- } catch (e) {
- log.error("collectionRenamer failed a collection", e);
- if (
- e.message ===
- CustomError.UPDATE_EXPORTED_RECORD_FAILED ||
- e.message ===
- CustomError.EXPORT_FOLDER_DOES_NOT_EXIST ||
- e.message === CustomError.EXPORT_STOPPED
- ) {
- throw e;
- }
- }
- }
- } catch (e) {
- if (
- e.message !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST &&
- e.message !== CustomError.EXPORT_STOPPED
- ) {
- log.error("collectionRenamer failed", e);
- }
- throw e;
- }
- }
-
- async collectionRemover(
- deletedExportedCollectionIDs: number[],
- exportFolder: string,
- isCanceled: CancellationStatus,
- ) {
- const fs = ensureElectron().fs;
- const rmdirIfExists = async (dirPath: string) => {
- if (await fs.exists(dirPath)) await fs.rmdir(dirPath);
- };
- try {
- const exportRecord = await this.getExportRecord(exportFolder);
- const collectionIDPathMap =
- convertCollectionIDExportNameObjectToMap(
- exportRecord.collectionExportNames,
- );
- for (const collectionID of deletedExportedCollectionIDs) {
- try {
- if (isCanceled.status) {
- throw Error(CustomError.EXPORT_STOPPED);
- }
- await this.verifyExportFolderExists(exportFolder);
- log.info(
- `removing collection with id ${collectionID} from export folder`,
- );
- const collectionExportName =
- collectionIDPathMap.get(collectionID);
- // verify that the all exported files from the collection has been removed
- const collectionExportedFiles = getCollectionExportedFiles(
- exportRecord,
- collectionID,
- );
- if (collectionExportedFiles.length > 0) {
- throw new Error(
- "collection is not empty, can't remove",
- );
- }
- const collectionExportPath = joinPath(
- exportFolder,
- collectionExportName,
- );
- await this.removeCollectionExportedRecord(
- exportFolder,
- collectionID,
- );
- try {
- // delete the collection metadata folder
- await rmdirIfExists(
- getMetadataFolderExportPath(collectionExportPath),
- );
- // delete the collection folder
- await rmdirIfExists(collectionExportPath);
- } catch (e) {
- await this.addCollectionExportedRecord(
- exportFolder,
- collectionID,
- collectionExportName,
- );
- throw e;
- }
- log.info(
- `removing collection with id ${collectionID} from export folder successful`,
- );
- } catch (e) {
- log.error("collectionRemover failed a collection", e);
- if (
- e.message ===
- CustomError.UPDATE_EXPORTED_RECORD_FAILED ||
- e.message ===
- CustomError.EXPORT_FOLDER_DOES_NOT_EXIST ||
- e.message === CustomError.EXPORT_STOPPED
- ) {
- throw e;
- }
- }
- }
- } catch (e) {
- if (
- e.message !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST &&
- e.message !== CustomError.EXPORT_STOPPED
- ) {
- log.error("collectionRemover failed", e);
- }
- throw e;
- }
- }
-
- async fileExporter(
- files: EnteFile[],
- collectionIDNameMap: Map<number, string>,
- collectionIDFolderNameMap: Map<number, string>,
- exportDir: string,
- incrementSuccess: () => void,
- incrementFailed: () => void,
- isCanceled: CancellationStatus,
- ): Promise<void> {
- const fs = ensureElectron().fs;
- try {
- for (const file of files) {
- log.info(
- `exporting file ${file.metadata.title} with id ${
- file.id
- } from collection ${collectionIDNameMap.get(
- file.collectionID,
- )}`,
- );
- if (isCanceled.status) {
- throw Error(CustomError.EXPORT_STOPPED);
- }
- try {
- await this.verifyExportFolderExists(exportDir);
- let collectionExportName = collectionIDFolderNameMap.get(
- file.collectionID,
- );
- if (!collectionExportName) {
- collectionExportName =
- await this.createNewCollectionExport(
- exportDir,
- file.collectionID,
- collectionIDNameMap,
- );
- await this.addCollectionExportedRecord(
- exportDir,
- file.collectionID,
- collectionExportName,
- );
- collectionIDFolderNameMap.set(
- file.collectionID,
- collectionExportName,
- );
- }
- const collectionExportPath = joinPath(
- exportDir,
- collectionExportName,
- );
- await fs.mkdirIfNeeded(collectionExportPath);
- await fs.mkdirIfNeeded(
- getMetadataFolderExportPath(collectionExportPath),
- );
- await this.downloadAndSave(
- exportDir,
- collectionExportPath,
- file,
- );
- incrementSuccess();
- log.info(
- `exporting file ${file.metadata.title} with id ${
- file.id
- } from collection ${collectionIDNameMap.get(
- file.collectionID,
- )} successful`,
- );
- } catch (e) {
- incrementFailed();
- log.error("export failed for a file", e);
- if (
- e.message ===
- CustomError.UPDATE_EXPORTED_RECORD_FAILED ||
- e.message ===
- CustomError.EXPORT_FOLDER_DOES_NOT_EXIST ||
- e.message === CustomError.EXPORT_STOPPED
- ) {
- throw e;
- }
- }
- }
- } catch (e) {
- if (
- e.message !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST &&
- e.message !== CustomError.EXPORT_STOPPED
- ) {
- log.error("fileExporter failed", e);
- }
- throw e;
- }
- }
-
- async fileTrasher(
- exportDir: string,
- collectionIDExportNameMap: Map<number, string>,
- removedFileUIDs: string[],
- isCanceled: CancellationStatus,
- ): Promise<void> {
- try {
- const exportRecord = await this.getExportRecord(exportDir);
- const fileIDExportNameMap = convertFileIDExportNameObjectToMap(
- exportRecord.fileExportNames,
- );
- for (const fileUID of removedFileUIDs) {
- await this.verifyExportFolderExists(exportDir);
- log.info(`trashing file with id ${fileUID}`);
- if (isCanceled.status) {
- throw Error(CustomError.EXPORT_STOPPED);
- }
- try {
- const fileExportName = fileIDExportNameMap.get(fileUID);
- const collectionID = getCollectionIDFromFileUID(fileUID);
- const collectionExportName =
- collectionIDExportNameMap.get(collectionID);
-
- if (isLivePhotoExportName(fileExportName)) {
- const { image, video } =
- parseLivePhotoExportName(fileExportName);
-
- await moveToFSTrash(
- exportDir,
- collectionExportName,
- image,
- );
-
- await moveToFSTrash(
- exportDir,
- collectionExportName,
- video,
- );
- } else {
- await moveToFSTrash(
- exportDir,
- collectionExportName,
- fileExportName,
- );
- }
-
- await this.removeFileExportedRecord(exportDir, fileUID);
-
- log.info(`Moved file id ${fileUID} to Trash`);
- } catch (e) {
- log.error("trashing failed for a file", e);
- if (
- e.message ===
- CustomError.UPDATE_EXPORTED_RECORD_FAILED ||
- e.message ===
- CustomError.EXPORT_FOLDER_DOES_NOT_EXIST ||
- e.message === CustomError.EXPORT_STOPPED
- ) {
- throw e;
- }
- }
- }
- } catch (e) {
- if (
- e.message !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST &&
- e.message !== CustomError.EXPORT_STOPPED
- ) {
- log.error("fileTrasher failed", e);
- }
- throw e;
- }
- }
-
- async addFileExportedRecord(
- folder: string,
- fileUID: string,
- fileExportName: string,
- ) {
- try {
- const exportRecord = await this.getExportRecord(folder);
- if (!exportRecord.fileExportNames) {
- exportRecord.fileExportNames = {};
- }
- exportRecord.fileExportNames = {
- ...exportRecord.fileExportNames,
- [fileUID]: fileExportName,
- };
- await this.updateExportRecord(folder, exportRecord);
- } catch (e) {
- if (e.message !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST) {
- log.error("addFileExportedRecord failed", e);
- }
- throw e;
- }
- }
-
- async addCollectionExportedRecord(
- folder: string,
- collectionID: number,
- collectionExportName: string,
- ) {
- try {
- const exportRecord = await this.getExportRecord(folder);
- if (!exportRecord?.collectionExportNames) {
- exportRecord.collectionExportNames = {};
- }
- exportRecord.collectionExportNames = {
- ...exportRecord.collectionExportNames,
- [collectionID]: collectionExportName,
- };
-
- await this.updateExportRecord(folder, exportRecord);
- } catch (e) {
- if (e.message !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST) {
- log.error("addCollectionExportedRecord failed", e);
- }
- throw e;
- }
- }
-
- async removeCollectionExportedRecord(folder: string, collectionID: number) {
- try {
- const exportRecord = await this.getExportRecord(folder);
-
- exportRecord.collectionExportNames = Object.fromEntries(
- Object.entries(exportRecord.collectionExportNames).filter(
- ([key]) => key !== collectionID.toString(),
- ),
- );
-
- await this.updateExportRecord(folder, exportRecord);
- } catch (e) {
- if (e.message !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST) {
- log.error("removeCollectionExportedRecord failed", e);
- }
- throw e;
- }
- }
-
- async removeFileExportedRecord(folder: string, fileUID: string) {
- try {
- const exportRecord = await this.getExportRecord(folder);
- exportRecord.fileExportNames = Object.fromEntries(
- Object.entries(exportRecord.fileExportNames).filter(
- ([key]) => key !== fileUID,
- ),
- );
- await this.updateExportRecord(folder, exportRecord);
- } catch (e) {
- if (e.message !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST) {
- log.error("removeFileExportedRecord failed", e);
- }
- throw e;
- }
- }
-
- async updateExportRecord(folder: string, newData: Partial<ExportRecord>) {
- return this.exportRecordUpdater.add(() =>
- this.updateExportRecordHelper(folder, newData),
- );
- }
-
- async updateExportRecordHelper(
- folder: string,
- newData: Partial<ExportRecord>,
- ) {
- try {
- const exportRecord = await this.getExportRecord(folder);
- const newRecord: ExportRecord = { ...exportRecord, ...newData };
- await ensureElectron().fs.writeFileViaBackup(
- joinPath(folder, exportRecordFileName),
- JSON.stringify(newRecord, null, 2),
- );
- return newRecord;
- } catch (e) {
- if (e.message === CustomError.EXPORT_FOLDER_DOES_NOT_EXIST) {
- throw e;
- }
- log.error("error updating Export Record", e);
- throw Error(CustomError.UPDATE_EXPORTED_RECORD_FAILED);
- }
- }
-
- async getExportRecord(folder: string): Promise<ExportRecord> {
- const electron = ensureElectron();
- const fs = electron.fs;
- try {
- await this.verifyExportFolderExists(folder);
- 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);
- } catch (e) {
- if (e.message !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST) {
- log.error("export Record JSON parsing failed", e);
- }
- throw e;
- }
- }
-
- async createNewCollectionExport(
- exportFolder: string,
- collectionID: number,
- collectionIDNameMap: Map<number, string>,
- ) {
- const fs = ensureElectron().fs;
- await this.verifyExportFolderExists(exportFolder);
- const collectionName = collectionIDNameMap.get(collectionID);
- const collectionExportName = await safeDirectoryName(
- exportFolder,
- collectionName,
- fs.exists,
- );
- const collectionExportPath = joinPath(
- exportFolder,
- collectionExportName,
- );
- await fs.mkdirIfNeeded(collectionExportPath);
- await fs.mkdirIfNeeded(
- getMetadataFolderExportPath(collectionExportPath),
- );
-
- return collectionExportName;
- }
-
- async downloadAndSave(
- exportDir: string,
- collectionExportPath: string,
- file: EnteFile,
- ): Promise<void> {
- const electron = ensureElectron();
- try {
- const fileUID = getExportRecordFileUID(file);
- const originalFileStream = await downloadManager.fileStream(file);
- if (file.metadata.fileType === FileType.livePhoto) {
- await this.exportLivePhoto(
- exportDir,
- fileUID,
- collectionExportPath,
- originalFileStream,
- file,
- );
- } else {
- const fileExportName = await safeFileName(
- collectionExportPath,
- file.metadata.title,
- electron.fs.exists,
- );
- await this.saveMetadataFile(
- collectionExportPath,
- fileExportName,
- file,
- );
- await writeStream(
- electron,
- joinPath(collectionExportPath, fileExportName),
- originalFileStream,
- );
- await this.addFileExportedRecord(
- exportDir,
- fileUID,
- fileExportName,
- );
- }
- } catch (e) {
- log.error("download and save failed", e);
- throw e;
- }
- }
-
- private async exportLivePhoto(
- exportDir: string,
- fileUID: string,
- collectionExportPath: string,
- fileStream: ReadableStream,
- file: EnteFile,
- ) {
- const fs = ensureElectron().fs;
- const fileBlob = await new Response(fileStream).blob();
- const livePhoto = await decodeLivePhoto(file.metadata.title, fileBlob);
- const imageExportName = await safeFileName(
- collectionExportPath,
- livePhoto.imageFileName,
- fs.exists,
- );
- const videoExportName = await safeFileName(
- collectionExportPath,
- livePhoto.videoFileName,
- fs.exists,
- );
-
- const livePhotoExportName = getLivePhotoExportName(
- imageExportName,
- videoExportName,
- );
-
- await this.saveMetadataFile(
- collectionExportPath,
- imageExportName,
- file,
- );
- await writeStream(
- electron,
- joinPath(collectionExportPath, imageExportName),
- new Response(livePhoto.imageData).body,
- );
-
- await this.saveMetadataFile(
- collectionExportPath,
- videoExportName,
- file,
- );
- try {
- await writeStream(
- electron,
- joinPath(collectionExportPath, videoExportName),
- new Response(livePhoto.videoData).body,
- );
- } catch (e) {
- await fs.rm(joinPath(collectionExportPath, imageExportName));
- throw e;
- }
-
- await this.addFileExportedRecord(
- exportDir,
- fileUID,
- livePhotoExportName,
- );
- }
-
- private async saveMetadataFile(
- collectionExportPath: string,
- fileExportName: string,
- file: EnteFile,
- ) {
- const formatter = this.metadataDateTimeFormatter();
- await ensureElectron().fs.writeFile(
- getFileMetadataExportPath(collectionExportPath, fileExportName),
- getGoogleLikeMetadataFile(fileExportName, file, formatter),
- );
- }
-
- /**
- * Lazily created, cached instance of the date time formatter that should be
- * used for formatting the dates added to the metadata file.
- */
- private metadataDateTimeFormatter() {
- if (this.cachedMetadataDateTimeFormatter)
- return this.cachedMetadataDateTimeFormatter;
-
- // AFAIK, Google's format is not documented. It also seems to vary with
- // locale. This is a best attempt at constructing a formatter that
- // mirrors the format used by the timestamps in the takeout JSON.
- const formatter = new Intl.DateTimeFormat(i18n.language, {
- month: "short",
- day: "numeric",
- year: "numeric",
- hour: "numeric",
- minute: "numeric",
- second: "numeric",
- timeZoneName: "short",
- timeZone: "UTC",
- });
- this.cachedMetadataDateTimeFormatter = formatter;
- return formatter;
- }
-
- isExportInProgress = () => {
- return this.exportInProgress;
- };
-
- exportFolderExists = async (exportFolder: string) => {
- return exportFolder && (await ensureElectron().fs.exists(exportFolder));
- };
-
- private verifyExportFolderExists = async (exportFolder: string) => {
- try {
- if (!(await this.exportFolderExists(exportFolder))) {
- throw Error(CustomError.EXPORT_FOLDER_DOES_NOT_EXIST);
- }
- } catch (e) {
- if (e.message !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST) {
- log.error("verifyExportFolderExists failed", e);
- }
- throw e;
- }
- };
-
- private createEmptyExportRecord = async (exportRecordJSONPath: string) => {
- const exportRecord: ExportRecord = NULL_EXPORT_RECORD;
- await ensureElectron().fs.writeFile(
- exportRecordJSONPath,
- JSON.stringify(exportRecord, null, 2),
- );
- return exportRecord;
- };
-}
-
-const exportService = new ExportService();
-
-export default exportService;
-
-/**
- * If there are any in-progress exports, or if continuous exports are enabled,
- * resume them.
- */
-export const resumeExportsIfNeeded = async () => {
- const exportSettings = exportService.getExportSettings();
- if (!(await exportService.exportFolderExists(exportSettings?.folder))) {
- return;
- }
- const exportRecord = await exportService.getExportRecord(
- exportSettings.folder,
- );
- if (exportSettings.continuousExport) {
- exportService.enableContinuousExport();
- }
- if (isExportInProgress(exportRecord.stage)) {
- log.debug(() => "Resuming in-progress export");
- exportService.resumeExport();
- }
-};
-
-/**
- * Prompt the user to select a directory and create an export directory in it.
- *
- * If the user cancels the selection, return undefined.
- */
-export const selectAndPrepareExportDirectory = async (): Promise<
- string | undefined
-> => {
- const electron = ensureElectron();
-
- const rootDir = await electron.selectDirectory();
- if (!rootDir) return undefined;
-
- const exportDir = joinPath(rootDir, exportDirectoryName);
- await electron.fs.mkdirIfNeeded(exportDir);
- return exportDir;
-};
-
-export const getExportRecordFileUID = (file: EnteFile) =>
- `${file.id}_${file.collectionID}_${file.updationTime}`;
-
-export const getCollectionIDFromFileUID = (fileUID: string) =>
- Number(fileUID.split("_")[1]);
-
-const convertCollectionIDExportNameObjectToMap = (
- collectionExportNames: CollectionExportNames,
-): Map<number, string> => {
- return new Map<number, string>(
- Object.entries(collectionExportNames ?? {}).map((e) => {
- return [Number(e[0]), String(e[1])];
- }),
- );
-};
-
-const convertFileIDExportNameObjectToMap = (
- fileExportNames: FileExportNames,
-): Map<string, string> => {
- return new Map<string, string>(
- Object.entries(fileExportNames ?? {}).map((e) => {
- return [String(e[0]), String(e[1])];
- }),
- );
-};
-
-const getRenamedExportedCollections = (
- collections: Collection[],
- exportRecord: ExportRecord,
-) => {
- if (!exportRecord?.collectionExportNames) {
- return [];
- }
- const collectionIDExportNameMap = convertCollectionIDExportNameObjectToMap(
- exportRecord.collectionExportNames,
- );
- const renamedCollections = collections.filter((collection) => {
- if (collectionIDExportNameMap.has(collection.id)) {
- const currentExportName = collectionIDExportNameMap.get(
- collection.id,
- );
-
- const collectionExportName =
- getCollectionUserFacingName(collection);
-
- if (currentExportName === collectionExportName) {
- return false;
- }
- const hasNumberedSuffix = /\(\d+\)$/.exec(currentExportName);
- const currentExportNameWithoutNumberedSuffix = hasNumberedSuffix
- ? currentExportName.replace(/\(\d+\)$/, "")
- : currentExportName;
-
- return (
- collectionExportName !== currentExportNameWithoutNumberedSuffix
- );
- }
- return false;
- });
- return renamedCollections;
-};
-
-const getDeletedExportedCollections = (
- collections: Collection[],
- exportRecord: ExportRecord,
-) => {
- if (!exportRecord?.collectionExportNames) {
- return [];
- }
- const presentCollections = new Set(
- collections.map((collection) => collection.id),
- );
- const deletedExportedCollections = Object.keys(
- exportRecord?.collectionExportNames,
- )
- .map(Number)
- .filter((collectionID) => {
- if (!presentCollections.has(collectionID)) {
- return true;
- }
- return false;
- });
- return deletedExportedCollections;
-};
-
-/**
- * Return export record IDs of {@link files} for which there is also exists a
- * file on disk.
- */
-const readOnDiskFileExportRecordIDs = async (
- files: EnteFile[],
- collectionIDFolderNameMap: Map<number, string>,
- exportDir: string,
- exportRecord: ExportRecord,
- isCanceled: CancellationStatus,
-): Promise<Set<string>> => {
- const fs = ensureElectron().fs;
-
- const result = new Set<string>();
- if (!(await fs.exists(exportDir))) return result;
-
- // Both the paths involved are guaranteed to use POSIX separators and thus
- // can directly be compared.
- //
- // - `exportDir` traces its origin to `electron.selectDirectory()`, which
- // returns POSIX paths. Down below we use it as the base directory when
- // constructing paths for the items to export.
- //
- // - `findFiles` is also guaranteed to return POSIX paths.
- //
- const ls = new Set(await ensureElectron().fs.findFiles(exportDir));
-
- const fileExportNames = exportRecord.fileExportNames ?? {};
-
- for (const file of files) {
- if (isCanceled.status) throw Error(CustomError.EXPORT_STOPPED);
-
- const collectionExportName = collectionIDFolderNameMap.get(
- file.collectionID,
- );
- if (!collectionExportName) continue;
-
- const collectionExportPath = joinPath(exportDir, collectionExportName);
- const recordID = getExportRecordFileUID(file);
- const exportName = fileExportNames[recordID];
- if (!exportName) continue;
-
- if (ls.has(joinPath(collectionExportPath, exportName))) {
- result.add(recordID);
- } else {
- // It might be a live photo - these store a JSON string instead of
- // the file's name as the exportName.
- try {
- const { image, video } = parseLivePhotoExportName(exportName);
- if (
- ls.has(joinPath(collectionExportPath, image)) &&
- ls.has(joinPath(collectionExportPath, video))
- ) {
- result.add(recordID);
- }
- } catch {
- /* Not an error, the file just might not exist on disk yet */
- }
- }
- }
-
- return result;
-};
-
-/**
- * Return the list of files from amongst {@link allFiles} that still need to be
- * exported.
- *
- * @param allFiles The list of files to export.
- *
- * @param exportRecord The export record containing bookkeeping for the export.
- *
- * @paramd diskFileRecordIDs (Optional) The export record IDs of files from
- * amongst {@link allFiles} that already exist on disk. If provided (e.g. when
- * doing a resync), we perform an extra check for on-disk existence instead of
- * relying solely on the export record.
- */
-const getUnExportedFiles = (
- allFiles: EnteFile[],
- exportRecord: ExportRecord | undefined,
- diskFileRecordIDs: Set<string> | undefined,
-) => {
- if (!exportRecord?.fileExportNames) {
- return allFiles;
- }
- const exportedFiles = new Set(Object.keys(exportRecord?.fileExportNames));
- return allFiles.filter((file) => {
- const recordID = getExportRecordFileUID(file);
- if (!exportedFiles.has(recordID)) return true;
- if (diskFileRecordIDs && !diskFileRecordIDs.has(recordID)) return true;
- return false;
- });
-};
-
-const getDeletedExportedFiles = (
- allFiles: EnteFile[],
- exportRecord: ExportRecord,
-): string[] => {
- if (!exportRecord?.fileExportNames) {
- return [];
- }
- const presentFileUIDs = new Set(
- allFiles?.map((file) => getExportRecordFileUID(file)),
- );
- const deletedExportedFiles = Object.keys(
- exportRecord?.fileExportNames,
- ).filter((fileUID) => {
- if (!presentFileUIDs.has(fileUID)) {
- return true;
- }
- return false;
- });
- return deletedExportedFiles;
-};
-
-const getCollectionExportedFiles = (
- exportRecord: ExportRecord,
- collectionID: number,
-): string[] => {
- if (!exportRecord?.fileExportNames) {
- return [];
- }
- const collectionExportedFiles = Object.keys(
- exportRecord?.fileExportNames,
- ).filter((fileUID) => {
- const fileCollectionID = Number(fileUID.split("_")[1]);
- if (fileCollectionID === collectionID) {
- return true;
- } else {
- return false;
- }
- });
- return collectionExportedFiles;
-};
-
-const getGoogleLikeMetadataFile = (
- fileExportName: string,
- file: EnteFile,
- dateTimeFormatter: Intl.DateTimeFormat,
-) => {
- const metadata: Metadata = file.metadata;
- const publicMagicMetadata = file.pubMagicMetadata?.data;
- const creationTime = Math.floor(
- (publicMagicMetadata?.editedTime ?? metadata.creationTime) / 1e6,
- );
- const modificationTime = Math.floor(
- (metadata.modificationTime ?? metadata.creationTime) / 1e6,
- );
- const result: Record<string, unknown> = {
- title: fileExportName,
- creationTime: {
- timestamp: `${creationTime}`,
- formatted: dateTimeFormatter.format(creationTime * 1000),
- },
- modificationTime: {
- timestamp: `${modificationTime}`,
- formatted: dateTimeFormatter.format(modificationTime * 1000),
- },
- };
- const caption = file?.pubMagicMetadata?.data?.caption;
- if (caption) result.caption = caption;
- const geoData = fileLocation(file);
- if (geoData) result.geoData = geoData;
- return JSON.stringify(result, null, 2);
-};
-
-export const getMetadataFolderExportPath = (collectionExportPath: string) =>
- joinPath(collectionExportPath, exportMetadataDirectoryName);
-
-// if filepath is /home/user/Ente/Export/Collection1/1.jpg
-// then metadata path is /home/user/Ente/Export/Collection1/ENTE_METADATA_FOLDER/1.jpg.json
-const getFileMetadataExportPath = (
- collectionExportPath: string,
- fileExportName: string,
-) =>
- joinPath(
- collectionExportPath,
- joinPath(exportMetadataDirectoryName, `${fileExportName}.json`),
- );
-
-export const getLivePhotoExportName = (
- imageExportName: string,
- videoExportName: string,
-) => JSON.stringify({ image: imageExportName, video: videoExportName });
-
-export const isLivePhotoExportName = (exportName: string) => {
- try {
- JSON.parse(exportName);
- return true;
- } catch {
- return false;
- }
-};
-
-const parseLivePhotoExportName = (
- livePhotoExportName: string,
-): { image: string; video: string } => {
- const { image, video } = JSON.parse(livePhotoExportName);
- return { image, video };
-};
-
-const isExportInProgress = (exportStage: ExportStage) =>
- exportStage > ExportStage.init && exportStage < ExportStage.finished;
-
-/**
- * Move {@link fileName} in {@link collectionName} to the special per-collection
- * file system "Trash" folder we created under the export directory.
- *
- * Also move its associated metadata JSON to Trash.
- *
- * @param exportDir The root directory on the user's file system where we are
- * exporting to.
- * */
-const moveToFSTrash = async (
- exportDir: string,
- collectionName: string,
- fileName: string,
-) => {
- const fs = ensureElectron().fs;
-
- const filePath = joinPath(exportDir, joinPath(collectionName, fileName));
- const trashDir = joinPath(
- exportDir,
- joinPath(exportTrashDirectoryName, collectionName),
- );
- const metadataFileName = `${fileName}.json`;
- const metadataFilePath = joinPath(
- exportDir,
- joinPath(
- collectionName,
- joinPath(exportMetadataDirectoryName, metadataFileName),
- ),
- );
- const metadataTrashDir = joinPath(
- exportDir,
- joinPath(
- exportTrashDirectoryName,
- joinPath(collectionName, exportMetadataDirectoryName),
- ),
- );
-
- log.info(`Moving file ${filePath} and its metadata to trash folder`);
-
- if (await fs.exists(filePath)) {
- await fs.mkdirIfNeeded(trashDir);
- const trashFileName = await safeFileName(trashDir, fileName, fs.exists);
- const trashFilePath = joinPath(trashDir, trashFileName);
- await fs.rename(filePath, trashFilePath);
- }
-
- if (await fs.exists(metadataFilePath)) {
- await fs.mkdirIfNeeded(metadataTrashDir);
- const metadataTrashFileName = await safeFileName(
- metadataTrashDir,
- metadataFileName,
- fs.exists,
- );
- const metadataTrashFilePath = joinPath(
- metadataTrashDir,
- metadataTrashFileName,
- );
- await fs.rename(metadataFilePath, metadataTrashFilePath);
- }
-};
diff --git a/web/apps/photos/src/services/export/migration.ts b/web/packages/new/photos/services/export-migration.ts
similarity index 96%
rename from web/apps/photos/src/services/export/migration.ts
rename to web/packages/new/photos/services/export-migration.ts
index b775db08d7..94cd037964 100644
--- a/web/apps/photos/src/services/export/migration.ts
+++ b/web/packages/new/photos/services/export-migration.ts
@@ -1,3 +1,8 @@
+// TODO: Audit this file
+/* eslint-disable @typescript-eslint/require-await */
+/* eslint-disable @typescript-eslint/ban-ts-comment */
+/* eslint-disable @typescript-eslint/no-unsafe-assignment */
+/* eslint-disable @typescript-eslint/no-unnecessary-condition */
import { ensureElectron } from "ente-base/electron";
import { joinPath, nameAndExtension } from "ente-base/file-name";
import log from "ente-base/log";
@@ -26,7 +31,7 @@ import exportService, {
type ExportProgress,
type ExportStage,
type FileExportNames,
-} from ".";
+} from "./export";
type ExportedCollectionPaths = Record<number, string>;
@@ -187,7 +192,9 @@ async function migrationV2ToV3(
updateProgress,
);
+ // @ts-ignore
exportRecord.exportedCollectionPaths = undefined;
+ // @ts-ignore
exportRecord.exportedFiles = undefined;
const updatedExportRecord: ExportRecord = {
...exportRecord,
@@ -255,11 +262,13 @@ async function migrateFiles(
for (const file of files) {
const collectionPath = collectionIDPathMap.get(file.collectionID);
const metadataPath = joinPath(
+ // @ts-ignore
collectionPath,
exportMetadataDirectoryName,
);
const oldFileName = `${file.id}_${oldSanitizeName(file.metadata.title)}`;
+ // @ts-ignore
const oldFilePath = joinPath(collectionPath, oldFileName);
const oldFileMetadataPath = joinPath(
metadataPath,
@@ -267,10 +276,12 @@ async function migrateFiles(
);
const newFileName = await safeFileName(
+ // @ts-ignore
collectionPath,
file.metadata.title,
fs.exists,
);
+ // @ts-ignore
const newFilePath = joinPath(collectionPath, newFileName);
const newFileMetadataPath = joinPath(
metadataPath,
@@ -289,12 +300,15 @@ async function removeDeprecatedExportRecordProperties(
exportDir: string,
) {
if (exportRecord?.queuedFiles) {
+ // @ts-ignore
exportRecord.queuedFiles = undefined;
}
if (exportRecord?.progress) {
+ // @ts-ignore
exportRecord.progress = undefined;
}
if (exportRecord?.failedFiles) {
+ // @ts-ignore
exportRecord.failedFiles = undefined;
}
await exportService.updateExportRecord(exportDir, exportRecord);
@@ -304,6 +318,7 @@ async function getCollectionExportNamesFromExportedCollectionPaths(
exportRecord: ExportRecordV2,
): Promise<CollectionExportNames> {
if (!exportRecord.exportedCollectionPaths) {
+ // @ts-ignore
return;
}
const exportedCollectionNames = Object.fromEntries(
@@ -316,6 +331,7 @@ async function getCollectionExportNamesFromExportedCollectionPaths(
},
),
);
+ // @ts-ignore
return exportedCollectionNames;
}
@@ -332,6 +348,7 @@ async function getFileExportNamesFromExportedFiles(
updateProgress: (progress: ExportProgress) => void,
): Promise<FileExportNames> {
if (!exportedFiles.length) {
+ // @ts-ignore
return;
}
log.info(
@@ -361,11 +378,13 @@ async function getFileExportNamesFromExportedFiles(
fileBlob,
);
const imageExportName = getUniqueFileExportNameForMigration(
+ // @ts-ignore
collectionPath,
imageFileName,
usedFilePaths,
);
const videoExportName = getUniqueFileExportNameForMigration(
+ // @ts-ignore
collectionPath,
videoFileName,
usedFilePaths,
@@ -376,6 +395,7 @@ async function getFileExportNamesFromExportedFiles(
);
} else {
fileExportName = getUniqueFileExportNameForMigration(
+ // @ts-ignore
collectionPath,
file.metadata.title,
usedFilePaths,
@@ -386,6 +406,7 @@ async function getFileExportNamesFromExportedFiles(
`file export name for ${file.metadata.title} is ${fileExportName}`,
);
exportedFileNames = {
+ // @ts-ignore
...exportedFileNames,
[getExportRecordFileUID(file)]: fileExportName,
};
@@ -395,6 +416,7 @@ async function getFileExportNamesFromExportedFiles(
failed: 0,
});
}
+ // @ts-ignore
return exportedFileNames;
}
@@ -411,6 +433,7 @@ function reMigrateCollectionExportNames(
},
),
);
+ // @ts-ignore
return exportedCollectionNames;
}
@@ -550,6 +573,7 @@ const getUniqueFileExportNameForMigration = (
if (!usedFilePaths.has(collectionPath)) {
usedFilePaths.set(collectionPath, new Set());
}
+ // @ts-ignore
usedFilePaths
.get(collectionPath)
.add(getFileSavePath(collectionPath, fileExportName));
diff --git a/web/packages/new/photos/services/export.ts b/web/packages/new/photos/services/export.ts
index 40229741ed..e05caf5663 100644
--- a/web/packages/new/photos/services/export.ts
+++ b/web/packages/new/photos/services/export.ts
@@ -1,3 +1,36 @@
+// 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 */
+/* eslint-disable @typescript-eslint/no-unnecessary-condition */
+/* eslint-disable @typescript-eslint/no-floating-promises */
+import { ensureElectron } from "ente-base/electron";
+import { joinPath } from "ente-base/file-name";
+import log from "ente-base/log";
+import { downloadManager } from "ente-gallery/services/download";
+import { writeStream } from "ente-gallery/utils/native-stream";
+import type { Collection } from "ente-media/collection";
+import { mergeMetadata, type EnteFile } from "ente-media/file";
+import { fileLocation, type Metadata } from "ente-media/file-metadata";
+import { FileType } from "ente-media/file-type";
+import { decodeLivePhoto } from "ente-media/live-photo";
+import {
+ createCollectionNameByID,
+ getCollectionUserFacingName,
+} from "ente-new/photos/services/collection";
+import { getAllLocalCollections } from "ente-new/photos/services/collections";
+import { getAllLocalFiles } from "ente-new/photos/services/files";
+import {
+ safeDirectoryName,
+ safeFileName,
+} from "ente-new/photos/utils/native-fs";
+import { CustomError } from "ente-shared/error";
+import { getData, setData } from "ente-shared/storage/localStorage";
+import { PromiseQueue } from "ente-utils/promise";
+import i18n from "i18next";
+import { migrateExport, type ExportRecord } from "./export-migration";
+
/**
* Name of the directory in which we put our metadata when exporting to the file
* system.
@@ -9,3 +42,1547 @@ export const exportMetadataDirectoryName = "metadata";
* have been exported to the local disk previously.
*/
export const exportTrashDirectoryName = "Trash";
+
+/** Name of the JSON file in which we keep the state of the export. */
+const exportRecordFileName = "export_status.json";
+
+/**
+ * Name of the top level directory which we create underneath the selected
+ * directory when the user starts an export to the file system.
+ */
+const exportDirectoryName = "Ente Photos";
+
+export const ExportStage = {
+ init: 0,
+ migration: 1,
+ starting: 2,
+ exportingFiles: 3,
+ trashingDeletedFiles: 4,
+ renamingCollectionFolders: 5,
+ trashingDeletedCollections: 6,
+ finished: 7,
+} as const;
+
+export type ExportStage = (typeof ExportStage)[keyof typeof ExportStage];
+
+export interface ExportProgress {
+ success: number;
+ failed: number;
+ total: number;
+}
+
+export interface ExportSettings {
+ folder: string;
+ continuousExport: boolean;
+}
+
+export type CollectionExportNames = Record<number, string>;
+
+export type FileExportNames = Record<string, string>;
+
+export const NULL_EXPORT_RECORD: ExportRecord = {
+ version: 3,
+ // @ts-ignore
+ lastAttemptTimestamp: null,
+ stage: ExportStage.init,
+ fileExportNames: {},
+ collectionExportNames: {},
+};
+
+export interface ExportOpts {
+ /**
+ * If true, perform an additional on-disk check to determine which files
+ * need to be exported.
+ *
+ * This has performance implications for huge libraries, so we only do this:
+ * - For the first export after an app start
+ * - If the user explicitly presses the "Resync" button.
+ */
+ resync?: boolean;
+}
+
+interface ExportUIUpdaters {
+ setExportStage: (stage: ExportStage) => void;
+ setExportProgress: (progress: ExportProgress) => void;
+ setLastExportTime: (exportTime: number) => void;
+ setPendingFiles: (pendingFiles: EnteFile[]) => void;
+}
+
+interface RequestCanceller {
+ exec: () => void;
+}
+
+interface CancellationStatus {
+ status: boolean;
+}
+
+class ExportService {
+ // @ts-ignore
+ private exportSettings: ExportSettings;
+ // @ts-ignore
+ private exportInProgress: RequestCanceller = null;
+ private resync = true;
+ private reRunNeeded = false;
+ private exportRecordUpdater = new PromiseQueue<ExportRecord>();
+ // @ts-ignore
+ private continuousExportEventHandler: () => void;
+ private uiUpdater: ExportUIUpdaters = {
+ setExportProgress: () => {},
+ setExportStage: () => {},
+ setLastExportTime: () => {},
+ setPendingFiles: () => {},
+ };
+ private currentExportProgress: ExportProgress = {
+ total: 0,
+ success: 0,
+ failed: 0,
+ };
+ // @ts-ignore
+ private cachedMetadataDateTimeFormatter: Intl.DateTimeFormat;
+
+ getExportSettings(): ExportSettings {
+ try {
+ if (this.exportSettings) {
+ return this.exportSettings;
+ }
+ const exportSettings = getData("export");
+ this.exportSettings = exportSettings;
+ return exportSettings;
+ } catch (e) {
+ log.error("getExportSettings failed", e);
+ throw e;
+ }
+ }
+
+ updateExportSettings(newData: Partial<ExportSettings>) {
+ try {
+ const exportSettings = this.getExportSettings();
+ const newSettings = { ...exportSettings, ...newData };
+ this.exportSettings = newSettings;
+ setData("export", newSettings);
+ } catch (e) {
+ log.error("updateExportSettings failed", e);
+ throw e;
+ }
+ }
+
+ async runMigration(
+ exportDir: string,
+ exportRecord: ExportRecord,
+ updateProgress: (progress: ExportProgress) => void,
+ ) {
+ try {
+ log.info("running migration");
+ await migrateExport(exportDir, exportRecord, updateProgress);
+ log.info("migration completed");
+ } catch (e) {
+ log.error("migration failed", e);
+ throw e;
+ }
+ }
+
+ setUIUpdaters(uiUpdater: ExportUIUpdaters) {
+ this.uiUpdater = uiUpdater;
+ this.uiUpdater.setExportProgress(this.currentExportProgress);
+ }
+
+ private updateExportProgress(exportProgress: ExportProgress) {
+ this.currentExportProgress = exportProgress;
+ this.uiUpdater.setExportProgress(exportProgress);
+ }
+
+ private async updateExportStage(stage: ExportStage) {
+ const exportFolder = this.getExportSettings()?.folder;
+ await this.updateExportRecord(exportFolder, { stage });
+ this.uiUpdater.setExportStage(stage);
+ }
+
+ private async updateLastExportTime(exportTime: number) {
+ const exportFolder = this.getExportSettings()?.folder;
+ await this.updateExportRecord(exportFolder, {
+ lastAttemptTimestamp: exportTime,
+ });
+ this.uiUpdater.setLastExportTime(exportTime);
+ }
+
+ private resyncOnce() {
+ const resync = this.resync;
+ this.resync = false;
+ return resync;
+ }
+
+ resumeExport() {
+ this.scheduleExport({ resync: this.resyncOnce() });
+ }
+
+ enableContinuousExport() {
+ // @ts-ignore
+ if (this.continuousExportEventHandler) {
+ log.warn("Continuous export already enabled");
+ return;
+ }
+ this.continuousExportEventHandler = () => {
+ this.scheduleExport({ resync: this.resyncOnce() });
+ };
+ this.continuousExportEventHandler();
+ }
+
+ disableContinuousExport() {
+ if (!this.continuousExportEventHandler) {
+ log.warn("Continuous export already disabled");
+ return;
+ }
+ // @ts-ignore
+ this.continuousExportEventHandler = null;
+ }
+
+ /**
+ * Called when the local database of files changes.
+ */
+ onLocalFilesUpdated() {
+ if (this.continuousExportEventHandler) {
+ this.continuousExportEventHandler();
+ }
+ }
+
+ /**
+ * Return the list of files that have not yet been exported.
+ *
+ * @param exportRecord The export record containing information about the
+ * export, including files which've been exported. If an export record is
+ * not specified (e.g. if the user has not exported anything yet and we just
+ * wish to show a preview of what will be exported), then the function will
+ * return the list of all files that will be exported if an export were to
+ * happen.
+ */
+ pendingFiles = async (exportRecord?: ExportRecord): Promise<EnteFile[]> => {
+ return getUnExportedFiles(
+ await getAllLocalFiles(),
+ exportRecord,
+ undefined,
+ );
+ };
+
+ async preExport(exportFolder: string) {
+ await this.verifyExportFolderExists(exportFolder);
+ const exportRecord = await this.getExportRecord(exportFolder);
+ await this.updateExportStage(ExportStage.migration);
+ await this.runMigration(
+ exportFolder,
+ exportRecord,
+ this.updateExportProgress.bind(this),
+ );
+ await this.updateExportStage(ExportStage.starting);
+ }
+
+ async postExport() {
+ try {
+ const exportFolder = this.getExportSettings()?.folder;
+ if (!(await this.exportFolderExists(exportFolder))) {
+ this.uiUpdater.setExportStage(ExportStage.init);
+ return;
+ }
+ await this.updateExportStage(ExportStage.finished);
+ await this.updateLastExportTime(Date.now());
+ this.uiUpdater.setPendingFiles(
+ await this.pendingFiles(
+ await this.getExportRecord(exportFolder),
+ ),
+ );
+ } catch (e) {
+ log.error("postExport failed", e);
+ }
+ }
+
+ async stopRunningExport() {
+ try {
+ log.info("user requested export cancellation");
+ this.exportInProgress.exec();
+ // @ts-ignore
+ this.exportInProgress = null;
+ this.reRunNeeded = false;
+ await this.postExport();
+ } catch (e) {
+ log.error("stopRunningExport failed", e);
+ }
+ }
+
+ scheduleExport = async (exportOpts: ExportOpts) => {
+ try {
+ if (this.exportInProgress) {
+ log.info("export in progress, scheduling re-run");
+ this.reRunNeeded = true;
+ return;
+ } else {
+ log.info("export not in progress, starting export");
+ }
+
+ const isCanceled: CancellationStatus = { status: false };
+ const canceller: RequestCanceller = {
+ exec: () => {
+ isCanceled.status = true;
+ },
+ };
+ this.exportInProgress = canceller;
+ try {
+ const exportFolder = this.getExportSettings()?.folder;
+ await this.preExport(exportFolder);
+ log.info("export started");
+ await this.runExport(exportFolder, isCanceled, exportOpts);
+ log.info("export completed");
+ } finally {
+ if (isCanceled.status) {
+ log.info("export cancellation done");
+ if (!this.exportInProgress) {
+ await this.postExport();
+ }
+ } else {
+ await this.postExport();
+ log.info("resetting export in progress after completion");
+ // @ts-ignore
+ this.exportInProgress = null;
+ if (this.reRunNeeded) {
+ this.reRunNeeded = false;
+ log.info("re-running export");
+ setTimeout(() => this.scheduleExport(exportOpts), 0);
+ }
+ }
+ }
+ } catch (e) {
+ if (
+ // @ts-ignore
+ e.message !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST &&
+ // @ts-ignore
+ e.message !== CustomError.EXPORT_STOPPED
+ ) {
+ log.error("scheduleExport failed", e);
+ }
+ }
+ };
+
+ private async runExport(
+ exportFolder: string,
+ isCanceled: CancellationStatus,
+ { resync }: ExportOpts,
+ ) {
+ try {
+ const files = mergeMetadata(await getAllLocalFiles());
+ const collections = await getAllLocalCollections();
+
+ const exportRecord = await this.getExportRecord(exportFolder);
+ const collectionIDExportNameMap =
+ convertCollectionIDExportNameObjectToMap(
+ exportRecord.collectionExportNames,
+ );
+ const collectionIDNameMap = createCollectionNameByID(collections);
+
+ const renamedCollections = getRenamedExportedCollections(
+ collections,
+ exportRecord,
+ );
+
+ const removedFileUIDs = getDeletedExportedFiles(
+ files,
+ exportRecord,
+ );
+
+ const diskFileRecordIDs = resync
+ ? await readOnDiskFileExportRecordIDs(
+ files,
+ collectionIDExportNameMap,
+ exportFolder,
+ exportRecord,
+ isCanceled,
+ )
+ : undefined;
+
+ const filesToExport = getUnExportedFiles(
+ files,
+ exportRecord,
+ diskFileRecordIDs,
+ );
+
+ const deletedExportedCollections = getDeletedExportedCollections(
+ collections,
+ exportRecord,
+ );
+
+ log.info(
+ `[export] files: ${files.length}, disk files: ${diskFileRecordIDs?.size ?? "<na>"}, unexported files: ${filesToExport.length}, deleted exported files: ${removedFileUIDs.length}, renamed collections: ${renamedCollections.length}, deleted collections: ${deletedExportedCollections.length}`,
+ );
+ let success = 0;
+ let failed = 0;
+ this.uiUpdater.setExportProgress({
+ success: success,
+ failed: failed,
+ total: filesToExport.length,
+ });
+ const incrementSuccess = () => {
+ this.updateExportProgress({
+ success: ++success,
+ failed: failed,
+ total: filesToExport.length,
+ });
+ };
+ const incrementFailed = () => {
+ this.updateExportProgress({
+ success: success,
+ failed: ++failed,
+ total: filesToExport.length,
+ });
+ };
+ if (renamedCollections?.length > 0) {
+ this.updateExportStage(ExportStage.renamingCollectionFolders);
+ log.info(`renaming ${renamedCollections.length} collections`);
+ await this.collectionRenamer(
+ exportFolder,
+ collectionIDExportNameMap,
+ renamedCollections,
+ isCanceled,
+ );
+ }
+
+ if (removedFileUIDs?.length > 0) {
+ this.updateExportStage(ExportStage.trashingDeletedFiles);
+ log.info(`trashing ${removedFileUIDs.length} files`);
+ await this.fileTrasher(
+ exportFolder,
+ collectionIDExportNameMap,
+ removedFileUIDs,
+ isCanceled,
+ );
+ }
+ if (filesToExport?.length > 0) {
+ this.updateExportStage(ExportStage.exportingFiles);
+ log.info(`exporting ${filesToExport.length} files`);
+ await this.fileExporter(
+ filesToExport,
+ collectionIDNameMap,
+ collectionIDExportNameMap,
+ exportFolder,
+ incrementSuccess,
+ incrementFailed,
+ isCanceled,
+ );
+ }
+ if (deletedExportedCollections?.length > 0) {
+ this.updateExportStage(ExportStage.trashingDeletedCollections);
+ log.info(
+ `removing ${deletedExportedCollections.length} collections`,
+ );
+ await this.collectionRemover(
+ deletedExportedCollections,
+ exportFolder,
+ isCanceled,
+ );
+ }
+ } catch (e) {
+ if (
+ // @ts-ignore
+ e.message !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST &&
+ // @ts-ignore
+ e.message !== CustomError.EXPORT_STOPPED
+ ) {
+ log.error("runExport failed", e);
+ }
+ throw e;
+ }
+ }
+
+ async collectionRenamer(
+ exportFolder: string,
+ collectionIDExportNameMap: Map<number, string>,
+ renamedCollections: Collection[],
+ isCanceled: CancellationStatus,
+ ) {
+ const fs = ensureElectron().fs;
+ try {
+ for (const collection of renamedCollections) {
+ try {
+ if (isCanceled.status) {
+ throw Error(CustomError.EXPORT_STOPPED);
+ }
+ await this.verifyExportFolderExists(exportFolder);
+ const oldCollectionExportName =
+ collectionIDExportNameMap.get(collection.id);
+ const oldCollectionExportPath = joinPath(
+ exportFolder,
+ // @ts-ignore
+ oldCollectionExportName,
+ );
+ const newCollectionExportName = await safeDirectoryName(
+ exportFolder,
+ getCollectionUserFacingName(collection),
+ fs.exists,
+ );
+ log.info(
+ `renaming collection with id ${collection.id} from ${oldCollectionExportName} to ${newCollectionExportName}`,
+ );
+ const newCollectionExportPath = joinPath(
+ exportFolder,
+ newCollectionExportName,
+ );
+ await this.addCollectionExportedRecord(
+ exportFolder,
+ collection.id,
+ newCollectionExportName,
+ );
+ collectionIDExportNameMap.set(
+ collection.id,
+ newCollectionExportName,
+ );
+ try {
+ await fs.rename(
+ oldCollectionExportPath,
+ newCollectionExportPath,
+ );
+ } catch (e) {
+ await this.addCollectionExportedRecord(
+ exportFolder,
+ collection.id,
+ // @ts-ignore
+ oldCollectionExportName,
+ );
+ collectionIDExportNameMap.set(
+ collection.id,
+ // @ts-ignore
+ oldCollectionExportName,
+ );
+ throw e;
+ }
+ log.info(
+ `renaming collection with id ${collection.id} from ${oldCollectionExportName} to ${newCollectionExportName} successful`,
+ );
+ } catch (e) {
+ log.error("collectionRenamer failed a collection", e);
+ if (
+ // @ts-ignore
+ e.message ===
+ CustomError.UPDATE_EXPORTED_RECORD_FAILED ||
+ // @ts-ignore
+ e.message ===
+ CustomError.EXPORT_FOLDER_DOES_NOT_EXIST ||
+ // @ts-ignore
+ e.message === CustomError.EXPORT_STOPPED
+ ) {
+ throw e;
+ }
+ }
+ }
+ } catch (e) {
+ if (
+ // @ts-ignore
+ e.message !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST &&
+ // @ts-ignore
+ e.message !== CustomError.EXPORT_STOPPED
+ ) {
+ log.error("collectionRenamer failed", e);
+ }
+ throw e;
+ }
+ }
+
+ async collectionRemover(
+ deletedExportedCollectionIDs: number[],
+ exportFolder: string,
+ isCanceled: CancellationStatus,
+ ) {
+ const fs = ensureElectron().fs;
+ const rmdirIfExists = async (dirPath: string) => {
+ if (await fs.exists(dirPath)) await fs.rmdir(dirPath);
+ };
+ try {
+ const exportRecord = await this.getExportRecord(exportFolder);
+ const collectionIDPathMap =
+ convertCollectionIDExportNameObjectToMap(
+ exportRecord.collectionExportNames,
+ );
+ for (const collectionID of deletedExportedCollectionIDs) {
+ try {
+ if (isCanceled.status) {
+ throw Error(CustomError.EXPORT_STOPPED);
+ }
+ await this.verifyExportFolderExists(exportFolder);
+ log.info(
+ `removing collection with id ${collectionID} from export folder`,
+ );
+ const collectionExportName =
+ collectionIDPathMap.get(collectionID);
+ // verify that the all exported files from the collection has been removed
+ const collectionExportedFiles = getCollectionExportedFiles(
+ exportRecord,
+ collectionID,
+ );
+ if (collectionExportedFiles.length > 0) {
+ throw new Error(
+ "collection is not empty, can't remove",
+ );
+ }
+ const collectionExportPath = joinPath(
+ exportFolder,
+ // @ts-ignore
+ collectionExportName,
+ );
+ await this.removeCollectionExportedRecord(
+ exportFolder,
+ collectionID,
+ );
+ try {
+ // delete the collection metadata folder
+ await rmdirIfExists(
+ getMetadataFolderExportPath(collectionExportPath),
+ );
+ // delete the collection folder
+ await rmdirIfExists(collectionExportPath);
+ } catch (e) {
+ await this.addCollectionExportedRecord(
+ exportFolder,
+ collectionID,
+ // @ts-ignore
+ collectionExportName,
+ );
+ throw e;
+ }
+ log.info(
+ `removing collection with id ${collectionID} from export folder successful`,
+ );
+ } catch (e) {
+ log.error("collectionRemover failed a collection", e);
+ if (
+ // @ts-ignore
+ e.message ===
+ CustomError.UPDATE_EXPORTED_RECORD_FAILED ||
+ // @ts-ignore
+ e.message ===
+ CustomError.EXPORT_FOLDER_DOES_NOT_EXIST ||
+ // @ts-ignore
+ e.message === CustomError.EXPORT_STOPPED
+ ) {
+ throw e;
+ }
+ }
+ }
+ } catch (e) {
+ if (
+ // @ts-ignore
+ e.message !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST &&
+ // @ts-ignore
+ e.message !== CustomError.EXPORT_STOPPED
+ ) {
+ log.error("collectionRemover failed", e);
+ }
+ throw e;
+ }
+ }
+
+ async fileExporter(
+ files: EnteFile[],
+ collectionIDNameMap: Map<number, string>,
+ collectionIDFolderNameMap: Map<number, string>,
+ exportDir: string,
+ incrementSuccess: () => void,
+ incrementFailed: () => void,
+ isCanceled: CancellationStatus,
+ ): Promise<void> {
+ const fs = ensureElectron().fs;
+ try {
+ for (const file of files) {
+ log.info(
+ `exporting file ${file.metadata.title} with id ${
+ file.id
+ } from collection ${collectionIDNameMap.get(
+ file.collectionID,
+ )}`,
+ );
+ if (isCanceled.status) {
+ throw Error(CustomError.EXPORT_STOPPED);
+ }
+ try {
+ await this.verifyExportFolderExists(exportDir);
+ let collectionExportName = collectionIDFolderNameMap.get(
+ file.collectionID,
+ );
+ if (!collectionExportName) {
+ collectionExportName =
+ await this.createNewCollectionExport(
+ exportDir,
+ file.collectionID,
+ collectionIDNameMap,
+ );
+ await this.addCollectionExportedRecord(
+ exportDir,
+ file.collectionID,
+ collectionExportName,
+ );
+ collectionIDFolderNameMap.set(
+ file.collectionID,
+ collectionExportName,
+ );
+ }
+ const collectionExportPath = joinPath(
+ exportDir,
+ collectionExportName,
+ );
+ await fs.mkdirIfNeeded(collectionExportPath);
+ await fs.mkdirIfNeeded(
+ getMetadataFolderExportPath(collectionExportPath),
+ );
+ await this.downloadAndSave(
+ exportDir,
+ collectionExportPath,
+ file,
+ );
+ incrementSuccess();
+ log.info(
+ `exporting file ${file.metadata.title} with id ${
+ file.id
+ } from collection ${collectionIDNameMap.get(
+ file.collectionID,
+ )} successful`,
+ );
+ } catch (e) {
+ incrementFailed();
+ log.error("export failed for a file", e);
+ if (
+ // @ts-ignore
+ e.message ===
+ CustomError.UPDATE_EXPORTED_RECORD_FAILED ||
+ // @ts-ignore
+ e.message ===
+ CustomError.EXPORT_FOLDER_DOES_NOT_EXIST ||
+ // @ts-ignore
+ e.message === CustomError.EXPORT_STOPPED
+ ) {
+ throw e;
+ }
+ }
+ }
+ } catch (e) {
+ if (
+ // @ts-ignore
+ e.message !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST &&
+ // @ts-ignore
+ e.message !== CustomError.EXPORT_STOPPED
+ ) {
+ log.error("fileExporter failed", e);
+ }
+ throw e;
+ }
+ }
+
+ async fileTrasher(
+ exportDir: string,
+ collectionIDExportNameMap: Map<number, string>,
+ removedFileUIDs: string[],
+ isCanceled: CancellationStatus,
+ ): Promise<void> {
+ try {
+ const exportRecord = await this.getExportRecord(exportDir);
+ const fileIDExportNameMap = convertFileIDExportNameObjectToMap(
+ exportRecord.fileExportNames,
+ );
+ for (const fileUID of removedFileUIDs) {
+ await this.verifyExportFolderExists(exportDir);
+ log.info(`trashing file with id ${fileUID}`);
+ if (isCanceled.status) {
+ throw Error(CustomError.EXPORT_STOPPED);
+ }
+ try {
+ const fileExportName = fileIDExportNameMap.get(fileUID);
+ const collectionID = getCollectionIDFromFileUID(fileUID);
+ const collectionExportName =
+ collectionIDExportNameMap.get(collectionID);
+
+ // @ts-ignore
+ if (isLivePhotoExportName(fileExportName)) {
+ const { image, video } =
+ // @ts-ignore
+ parseLivePhotoExportName(fileExportName);
+
+ await moveToFSTrash(
+ exportDir,
+ // @ts-ignore
+ collectionExportName,
+ image,
+ );
+
+ await moveToFSTrash(
+ exportDir,
+ // @ts-ignore
+ collectionExportName,
+ video,
+ );
+ } else {
+ await moveToFSTrash(
+ exportDir,
+ // @ts-ignore
+ collectionExportName,
+ fileExportName,
+ );
+ }
+
+ await this.removeFileExportedRecord(exportDir, fileUID);
+
+ log.info(`Moved file id ${fileUID} to Trash`);
+ } catch (e) {
+ log.error("trashing failed for a file", e);
+ if (
+ // @ts-ignore
+ e.message ===
+ CustomError.UPDATE_EXPORTED_RECORD_FAILED ||
+ // @ts-ignore
+ e.message ===
+ CustomError.EXPORT_FOLDER_DOES_NOT_EXIST ||
+ // @ts-ignore
+ e.message === CustomError.EXPORT_STOPPED
+ ) {
+ throw e;
+ }
+ }
+ }
+ } catch (e) {
+ if (
+ // @ts-ignore
+ e.message !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST &&
+ // @ts-ignore
+ e.message !== CustomError.EXPORT_STOPPED
+ ) {
+ log.error("fileTrasher failed", e);
+ }
+ throw e;
+ }
+ }
+
+ async addFileExportedRecord(
+ folder: string,
+ fileUID: string,
+ fileExportName: string,
+ ) {
+ try {
+ const exportRecord = await this.getExportRecord(folder);
+ if (!exportRecord.fileExportNames) {
+ exportRecord.fileExportNames = {};
+ }
+ exportRecord.fileExportNames = {
+ ...exportRecord.fileExportNames,
+ [fileUID]: fileExportName,
+ };
+ await this.updateExportRecord(folder, exportRecord);
+ } catch (e) {
+ // @ts-ignore
+ if (e.message !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST) {
+ log.error("addFileExportedRecord failed", e);
+ }
+ throw e;
+ }
+ }
+
+ async addCollectionExportedRecord(
+ folder: string,
+ collectionID: number,
+ collectionExportName: string,
+ ) {
+ try {
+ const exportRecord = await this.getExportRecord(folder);
+ if (!exportRecord?.collectionExportNames) {
+ exportRecord.collectionExportNames = {};
+ }
+ exportRecord.collectionExportNames = {
+ ...exportRecord.collectionExportNames,
+ [collectionID]: collectionExportName,
+ };
+
+ await this.updateExportRecord(folder, exportRecord);
+ } catch (e) {
+ // @ts-ignore
+ if (e.message !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST) {
+ log.error("addCollectionExportedRecord failed", e);
+ }
+ throw e;
+ }
+ }
+
+ async removeCollectionExportedRecord(folder: string, collectionID: number) {
+ try {
+ const exportRecord = await this.getExportRecord(folder);
+
+ exportRecord.collectionExportNames = Object.fromEntries(
+ Object.entries(exportRecord.collectionExportNames).filter(
+ ([key]) => key !== collectionID.toString(),
+ ),
+ );
+
+ await this.updateExportRecord(folder, exportRecord);
+ } catch (e) {
+ // @ts-ignore
+ if (e.message !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST) {
+ log.error("removeCollectionExportedRecord failed", e);
+ }
+ throw e;
+ }
+ }
+
+ async removeFileExportedRecord(folder: string, fileUID: string) {
+ try {
+ const exportRecord = await this.getExportRecord(folder);
+ exportRecord.fileExportNames = Object.fromEntries(
+ Object.entries(exportRecord.fileExportNames).filter(
+ ([key]) => key !== fileUID,
+ ),
+ );
+ await this.updateExportRecord(folder, exportRecord);
+ } catch (e) {
+ // @ts-ignore
+ if (e.message !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST) {
+ log.error("removeFileExportedRecord failed", e);
+ }
+ throw e;
+ }
+ }
+
+ async updateExportRecord(folder: string, newData: Partial<ExportRecord>) {
+ return this.exportRecordUpdater.add(() =>
+ this.updateExportRecordHelper(folder, newData),
+ );
+ }
+
+ async updateExportRecordHelper(
+ folder: string,
+ newData: Partial<ExportRecord>,
+ ) {
+ try {
+ const exportRecord = await this.getExportRecord(folder);
+ const newRecord: ExportRecord = { ...exportRecord, ...newData };
+ await ensureElectron().fs.writeFileViaBackup(
+ joinPath(folder, exportRecordFileName),
+ JSON.stringify(newRecord, null, 2),
+ );
+ return newRecord;
+ } catch (e) {
+ // @ts-ignore
+ if (e.message === CustomError.EXPORT_FOLDER_DOES_NOT_EXIST) {
+ throw e;
+ }
+ log.error("error updating Export Record", e);
+ throw Error(CustomError.UPDATE_EXPORTED_RECORD_FAILED);
+ }
+ }
+
+ async getExportRecord(folder: string): Promise<ExportRecord> {
+ const electron = ensureElectron();
+ const fs = electron.fs;
+ try {
+ await this.verifyExportFolderExists(folder);
+ 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);
+ } catch (e) {
+ // @ts-ignore
+ if (e.message !== CustomError.EXPORT_FOLDER_DOES_NOT_EXIST) {
+ log.error("export Record JSON parsing failed", e);
+ }
+ throw e;
+ }
+ }
+
+ async createNewCollectionExport(
+ exportFolder: string,
+ collectionID: number,
+ collectionIDNameMap: Map<number, string>,
+ ) {
+ const fs = ensureElectron().fs;
+ await this.verifyExportFolderExists(exportFolder);
+ const collectionName = collectionIDNameMap.get(collectionID);
+ const collectionExportName = await safeDirectoryName(
+ exportFolder,
+ // @ts-ignore
+ collectionName,
+ fs.exists,
+ );
+ const collectionExportPath = joinPath(
+ exportFolder,
+ collectionExportName,
+ );
+ await fs.mkdirIfNeeded(collectionExportPath);
+ await fs.mkdirIfNeeded(
+ getMetadataFolderExportPath(collectionExportPath),
+ );
+
+ return collectionExportName;
+ }
+
+ async downloadAndSave(
+ exportDir: string,
+ collectionExportPath: string,
+ file: EnteFile,
+ ): Promise<void> {
+ const electron = ensureElectron();
+ try {
+ const fileUID = getExportRecordFileUID(file);
+ const originalFileStream = await downloadManager.fileStream(file);
+ if (file.metadata.fileType === FileType.livePhoto) {
+ await this.exportLivePhoto(
+ exportDir,
+ fileUID,
+ collectionExportPath,
+ // @ts-ignore
+ originalFileStream,
+ file,
+ );
+ } else {
+ const fileExportName = await safeFileName(
+ collectionExportPath,
+ file.metadata.title,
+ electron.fs.exists,
+ );
+ await this.saveMetadataFile(
+ collectionExportPath,
+ fileExportName,
+ file,
+ );
+ await writeStream(
+ electron,
+ joinPath(collectionExportPath, fileExportName),
+ // @ts-ignore
+ originalFileStream,
+ );
+ await this.addFileExportedRecord(
+ exportDir,
+ fileUID,
+ fileExportName,
+ );
+ }
+ } catch (e) {
+ log.error("download and save failed", e);
+ throw e;
+ }
+ }
+
+ private async exportLivePhoto(
+ exportDir: string,
+ fileUID: string,
+ collectionExportPath: string,
+ fileStream: ReadableStream,
+ file: EnteFile,
+ ) {
+ const fs = ensureElectron().fs;
+ const fileBlob = await new Response(fileStream).blob();
+ const livePhoto = await decodeLivePhoto(file.metadata.title, fileBlob);
+ const imageExportName = await safeFileName(
+ collectionExportPath,
+ livePhoto.imageFileName,
+ fs.exists,
+ );
+ const videoExportName = await safeFileName(
+ collectionExportPath,
+ livePhoto.videoFileName,
+ fs.exists,
+ );
+
+ const livePhotoExportName = getLivePhotoExportName(
+ imageExportName,
+ videoExportName,
+ );
+
+ await this.saveMetadataFile(
+ collectionExportPath,
+ imageExportName,
+ file,
+ );
+ await writeStream(
+ // @ts-ignore
+ electron,
+ joinPath(collectionExportPath, imageExportName),
+ new Response(livePhoto.imageData).body,
+ );
+
+ await this.saveMetadataFile(
+ collectionExportPath,
+ videoExportName,
+ file,
+ );
+ try {
+ await writeStream(
+ // @ts-ignore
+ electron,
+ joinPath(collectionExportPath, videoExportName),
+ new Response(livePhoto.videoData).body,
+ );
+ } catch (e) {
+ await fs.rm(joinPath(collectionExportPath, imageExportName));
+ throw e;
+ }
+
+ await this.addFileExportedRecord(
+ exportDir,
+ fileUID,
+ livePhotoExportName,
+ );
+ }
+
+ private async saveMetadataFile(
+ collectionExportPath: string,
+ fileExportName: string,
+ file: EnteFile,
+ ) {
+ const formatter = this.metadataDateTimeFormatter();
+ await ensureElectron().fs.writeFile(
+ getFileMetadataExportPath(collectionExportPath, fileExportName),
+ getGoogleLikeMetadataFile(fileExportName, file, formatter),
+ );
+ }
+
+ /**
+ * Lazily created, cached instance of the date time formatter that should be
+ * used for formatting the dates added to the metadata file.
+ */
+ private metadataDateTimeFormatter() {
+ if (this.cachedMetadataDateTimeFormatter)
+ return this.cachedMetadataDateTimeFormatter;
+
+ // AFAIK, Google's format is not documented. It also seems to vary with
+ // locale. This is a best attempt at constructing a formatter that
+ // mirrors the format used by the timestamps in the takeout JSON.
+ const formatter = new Intl.DateTimeFormat(i18n.language, {
+ month: "short",
+ day: "numeric",
+ year: "numeric",
+ hour: "numeric",
+ minute: "numeric",
+ second: "numeric",
+ timeZoneName: "short",
+ timeZone: "UTC",
+ });
+ this.cachedMetadataDateTimeFormatter = formatter;
+ return formatter;
+ }
+
+ isExportInProgress = () => {
+ return this.exportInProgress;
+ };
+
+ exportFolderExists = async (exportFolder: string) => {
+ return exportFolder && (await ensureElectron().fs.exists(exportFolder));
+ };
+
+ private verifyExportFolderExists = async (exportFolder: string) => {
+ 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) {
+ log.error("verifyExportFolderExists failed", e);
+ }
+ throw e;
+ }
+ };
+
+ private createEmptyExportRecord = async (exportRecordJSONPath: string) => {
+ const exportRecord: ExportRecord = NULL_EXPORT_RECORD;
+ await ensureElectron().fs.writeFile(
+ exportRecordJSONPath,
+ JSON.stringify(exportRecord, null, 2),
+ );
+ return exportRecord;
+ };
+}
+
+const exportService = new ExportService();
+
+export default exportService;
+
+/**
+ * If there are any in-progress exports, or if continuous exports are enabled,
+ * resume them.
+ */
+export const resumeExportsIfNeeded = async () => {
+ const exportSettings = exportService.getExportSettings();
+ if (!(await exportService.exportFolderExists(exportSettings?.folder))) {
+ return;
+ }
+ const exportRecord = await exportService.getExportRecord(
+ exportSettings.folder,
+ );
+ if (exportSettings.continuousExport) {
+ exportService.enableContinuousExport();
+ }
+ if (isExportInProgress(exportRecord.stage)) {
+ log.debug(() => "Resuming in-progress export");
+ exportService.resumeExport();
+ }
+};
+
+/**
+ * Prompt the user to select a directory and create an export directory in it.
+ *
+ * If the user cancels the selection, return undefined.
+ */
+export const selectAndPrepareExportDirectory = async (): Promise<
+ string | undefined
+> => {
+ const electron = ensureElectron();
+
+ const rootDir = await electron.selectDirectory();
+ if (!rootDir) return undefined;
+
+ const exportDir = joinPath(rootDir, exportDirectoryName);
+ await electron.fs.mkdirIfNeeded(exportDir);
+ return exportDir;
+};
+
+export const getExportRecordFileUID = (file: EnteFile) =>
+ `${file.id}_${file.collectionID}_${file.updationTime}`;
+
+export const getCollectionIDFromFileUID = (fileUID: string) =>
+ Number(fileUID.split("_")[1]);
+
+const convertCollectionIDExportNameObjectToMap = (
+ collectionExportNames: CollectionExportNames,
+): Map<number, string> => {
+ return new Map<number, string>(
+ Object.entries(collectionExportNames ?? {}).map((e) => {
+ return [Number(e[0]), String(e[1])];
+ }),
+ );
+};
+
+const convertFileIDExportNameObjectToMap = (
+ fileExportNames: FileExportNames,
+): Map<string, string> => {
+ return new Map<string, string>(
+ Object.entries(fileExportNames ?? {}).map((e) => {
+ return [String(e[0]), String(e[1])];
+ }),
+ );
+};
+
+const getRenamedExportedCollections = (
+ collections: Collection[],
+ exportRecord: ExportRecord,
+) => {
+ if (!exportRecord?.collectionExportNames) {
+ return [];
+ }
+ const collectionIDExportNameMap = convertCollectionIDExportNameObjectToMap(
+ exportRecord.collectionExportNames,
+ );
+ const renamedCollections = collections.filter((collection) => {
+ if (collectionIDExportNameMap.has(collection.id)) {
+ const currentExportName = collectionIDExportNameMap.get(
+ collection.id,
+ );
+
+ const collectionExportName =
+ getCollectionUserFacingName(collection);
+
+ if (currentExportName === collectionExportName) {
+ return false;
+ }
+ // @ts-ignore
+ const hasNumberedSuffix = /\(\d+\)$/.exec(currentExportName);
+ const currentExportNameWithoutNumberedSuffix = hasNumberedSuffix
+ ? // @ts-ignore
+ currentExportName.replace(/\(\d+\)$/, "")
+ : currentExportName;
+
+ return (
+ collectionExportName !== currentExportNameWithoutNumberedSuffix
+ );
+ }
+ return false;
+ });
+ return renamedCollections;
+};
+
+const getDeletedExportedCollections = (
+ collections: Collection[],
+ exportRecord: ExportRecord,
+) => {
+ if (!exportRecord?.collectionExportNames) {
+ return [];
+ }
+ const presentCollections = new Set(
+ collections.map((collection) => collection.id),
+ );
+ const deletedExportedCollections = Object.keys(
+ exportRecord?.collectionExportNames,
+ )
+ .map(Number)
+ .filter((collectionID) => {
+ if (!presentCollections.has(collectionID)) {
+ return true;
+ }
+ return false;
+ });
+ return deletedExportedCollections;
+};
+
+/**
+ * Return export record IDs of {@link files} for which there is also exists a
+ * file on disk.
+ */
+const readOnDiskFileExportRecordIDs = async (
+ files: EnteFile[],
+ collectionIDFolderNameMap: Map<number, string>,
+ exportDir: string,
+ exportRecord: ExportRecord,
+ isCanceled: CancellationStatus,
+): Promise<Set<string>> => {
+ const fs = ensureElectron().fs;
+
+ const result = new Set<string>();
+ if (!(await fs.exists(exportDir))) return result;
+
+ // Both the paths involved are guaranteed to use POSIX separators and thus
+ // can directly be compared.
+ //
+ // - `exportDir` traces its origin to `electron.selectDirectory()`, which
+ // returns POSIX paths. Down below we use it as the base directory when
+ // constructing paths for the items to export.
+ //
+ // - `findFiles` is also guaranteed to return POSIX paths.
+ //
+ const ls = new Set(await ensureElectron().fs.findFiles(exportDir));
+
+ const fileExportNames = exportRecord.fileExportNames ?? {};
+
+ for (const file of files) {
+ if (isCanceled.status) throw Error(CustomError.EXPORT_STOPPED);
+
+ const collectionExportName = collectionIDFolderNameMap.get(
+ file.collectionID,
+ );
+ if (!collectionExportName) continue;
+
+ const collectionExportPath = joinPath(exportDir, collectionExportName);
+ const recordID = getExportRecordFileUID(file);
+ const exportName = fileExportNames[recordID];
+ if (!exportName) continue;
+
+ if (ls.has(joinPath(collectionExportPath, exportName))) {
+ result.add(recordID);
+ } else {
+ // It might be a live photo - these store a JSON string instead of
+ // the file's name as the exportName.
+ try {
+ const { image, video } = parseLivePhotoExportName(exportName);
+ if (
+ ls.has(joinPath(collectionExportPath, image)) &&
+ ls.has(joinPath(collectionExportPath, video))
+ ) {
+ result.add(recordID);
+ }
+ } catch {
+ /* Not an error, the file just might not exist on disk yet */
+ }
+ }
+ }
+
+ return result;
+};
+
+/**
+ * Return the list of files from amongst {@link allFiles} that still need to be
+ * exported.
+ *
+ * @param allFiles The list of files to export.
+ *
+ * @param exportRecord The export record containing bookkeeping for the export.
+ *
+ * @paramd diskFileRecordIDs (Optional) The export record IDs of files from
+ * amongst {@link allFiles} that already exist on disk. If provided (e.g. when
+ * doing a resync), we perform an extra check for on-disk existence instead of
+ * relying solely on the export record.
+ */
+const getUnExportedFiles = (
+ allFiles: EnteFile[],
+ exportRecord: ExportRecord | undefined,
+ diskFileRecordIDs: Set<string> | undefined,
+) => {
+ if (!exportRecord?.fileExportNames) {
+ return allFiles;
+ }
+ const exportedFiles = new Set(Object.keys(exportRecord?.fileExportNames));
+ return allFiles.filter((file) => {
+ const recordID = getExportRecordFileUID(file);
+ if (!exportedFiles.has(recordID)) return true;
+ if (diskFileRecordIDs && !diskFileRecordIDs.has(recordID)) return true;
+ return false;
+ });
+};
+
+const getDeletedExportedFiles = (
+ allFiles: EnteFile[],
+ exportRecord: ExportRecord,
+): string[] => {
+ if (!exportRecord?.fileExportNames) {
+ return [];
+ }
+ const presentFileUIDs = new Set(
+ allFiles?.map((file) => getExportRecordFileUID(file)),
+ );
+ const deletedExportedFiles = Object.keys(
+ exportRecord?.fileExportNames,
+ ).filter((fileUID) => {
+ if (!presentFileUIDs.has(fileUID)) {
+ return true;
+ }
+ return false;
+ });
+ return deletedExportedFiles;
+};
+
+const getCollectionExportedFiles = (
+ exportRecord: ExportRecord,
+ collectionID: number,
+): string[] => {
+ if (!exportRecord?.fileExportNames) {
+ return [];
+ }
+ const collectionExportedFiles = Object.keys(
+ exportRecord?.fileExportNames,
+ ).filter((fileUID) => {
+ const fileCollectionID = Number(fileUID.split("_")[1]);
+ if (fileCollectionID === collectionID) {
+ return true;
+ } else {
+ return false;
+ }
+ });
+ return collectionExportedFiles;
+};
+
+const getGoogleLikeMetadataFile = (
+ fileExportName: string,
+ file: EnteFile,
+ dateTimeFormatter: Intl.DateTimeFormat,
+) => {
+ const metadata: Metadata = file.metadata;
+ const publicMagicMetadata = file.pubMagicMetadata?.data;
+ const creationTime = Math.floor(
+ (publicMagicMetadata?.editedTime ?? metadata.creationTime) / 1e6,
+ );
+ const modificationTime = Math.floor(
+ (metadata.modificationTime ?? metadata.creationTime) / 1e6,
+ );
+ const result: Record<string, unknown> = {
+ title: fileExportName,
+ creationTime: {
+ timestamp: `${creationTime}`,
+ formatted: dateTimeFormatter.format(creationTime * 1000),
+ },
+ modificationTime: {
+ timestamp: `${modificationTime}`,
+ formatted: dateTimeFormatter.format(modificationTime * 1000),
+ },
+ };
+ const caption = file?.pubMagicMetadata?.data?.caption;
+ if (caption) result.caption = caption;
+ const geoData = fileLocation(file);
+ if (geoData) result.geoData = geoData;
+ return JSON.stringify(result, null, 2);
+};
+
+export const getMetadataFolderExportPath = (collectionExportPath: string) =>
+ joinPath(collectionExportPath, exportMetadataDirectoryName);
+
+// if filepath is /home/user/Ente/Export/Collection1/1.jpg
+// then metadata path is /home/user/Ente/Export/Collection1/ENTE_METADATA_FOLDER/1.jpg.json
+const getFileMetadataExportPath = (
+ collectionExportPath: string,
+ fileExportName: string,
+) =>
+ joinPath(
+ collectionExportPath,
+ joinPath(exportMetadataDirectoryName, `${fileExportName}.json`),
+ );
+
+export const getLivePhotoExportName = (
+ imageExportName: string,
+ videoExportName: string,
+) => JSON.stringify({ image: imageExportName, video: videoExportName });
+
+export const isLivePhotoExportName = (exportName: string) => {
+ try {
+ JSON.parse(exportName);
+ return true;
+ } catch {
+ return false;
+ }
+};
+
+const parseLivePhotoExportName = (
+ livePhotoExportName: string,
+): { image: string; video: string } => {
+ const { image, video } = JSON.parse(livePhotoExportName);
+ return { image, video };
+};
+
+const isExportInProgress = (exportStage: ExportStage) =>
+ exportStage > ExportStage.init && exportStage < ExportStage.finished;
+
+/**
+ * Move {@link fileName} in {@link collectionName} to the special per-collection
+ * file system "Trash" folder we created under the export directory.
+ *
+ * Also move its associated metadata JSON to Trash.
+ *
+ * @param exportDir The root directory on the user's file system where we are
+ * exporting to.
+ * */
+const moveToFSTrash = async (
+ exportDir: string,
+ collectionName: string,
+ fileName: string,
+) => {
+ const fs = ensureElectron().fs;
+
+ const filePath = joinPath(exportDir, joinPath(collectionName, fileName));
+ const trashDir = joinPath(
+ exportDir,
+ joinPath(exportTrashDirectoryName, collectionName),
+ );
+ const metadataFileName = `${fileName}.json`;
+ const metadataFilePath = joinPath(
+ exportDir,
+ joinPath(
+ collectionName,
+ joinPath(exportMetadataDirectoryName, metadataFileName),
+ ),
+ );
+ const metadataTrashDir = joinPath(
+ exportDir,
+ joinPath(
+ exportTrashDirectoryName,
+ joinPath(collectionName, exportMetadataDirectoryName),
+ ),
+ );
+
+ log.info(`Moving file ${filePath} and its metadata to trash folder`);
+
+ if (await fs.exists(filePath)) {
+ await fs.mkdirIfNeeded(trashDir);
+ const trashFileName = await safeFileName(trashDir, fileName, fs.exists);
+ const trashFilePath = joinPath(trashDir, trashFileName);
+ await fs.rename(filePath, trashFilePath);
+ }
+
+ if (await fs.exists(metadataFilePath)) {
+ await fs.mkdirIfNeeded(metadataTrashDir);
+ const metadataTrashFileName = await safeFileName(
+ metadataTrashDir,
+ metadataFileName,
+ fs.exists,
+ );
+ const metadataTrashFilePath = joinPath(
+ metadataTrashDir,
+ metadataTrashFileName,
+ );
+ await fs.rename(metadataFilePath, metadataTrashFilePath);
+ }
+};