Просмотр исходного кода

Convert remaining upload functions

Manav Rathi 1 год назад
Родитель
Сommit
892bf852a5

+ 0 - 7
desktop/src/api/fs.ts

@@ -1,7 +0,0 @@
-import { getDirFilePaths, getElectronFile } from "../services/fs";
-
-export async function getDirFiles(dirPath: string) {
-    const files = await getDirFilePaths(dirPath);
-    const electronFiles = await Promise.all(files.map(getElectronFile));
-    return electronFiles;
-}

+ 31 - 1
desktop/src/main/ipc.ts

@@ -10,6 +10,12 @@ import type { FSWatcher } from "chokidar";
 import { ipcMain } from "electron/main";
 import { ipcMain } from "electron/main";
 import { clearElectronStore } from "../api/electronStore";
 import { clearElectronStore } from "../api/electronStore";
 import { getEncryptionKey, setEncryptionKey } from "../api/safeStorage";
 import { getEncryptionKey, setEncryptionKey } from "../api/safeStorage";
+import {
+    getElectronFilesFromGoogleZip,
+    getPendingUploads,
+    setToUploadCollection,
+    setToUploadFiles,
+} from "../api/upload";
 import {
 import {
     appVersion,
     appVersion,
     muteUpdateNotification,
     muteUpdateNotification,
@@ -21,6 +27,7 @@ import {
     computeTextEmbedding,
     computeTextEmbedding,
 } from "../services/clipService";
 } from "../services/clipService";
 import { runFFmpegCmd } from "../services/ffmpeg";
 import { runFFmpegCmd } from "../services/ffmpeg";
+import { getDirFiles } from "../services/fs";
 import {
 import {
     convertToJPEG,
     convertToJPEG,
     generateImageThumbnail,
     generateImageThumbnail,
@@ -32,7 +39,12 @@ import {
     updateWatchMappingIgnoredFiles,
     updateWatchMappingIgnoredFiles,
     updateWatchMappingSyncedFiles,
     updateWatchMappingSyncedFiles,
 } from "../services/watch";
 } from "../services/watch";
-import type { ElectronFile, Model, WatchMapping } from "../types";
+import type {
+    ElectronFile,
+    FILE_PATH_TYPE,
+    Model,
+    WatchMapping,
+} from "../types";
 import {
 import {
     selectDirectory,
     selectDirectory,
     showUploadDirsDialog,
     showUploadDirsDialog,
@@ -174,6 +186,24 @@ export const attachIPCHandlers = () => {
     ipcMain.handle("rename", (_, oldPath: string, newPath: string) =>
     ipcMain.handle("rename", (_, oldPath: string, newPath: string) =>
         rename(oldPath, newPath),
         rename(oldPath, newPath),
     );
     );
+
+    ipcMain.handle("getPendingUploads", (_) => getPendingUploads());
+
+    ipcMain.handle(
+        "setToUploadFiles",
+        (_, type: FILE_PATH_TYPE, filePaths: string[]) =>
+            setToUploadFiles(type, filePaths),
+    );
+
+    ipcMain.handle("getElectronFilesFromGoogleZip", (_, filePath: string) =>
+        getElectronFilesFromGoogleZip(filePath),
+    );
+
+    ipcMain.handle("setToUploadCollection", (_, collectionName: string) =>
+        setToUploadCollection(collectionName),
+    );
+
+    ipcMain.handle("getDirFiles", (_, dirPath: string) => getDirFiles(dirPath));
 };
 };
 
 
 /**
 /**

+ 33 - 10
desktop/src/preload.ts

@@ -27,13 +27,6 @@
  */
  */
 
 
 import { contextBridge, ipcRenderer } from "electron";
 import { contextBridge, ipcRenderer } from "electron";
-import { getDirFiles } from "./api/fs";
-import {
-    getElectronFilesFromGoogleZip,
-    getPendingUploads,
-    setToUploadCollection,
-    setToUploadFiles,
-} from "./api/upload";
 import { setupLogging } from "./main/log";
 import { setupLogging } from "./main/log";
 import type { ElectronFile } from "./types";
 import type { ElectronFile } from "./types";
 
 
@@ -159,7 +152,7 @@ const runFFmpegCmd = (
 // - ML
 // - ML
 
 
 /* preload: duplicated Model */
 /* preload: duplicated Model */
-export enum Model {
+enum Model {
     GGML_CLIP = "ggml-clip",
     GGML_CLIP = "ggml-clip",
     ONNX_CLIP = "onnx-clip",
     ONNX_CLIP = "onnx-clip",
 }
 }
@@ -236,7 +229,7 @@ interface WatchMappingSyncedFile {
 }
 }
 
 
 /* preload: duplicated WatchMapping */
 /* preload: duplicated WatchMapping */
-export interface WatchMapping {
+interface WatchMapping {
     rootFolderName: string;
     rootFolderName: string;
     uploadStrategy: number;
     uploadStrategy: number;
     folderPath: string;
     folderPath: string;
@@ -290,6 +283,36 @@ const deleteFile = (path: string): Promise<void> =>
 const rename = (oldPath: string, newPath: string): Promise<void> =>
 const rename = (oldPath: string, newPath: string): Promise<void> =>
     ipcRenderer.invoke("rename", oldPath, newPath);
     ipcRenderer.invoke("rename", oldPath, newPath);
 
 
+// - Upload
+
+const getPendingUploads = (): Promise<{
+    files: ElectronFile[];
+    collectionName: string;
+    type: string;
+}> => ipcRenderer.invoke("getPendingUploads");
+
+/* preload: duplicated FILE_PATH_TYPE */
+enum FILE_PATH_TYPE {
+    FILES = "files",
+    ZIPS = "zips",
+}
+
+const setToUploadFiles = (
+    type: FILE_PATH_TYPE,
+    filePaths: string[],
+): Promise<void> => ipcRenderer.invoke("setToUploadFiles", type, filePaths);
+
+const getElectronFilesFromGoogleZip = (
+    filePath: string,
+): Promise<ElectronFile[]> =>
+    ipcRenderer.invoke("getElectronFilesFromGoogleZip", filePath);
+
+const setToUploadCollection = (collectionName: string): Promise<void> =>
+    ipcRenderer.invoke("setToUploadCollection", collectionName);
+
+const getDirFiles = (dirPath: string): Promise<ElectronFile[]> =>
+    ipcRenderer.invoke("getDirFiles", dirPath);
+
 // These objects exposed here will become available to the JS code in our
 // These objects exposed here will become available to the JS code in our
 // renderer (the web/ code) as `window.ElectronAPIs.*`
 // renderer (the web/ code) as `window.ElectronAPIs.*`
 //
 //
@@ -381,7 +404,7 @@ contextBridge.exposeInMainWorld("ElectronAPIs", {
     deleteFile,
     deleteFile,
     rename,
     rename,
 
 
-    // - Export
+    // - Upload
 
 
     getPendingUploads,
     getPendingUploads,
     setToUploadFiles,
     setToUploadFiles,

+ 6 - 0
desktop/src/services/fs.ts

@@ -7,6 +7,12 @@ import { ElectronFile } from "../types";
 
 
 const FILE_STREAM_CHUNK_SIZE: number = 4 * 1024 * 1024;
 const FILE_STREAM_CHUNK_SIZE: number = 4 * 1024 * 1024;
 
 
+export async function getDirFiles(dirPath: string) {
+    const files = await getDirFilePaths(dirPath);
+    const electronFiles = await Promise.all(files.map(getElectronFile));
+    return electronFiles;
+}
+
 // https://stackoverflow.com/a/63111390
 // https://stackoverflow.com/a/63111390
 export const getDirFilePaths = async (dirPath: string) => {
 export const getDirFilePaths = async (dirPath: string) => {
     if (!(await fs.stat(dirPath)).isDirectory()) {
     if (!(await fs.stat(dirPath)).isDirectory()) {

+ 2 - 2
web/apps/photos/src/components/Upload/Uploader.tsx

@@ -530,13 +530,13 @@ export default function Uploader(props: Props) {
             ) {
             ) {
                 await ImportService.setToUploadCollection(collections);
                 await ImportService.setToUploadCollection(collections);
                 if (zipPaths.current) {
                 if (zipPaths.current) {
-                    ElectronAPIs.setToUploadFiles(
+                    await ElectronAPIs.setToUploadFiles(
                         PICKED_UPLOAD_TYPE.ZIPS,
                         PICKED_UPLOAD_TYPE.ZIPS,
                         zipPaths.current,
                         zipPaths.current,
                     );
                     );
                     zipPaths.current = null;
                     zipPaths.current = null;
                 }
                 }
-                ElectronAPIs.setToUploadFiles(
+                await ElectronAPIs.setToUploadFiles(
                     PICKED_UPLOAD_TYPE.FILES,
                     PICKED_UPLOAD_TYPE.FILES,
                     filesWithCollectionToUploadIn.map(
                     filesWithCollectionToUploadIn.map(
                         ({ file }) => (file as ElectronFile).path,
                         ({ file }) => (file as ElectronFile).path,

+ 9 - 9
web/apps/photos/src/services/importService.ts

@@ -30,20 +30,20 @@ class ImportService {
         let collectionName: string = null;
         let collectionName: string = null;
         /* collection being one suggest one of two things
         /* collection being one suggest one of two things
                 1. Either the user has upload to a single existing collection
                 1. Either the user has upload to a single existing collection
-                2. Created a new single collection to upload to 
+                2. Created a new single collection to upload to
                     may have had multiple folder, but chose to upload
                     may have had multiple folder, but chose to upload
                     to one album
                     to one album
                 hence saving the collection name when upload collection count is 1
                 hence saving the collection name when upload collection count is 1
                 helps the info of user choosing this options
                 helps the info of user choosing this options
-                and on next upload we can directly start uploading to this collection 
+                and on next upload we can directly start uploading to this collection
             */
             */
         if (collections.length === 1) {
         if (collections.length === 1) {
             collectionName = collections[0].name;
             collectionName = collections[0].name;
         }
         }
-        ElectronAPIs.setToUploadCollection(collectionName);
+        await ElectronAPIs.setToUploadCollection(collectionName);
     }
     }
 
 
-    updatePendingUploads(files: FileWithCollection[]) {
+    async updatePendingUploads(files: FileWithCollection[]) {
         const filePaths = [];
         const filePaths = [];
         for (const fileWithCollection of files) {
         for (const fileWithCollection of files) {
             if (fileWithCollection.isLivePhoto) {
             if (fileWithCollection.isLivePhoto) {
@@ -57,13 +57,13 @@ class ImportService {
                 filePaths.push((fileWithCollection.file as ElectronFile).path);
                 filePaths.push((fileWithCollection.file as ElectronFile).path);
             }
             }
         }
         }
-        ElectronAPIs.setToUploadFiles(PICKED_UPLOAD_TYPE.FILES, filePaths);
+        await ElectronAPIs.setToUploadFiles(PICKED_UPLOAD_TYPE.FILES, filePaths);
     }
     }
 
 
-    cancelRemainingUploads() {
-        ElectronAPIs.setToUploadCollection(null);
-        ElectronAPIs.setToUploadFiles(PICKED_UPLOAD_TYPE.ZIPS, []);
-        ElectronAPIs.setToUploadFiles(PICKED_UPLOAD_TYPE.FILES, []);
+    async cancelRemainingUploads() {
+        await ElectronAPIs.setToUploadCollection(null);
+        await ElectronAPIs.setToUploadFiles(PICKED_UPLOAD_TYPE.ZIPS, []);
+        await ElectronAPIs.setToUploadFiles(PICKED_UPLOAD_TYPE.FILES, []);
     }
     }
 }
 }
 
 

+ 3 - 3
web/apps/photos/src/services/upload/uploadManager.ts

@@ -180,7 +180,7 @@ class UploadManager {
             if (e.message === CustomError.UPLOAD_CANCELLED) {
             if (e.message === CustomError.UPLOAD_CANCELLED) {
                 if (isElectron()) {
                 if (isElectron()) {
                     this.remainingFiles = [];
                     this.remainingFiles = [];
-                    ImportService.cancelRemainingUploads();
+                    await ImportService.cancelRemainingUploads();
                 }
                 }
             } else {
             } else {
                 logError(e, "uploading failed with error");
                 logError(e, "uploading failed with error");
@@ -326,7 +326,7 @@ class UploadManager {
             addLogLine(
             addLogLine(
                 `post upload action -> fileUploadResult: ${fileUploadResult} uploadedFile present ${!!uploadedFile}`,
                 `post upload action -> fileUploadResult: ${fileUploadResult} uploadedFile present ${!!uploadedFile}`,
             );
             );
-            this.updateElectronRemainingFiles(fileWithCollection);
+            await this.updateElectronRemainingFiles(fileWithCollection);
             switch (fileUploadResult) {
             switch (fileUploadResult) {
                 case UPLOAD_RESULT.FAILED:
                 case UPLOAD_RESULT.FAILED:
                 case UPLOAD_RESULT.BLOCKED:
                 case UPLOAD_RESULT.BLOCKED:
@@ -434,7 +434,7 @@ class UploadManager {
             this.remainingFiles = this.remainingFiles.filter(
             this.remainingFiles = this.remainingFiles.filter(
                 (file) => !areFileWithCollectionsSame(file, fileWithCollection),
                 (file) => !areFileWithCollectionsSame(file, fileWithCollection),
             );
             );
-            ImportService.updatePendingUploads(this.remainingFiles);
+            await ImportService.updatePendingUploads(this.remainingFiles);
         }
         }
     }
     }
 
 

+ 11 - 3
web/packages/shared/electron/types.ts

@@ -11,6 +11,11 @@ export enum Model {
     ONNX_CLIP = "onnx-clip",
     ONNX_CLIP = "onnx-clip",
 }
 }
 
 
+export enum FILE_PATH_TYPE {
+    FILES = "files",
+    ZIPS = "zips",
+}
+
 /**
 /**
  * Extra APIs provided by the Node.js layer when our code is running in Electron
  * Extra APIs provided by the Node.js layer when our code is running in Electron
  *
  *
@@ -192,17 +197,20 @@ export interface ElectronAPIsType {
     deleteFile: (path: string) => Promise<void>;
     deleteFile: (path: string) => Promise<void>;
     rename: (oldPath: string, newPath: string) => Promise<void>;
     rename: (oldPath: string, newPath: string) => Promise<void>;
 
 
-    /** TODO: FIXME or migrate below this */
+    // - Upload
 
 
     getPendingUploads: () => Promise<{
     getPendingUploads: () => Promise<{
         files: ElectronFile[];
         files: ElectronFile[];
         collectionName: string;
         collectionName: string;
         type: string;
         type: string;
     }>;
     }>;
-    setToUploadFiles: (type: string, filePaths: string[]) => void;
+    setToUploadFiles: (
+        type: FILE_PATH_TYPE,
+        filePaths: string[],
+    ) => Promise<void>;
     getElectronFilesFromGoogleZip: (
     getElectronFilesFromGoogleZip: (
         filePath: string,
         filePath: string,
     ) => Promise<ElectronFile[]>;
     ) => Promise<ElectronFile[]>;
-    setToUploadCollection: (collectionName: string) => void;
+    setToUploadCollection: (collectionName: string) => Promise<void>;
     getDirFiles: (dirPath: string) => Promise<ElectronFile[]>;
     getDirFiles: (dirPath: string) => Promise<ElectronFile[]>;
 }
 }