Skip to content

armgit / ente

Public Imported from GitHub
0

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

Cleanup and document

Manav Rathi82bbb1c6dd69
diff --git a/web/apps/photos/src/services/export/index.ts b/web/apps/photos/src/services/export/index.ts
index c53448885b..07182c7b23 100644
--- a/web/apps/photos/src/services/export/index.ts
+++ b/web/apps/photos/src/services/export/index.ts
@@ -34,22 +34,33 @@ import {
mergeMetadata,
splitFilenameAndExtension,
} from "utils/file";
-import {
- ENTE_TRASH_FOLDER,
- sanitizedUniqueFileName,
- santizedUniqueDirectoryName,
-} from "utils/native-fs";
+import { safeDirectoryName, sanitizedUniqueFileName } from "utils/native-fs";
import { getAllLocalCollections } from "../collectionService";
import downloadManager from "../download";
import { getAllLocalFiles } from "../fileService";
import { decodeLivePhoto } from "../livePhotoService";
import { migrateExport } from "./migration";
-const EXPORT_RECORD_FILE_NAME = "export_status.json";
+/** Name of the JSON file in which we keep the state of the export. */
+const exportRecordFileName = "export_status.json";
-export const ENTE_EXPORT_DIRECTORY = "ente Photos";
+/**
+ * Name of the top level directory which we create underneath the selected
+ * directory when the user starts an export to the filesystem.
+ */
+const exportDirectoryName = "ente Photos";
-export const ENTE_METADATA_FOLDER = "metadata";
+/**
+ * Name of the directory in which we put our metadata when exporting to the
+ * filesystem.
+ */
+export const exportMetadataDirectoryName = "metadata";
+
+/**
+ * Name of the directory in which we keep trash items when deleting files that
+ * have been exported to the local disk previously.
+ */
+export const exportTrashDirectoryName = "Trash";
export enum ExportStage {
INIT = 0,
@@ -160,7 +171,7 @@ class ExportService {
if (!newRootDir) {
throw Error(CustomError.SELECT_FOLDER_ABORTED);
}
- const newExportDir = `${newRootDir}/${ENTE_EXPORT_DIRECTORY}`;
+ const newExportDir = `${newRootDir}/${exportDirectoryName}`;
await ensureElectron().checkExistsAndCreateDir(newExportDir);
return newExportDir;
} catch (e) {
@@ -484,11 +495,10 @@ class ExportService {
const oldCollectionExportName =
collectionIDExportNameMap.get(collection.id);
const oldCollectionExportPath = `${exportFolder}/${oldCollectionExportName}`;
- const newCollectionExportName =
- await santizedUniqueDirectoryName(
- exportFolder,
- getCollectionUserFacingName(collection),
- );
+ const newCollectionExportName = await safeDirectoryName(
+ exportFolder,
+ getCollectionUserFacingName(collection),
+ );
log.info(
`renaming collection with id ${collection.id} from ${oldCollectionExportName} to ${newCollectionExportName}`,
);
@@ -960,7 +970,7 @@ class ExportService {
const exportRecord = await this.getExportRecord(folder);
const newRecord: ExportRecord = { ...exportRecord, ...newData };
await ensureElectron().saveFileToDisk(
- `${folder}/${EXPORT_RECORD_FILE_NAME}`,
+ `${folder}/${exportRecordFileName}`,
JSON.stringify(newRecord, null, 2),
);
return newRecord;
@@ -976,7 +986,7 @@ class ExportService {
async getExportRecord(folder: string, retry = true): Promise<ExportRecord> {
try {
await this.verifyExportFolderExists(folder);
- const exportRecordJSONPath = `${folder}/${EXPORT_RECORD_FILE_NAME}`;
+ const exportRecordJSONPath = `${folder}/${exportRecordFileName}`;
if (!(await this.exists(exportRecordJSONPath))) {
return this.createEmptyExportRecord(exportRecordJSONPath);
}
@@ -1009,7 +1019,7 @@ class ExportService {
) {
await this.verifyExportFolderExists(exportFolder);
const collectionName = collectionIDNameMap.get(collectionID);
- const collectionExportName = await santizedUniqueDirectoryName(
+ const collectionExportName = await safeDirectoryName(
exportFolder,
collectionName,
);
@@ -1390,16 +1400,17 @@ const getGoogleLikeMetadataFile = (fileExportName: string, file: EnteFile) => {
};
export const getMetadataFolderExportPath = (collectionExportPath: string) =>
- `${collectionExportPath}/${ENTE_METADATA_FOLDER}`;
+ `${collectionExportPath}/${exportMetadataDirectoryName}`;
const getFileMetadataExportPath = (
collectionExportPath: string,
fileExportName: string,
-) => `${collectionExportPath}/${ENTE_METADATA_FOLDER}/${fileExportName}.json`;
+) =>
+ `${collectionExportPath}/${exportMetadataDirectoryName}/${fileExportName}.json`;
const getTrashedFileExportPath = async (exportDir: string, path: string) => {
const fileRelativePath = path.replace(`${exportDir}/`, "");
- let trashedFilePath = `${exportDir}/${ENTE_TRASH_FOLDER}/${fileRelativePath}`;
+ let trashedFilePath = `${exportDir}/${exportTrashDirectoryName}/${fileRelativePath}`;
let count = 1;
while (await exportService.exists(trashedFilePath)) {
const trashedFilePathParts = splitFilenameAndExtension(trashedFilePath);
@@ -1419,7 +1430,7 @@ const getMetadataFileExportPath = (filePath: string) => {
// extract filename and collection folder path
const filename = filePath.split("/").pop();
const collectionExportPath = filePath.replace(`/${filename}`, "");
- return `${collectionExportPath}/${ENTE_METADATA_FOLDER}/${filename}.json`;
+ return `${collectionExportPath}/${exportMetadataDirectoryName}/${filename}.json`;
};
export const getLivePhotoExportName = (
diff --git a/web/apps/photos/src/services/export/migration.ts b/web/apps/photos/src/services/export/migration.ts
index cb785749d3..c89b34d408 100644
--- a/web/apps/photos/src/services/export/migration.ts
+++ b/web/apps/photos/src/services/export/migration.ts
@@ -28,7 +28,7 @@ import {
} from "utils/file";
import { sanitizeFilename } from "utils/native-fs";
import {
- ENTE_METADATA_FOLDER,
+ exportMetadataDirectoryName,
getCollectionIDFromFileUID,
getExportRecordFileUID,
getLivePhotoExportName,
@@ -514,7 +514,7 @@ const getUniqueCollectionFolderPath = async (
};
export const getMetadataFolderPath = (collectionFolderPath: string) =>
- `${collectionFolderPath}/${ENTE_METADATA_FOLDER}`;
+ `${collectionFolderPath}/${exportMetadataDirectoryName}`;
const getUniqueFileSaveName = async (
collectionPath: string,
@@ -543,7 +543,8 @@ const getUniqueFileSaveName = async (
const getFileMetadataSavePath = (
collectionFolderPath: string,
fileSaveName: string,
-) => `${collectionFolderPath}/${ENTE_METADATA_FOLDER}/${fileSaveName}.json`;
+) =>
+ `${collectionFolderPath}/${exportMetadataDirectoryName}/${fileSaveName}.json`;
const getFileSavePath = (collectionFolderPath: string, fileSaveName: string) =>
`${collectionFolderPath}/${fileSaveName}`;
@@ -563,7 +564,7 @@ const getOldFileMetadataSavePath = (
collectionFolderPath: string,
file: EnteFile,
) =>
- `${collectionFolderPath}/${ENTE_METADATA_FOLDER}/${
+ `${collectionFolderPath}/${exportMetadataDirectoryName}/${
file.id
}_${oldSanitizeName(file.metadata.title)}.json`;
diff --git a/web/apps/photos/src/utils/collection/index.ts b/web/apps/photos/src/utils/collection/index.ts
index 5b4525341c..b57f9799f3 100644
--- a/web/apps/photos/src/utils/collection/index.ts
+++ b/web/apps/photos/src/utils/collection/index.ts
@@ -44,7 +44,7 @@ import { SetFilesDownloadProgressAttributes } from "types/gallery";
import { SUB_TYPE, VISIBILITY_STATE } from "types/magicMetadata";
import { downloadFilesWithProgress } from "utils/file";
import { isArchivedCollection, updateMagicMetadata } from "utils/magicMetadata";
-import { santizedUniqueDirectoryName } from "utils/native-fs";
+import { safeDirectoryName } from "utils/native-fs";
export enum COLLECTION_OPS_TYPE {
ADD,
@@ -169,7 +169,7 @@ async function createCollectionDownloadFolder(
downloadDirPath: string,
collectionName: string,
) {
- const collectionDownloadName = await santizedUniqueDirectoryName(
+ const collectionDownloadName = await safeDirectoryName(
downloadDirPath,
collectionName,
);
diff --git a/web/apps/photos/src/utils/native-fs.ts b/web/apps/photos/src/utils/native-fs.ts
index b6c20594e8..eb1c97c59a 100644
--- a/web/apps/photos/src/utils/native-fs.ts
+++ b/web/apps/photos/src/utils/native-fs.ts
@@ -1,9 +1,8 @@
import { ensureElectron } from "@/next/electron";
import sanitize from "sanitize-filename";
+import { exportTrashDirectoryName } from "services/export";
import { splitFilenameAndExtension } from "utils/file";
-export const ENTE_TRASH_FOLDER = "Trash";
-
/**
* Sanitize string for use as file or directory name.
*
@@ -18,13 +17,16 @@ export const sanitizeFilename = (s: string) =>
* Return a new unique directory name based on {@link name} that is not the same
* as any existing directory in the given {@link directoryPath}.
*
+ * We also ensure we don't return names which might collide with our own special
+ * directories.
+ *
* This function only works when we are running inside an electron app (since it
* requires permissionless access to the native filesystem to find a new
* filename that doesn't conflict with any existing items).
*
* See also: {@link santizedUniqueFileName}
*/
-export const santizedUniqueDirectoryName = async (
+export const safeDirectoryName = async (
directoryPath: string,
name: string,
): Promise<string> => {
@@ -32,7 +34,7 @@ export const santizedUniqueDirectoryName = async (
let count = 1;
while (
(await exists(`${directoryPath}/${result}`)) ||
- result === ENTE_TRASH_FOLDER
+ result == exportTrashDirectoryName
) {
result = `${sanitizeFilename(name)}(${count})`;
count++;
@@ -45,7 +47,7 @@ export const santizedUniqueDirectoryName = async (
* any existing directory in the given {@link directoryPath}.
*
* This function only works when we are running inside an electron app.
- * @see {@link santizedUniqueDirectoryName}.
+ * @see {@link safeDirectoryName}.
*/
export const sanitizedUniqueFileName = async (
directoryPath: string,
diff --git a/web/apps/photos/src/utils/upload/index.ts b/web/apps/photos/src/utils/upload/index.ts
index 708ec5dcf7..643c931fe5 100644
--- a/web/apps/photos/src/utils/upload/index.ts
+++ b/web/apps/photos/src/utils/upload/index.ts
@@ -5,7 +5,7 @@ import {
PICKED_UPLOAD_TYPE,
} from "constants/upload";
import isElectron from "is-electron";
-import { ENTE_METADATA_FOLDER } from "services/export";
+import { exportMetadataDirectoryName } from "services/export";
import { EnteFile } from "types/file";
import {
ElectronFile,
@@ -175,7 +175,7 @@ export function groupFilesBasedOnParentFolder(
// For Eg,For FileList -> [a/x.png, a/metadata/x.png.json]
// they will both we grouped into the collection "a"
// This is cluster the metadata json files in the same collection as the file it is for
- if (folderPath.endsWith(ENTE_METADATA_FOLDER)) {
+ if (folderPath.endsWith(exportMetadataDirectoryName)) {
folderPath = folderPath.substring(0, folderPath.lastIndexOf("/"));
}
const folderName = folderPath.substring(