Skip to content

armgit / ente

Public Imported from GitHub
0

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

Move mkdirIfNeeded into fs

Manav Rathi7438c3301ebf
diff --git a/desktop/src/main/fs.ts b/desktop/src/main/fs.ts
index a9138471c0..a1b9c6f79b 100644
--- a/desktop/src/main/fs.ts
+++ b/desktop/src/main/fs.ts
@@ -11,6 +11,9 @@ export const fsExists = (path: string) => existsSync(path);
export const fsRename = (oldPath: string, newPath: string) =>
fs.rename(oldPath, newPath);
+export const fsMkdirIfNeeded = (dirPath: string) =>
+ fs.mkdir(dirPath, { recursive: true });
+
/**
* Write a (web) ReadableStream to a file at the given {@link filePath}.
*
@@ -76,9 +79,6 @@ const writeNodeStream = async (
/* TODO: Audit below this */
-export const checkExistsAndCreateDir = (dirPath: string) =>
- fs.mkdir(dirPath, { recursive: true });
-
export const saveStreamToDisk = writeStream;
export const saveFileToDisk = (path: string, contents: string) =>
diff --git a/desktop/src/main/ipc.ts b/desktop/src/main/ipc.ts
index 9addcc4eba..5c9d4a22e8 100644
--- a/desktop/src/main/ipc.ts
+++ b/desktop/src/main/ipc.ts
@@ -18,10 +18,10 @@ import {
showUploadZipDialog,
} from "./dialogs";
import {
- checkExistsAndCreateDir,
deleteFile,
deleteFolder,
fsExists,
+ fsMkdirIfNeeded,
fsRename,
isFolder,
moveFile,
@@ -173,11 +173,9 @@ export const attachIPCHandlers = () => {
fsRename(oldPath, newPath),
);
- // - FS Legacy
+ ipcMain.handle("fsMkdirIfNeeded", (_, dirPath) => fsMkdirIfNeeded(dirPath));
- ipcMain.handle("checkExistsAndCreateDir", (_, dirPath) =>
- checkExistsAndCreateDir(dirPath),
- );
+ // - FS Legacy
ipcMain.handle(
"saveStreamToDisk",
diff --git a/desktop/src/preload.ts b/desktop/src/preload.ts
index 2ea6a81bd7..550bd6f632 100644
--- a/desktop/src/preload.ts
+++ b/desktop/src/preload.ts
@@ -99,6 +99,9 @@ const skipAppUpdate = (version: string) => {
const fsExists = (path: string): Promise<boolean> =>
ipcRenderer.invoke("fsExists", path);
+const fsMkdirIfNeeded = (dirPath: string): Promise<void> =>
+ ipcRenderer.invoke("fsMkdirIfNeeded", dirPath);
+
const fsRename = (oldPath: string, newPath: string): Promise<void> =>
ipcRenderer.invoke("fsRename", oldPath, newPath);
@@ -221,9 +224,6 @@ const updateWatchMappingIgnoredFiles = (
// - FS Legacy
-const checkExistsAndCreateDir = (dirPath: string): Promise<void> =>
- ipcRenderer.invoke("checkExistsAndCreateDir", dirPath);
-
const saveStreamToDisk = (
path: string,
fileStream: ReadableStream,
@@ -349,11 +349,11 @@ contextBridge.exposeInMainWorld("electron", {
fs: {
exists: fsExists,
rename: fsRename,
+ mkdirIfNeeded: fsMkdirIfNeeded,
},
// - FS legacy
// TODO: Move these into fs + document + rename if needed
- checkExistsAndCreateDir,
saveStreamToDisk,
saveFileToDisk,
readTextFile,
diff --git a/web/apps/photos/src/services/export/index.ts b/web/apps/photos/src/services/export/index.ts
index 2f43d43706..4d7a2ed311 100644
--- a/web/apps/photos/src/services/export/index.ts
+++ b/web/apps/photos/src/services/export/index.ts
@@ -166,13 +166,14 @@ class ExportService {
}
async changeExportDirectory() {
+ const electron = ensureElectron();
try {
- const newRootDir = await ensureElectron().selectDirectory();
+ const newRootDir = await electron.selectDirectory();
if (!newRootDir) {
throw Error(CustomError.SELECT_FOLDER_ABORTED);
}
const newExportDir = `${newRootDir}/${exportDirectoryName}`;
- await ensureElectron().checkExistsAndCreateDir(newExportDir);
+ await electron.fs.mkdirIfNeeded(newExportDir);
return newExportDir;
} catch (e) {
if (e.message !== CustomError.SELECT_FOLDER_ABORTED) {
@@ -648,6 +649,7 @@ class ExportService {
incrementFailed: () => void,
isCanceled: CancellationStatus,
): Promise<void> {
+ const fs = ensureElectron().fs;
try {
for (const file of files) {
log.info(
@@ -683,10 +685,8 @@ class ExportService {
);
}
const collectionExportPath = `${exportDir}/${collectionExportName}`;
- await ensureElectron().checkExistsAndCreateDir(
- collectionExportPath,
- );
- await ensureElectron().checkExistsAndCreateDir(
+ await fs.mkdirIfNeeded(collectionExportPath);
+ await fs.mkdirIfNeeded(
getMetadataFolderExportPath(collectionExportPath),
);
await this.downloadAndSave(
@@ -1019,17 +1019,17 @@ class ExportService {
collectionID: number,
collectionIDNameMap: Map<number, string>,
) {
- const electron = ensureElectron();
+ const fs = ensureElectron().fs;
await this.verifyExportFolderExists(exportFolder);
const collectionName = collectionIDNameMap.get(collectionID);
const collectionExportName = await safeDirectoryName(
exportFolder,
collectionName,
- electron.fs.exists,
+ fs.exists,
);
const collectionExportPath = `${exportFolder}/${collectionExportName}`;
- await electron.checkExistsAndCreateDir(collectionExportPath);
- await electron.checkExistsAndCreateDir(
+ await fs.mkdirIfNeeded(collectionExportPath);
+ await fs.mkdirIfNeeded(
getMetadataFolderExportPath(collectionExportPath),
);
@@ -1172,14 +1172,6 @@ class ExportService {
return this.exportInProgress;
};
- rename = (oldPath: string, newPath: string) => {
- return ensureElectron().fs.rename(oldPath, newPath);
- };
-
- checkExistsAndCreateDir = (path: string) => {
- return ensureElectron().checkExistsAndCreateDir(path);
- };
-
exportFolderExists = async (exportFolder: string) => {
return exportFolder && (await ensureElectron().fs.exists(exportFolder));
};
diff --git a/web/apps/photos/src/utils/collection/index.ts b/web/apps/photos/src/utils/collection/index.ts
index be72a7d0d2..9b92d794ad 100644
--- a/web/apps/photos/src/utils/collection/index.ts
+++ b/web/apps/photos/src/utils/collection/index.ts
@@ -31,7 +31,6 @@ import {
updatePublicCollectionMagicMetadata,
updateSharedCollectionMagicMetadata,
} from "services/collectionService";
-import exportService from "services/export";
import { getAllLocalFiles, getLocalFiles } from "services/fileService";
import {
COLLECTION_ROLE,
@@ -170,13 +169,14 @@ async function createCollectionDownloadFolder(
downloadDirPath: string,
collectionName: string,
) {
+ const fs = ensureElectron().fs;
const collectionDownloadName = await safeDirectoryName(
downloadDirPath,
collectionName,
- ensureElectron().fs.exists,
+ fs.exists,
);
const collectionDownloadPath = `${downloadDirPath}/${collectionDownloadName}`;
- await exportService.checkExistsAndCreateDir(collectionDownloadPath);
+ await fs.mkdirIfNeeded(collectionDownloadPath);
return collectionDownloadPath;
}
diff --git a/web/packages/next/types/ipc.ts b/web/packages/next/types/ipc.ts
index 534184ef4e..1eb53e1156 100644
--- a/web/packages/next/types/ipc.ts
+++ b/web/packages/next/types/ipc.ts
@@ -161,6 +161,17 @@ export interface Electron {
fs: {
/** Return true if there is an item at the given {@link path}. */
exists: (path: string) => Promise<boolean>;
+
+ /**
+ * mkdir -p
+ *
+ * Create a directory at the given path if it does not already exist.
+ * Any parent directories in the path that don't already exist will also
+ * be created recursively, i.e. this command is analogous to an running
+ * `mkdir -p`.
+ */
+ mkdirIfNeeded: (dirPath: string) => Promise<void>;
+
/** Rename {@link oldPath} to {@link newPath} */
rename: (oldPath: string, newPath: string) => Promise<void>;
};
@@ -275,7 +286,6 @@ export interface Electron {
) => Promise<void>;
// - FS legacy
- checkExistsAndCreateDir: (dirPath: string) => Promise<void>;
saveStreamToDisk: (
path: string,
fileStream: ReadableStream,