From 2fa1fcac655251b523d55785cdaed8bb222124eb Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Mon, 29 Apr 2024 14:34:05 +0530 Subject: [PATCH] impl --- desktop/src/main/ipc.ts | 6 ++- desktop/src/main/services/upload.ts | 75 +++++------------------------ desktop/src/preload.ts | 6 +-- web/packages/next/types/ipc.ts | 2 +- 4 files changed, 21 insertions(+), 68 deletions(-) diff --git a/desktop/src/main/ipc.ts b/desktop/src/main/ipc.ts index d7d4fdc09..a99a32d09 100644 --- a/desktop/src/main/ipc.ts +++ b/desktop/src/main/ipc.ts @@ -53,11 +53,11 @@ import { } from "./services/store"; import { clearPendingUploads, + listZipEntries, markUploadedFiles, markUploadedZipEntries, pendingUploads, setPendingUploads, - zipEntries, } from "./services/upload"; import { watchAdd, @@ -200,7 +200,9 @@ export const attachIPCHandlers = () => { // - Upload - ipcMain.handle("zipEntries", (_, zipPath: string) => zipEntries(zipPath)); + ipcMain.handle("listZipEntries", (_, zipPath: string) => + listZipEntries(zipPath), + ); ipcMain.handle("pendingUploads", () => pendingUploads()); diff --git a/desktop/src/main/services/upload.ts b/desktop/src/main/services/upload.ts index c5a987e7b..8487f8327 100644 --- a/desktop/src/main/services/upload.ts +++ b/desktop/src/main/services/upload.ts @@ -2,13 +2,10 @@ import StreamZip from "node-stream-zip"; import { existsSync } from "original-fs"; import path from "path"; import type { ElectronFile, PendingUploads, ZipEntry } from "../../types/ipc"; -import { - uploadStatusStore, - type UploadStatusStore, -} from "../stores/upload-status"; -import { getElectronFile, getZipFileStream } from "./fs"; +import { uploadStatusStore } from "../stores/upload-status"; +import { getZipFileStream } from "./fs"; -export const zipEntries = async (zipPath: string): Promise => { +export const listZipEntries = async (zipPath: string): Promise => { const zip = new StreamZip.async({ file: zipPath }); const entries = await zip.entries(); @@ -33,7 +30,9 @@ export const pendingUploads = async (): Promise => { const allFilePaths = uploadStatusStore.get("filePaths"); const filePaths = allFilePaths.filter((f) => existsSync(f)); - let allZipEntries = uploadStatusStore.get("zipEntries"); + const allZipEntries = uploadStatusStore.get("zipEntries"); + let zipEntries: typeof allZipEntries; + // Migration code - May 2024. Remove after a bit. // // The older store formats will not have zipEntries and instead will have @@ -44,34 +43,17 @@ export const pendingUploads = async (): Promise => { if (allZipEntries === undefined) { const allZipPaths = uploadStatusStore.get("filePaths"); const zipPaths = allZipPaths.filter((f) => existsSync(f)); - lsZip(); - } - - if (allZipEntries) "files"; - const zipPaths = validSavedPaths("zips"); - - let files: ElectronFile[] = []; - let type: PendingUploads["type"]; - - if (zipPaths.length) { - type = "zips"; - for (const zipPath of zipPaths) { - files = [ - ...files, - ...(await getElectronFilesFromGoogleZip(zipPath)), - ]; - } - const pendingFilePaths = new Set(filePaths); - files = files.filter((file) => pendingFilePaths.has(file.path)); - } else if (filePaths.length) { - type = "files"; - files = await Promise.all(filePaths.map(getElectronFile)); + zipEntries = []; + for (const zip of zipPaths) + zipEntries = zipEntries.concat(await listZipEntries(zip)); + } else { + zipEntries = allZipEntries.filter(([z]) => existsSync(z)); } return { - files, collectionName, - type, + filePaths, + zipEntries, }; }; @@ -94,39 +76,8 @@ export const markUploadedZipEntries = async ( uploadStatusStore.set("zipEntries", updated); }; -const validSavedPaths = (type: PendingUploads["type"]) => { - const key = storeKey(type); - const savedPaths = (uploadStatusStore.get(key) as string[]) ?? []; - const paths = savedPaths.filter((p) => existsSync(p)); - uploadStatusStore.set(key, paths); - return paths; -}; - -const setPendingUploadCollection = (collectionName: string) => { - if (collectionName) uploadStatusStore.set("collectionName", collectionName); - else uploadStatusStore.delete("collectionName"); -}; - -const setPendingUploadFiles = ( - type: PendingUploads["type"], - filePaths: string[], -) => { - const key = storeKey(type); - if (filePaths) uploadStatusStore.set(key, filePaths); - else uploadStatusStore.delete(key); -}; - export const clearPendingUploads = () => uploadStatusStore.clear(); -const storeKey = (type: PendingUploads["type"]): keyof UploadStatusStore => { - switch (type) { - case "zips": - return "zipPaths"; - case "files": - return "filePaths"; - } -}; - export const getElectronFilesFromGoogleZip = async (filePath: string) => { const zip = new StreamZip.async({ file: filePath, diff --git a/desktop/src/preload.ts b/desktop/src/preload.ts index ac149ad13..e80625de9 100644 --- a/desktop/src/preload.ts +++ b/desktop/src/preload.ts @@ -242,8 +242,8 @@ const watchFindFiles = (folderPath: string): Promise => // - Upload -const zipEntries = (zipPath: string): Promise => - ipcRenderer.invoke("zipEntries", zipPath); +const listZipEntries = (zipPath: string): Promise => + ipcRenderer.invoke("listZipEntries", zipPath); const pendingUploads = (): Promise => ipcRenderer.invoke("pendingUploads"); @@ -370,7 +370,7 @@ contextBridge.exposeInMainWorld("electron", { // - Upload - zipEntries, + listZipEntries, pendingUploads, setPendingUploads, markUploadedFiles, diff --git a/web/packages/next/types/ipc.ts b/web/packages/next/types/ipc.ts index 6aa394c6c..7198a2ebc 100644 --- a/web/packages/next/types/ipc.ts +++ b/web/packages/next/types/ipc.ts @@ -478,7 +478,7 @@ export interface Electron { * * To read the contents of the files themselves, see [Note: IPC streams]. */ - zipEntries : (zipPath: string) => Promise + listZipEntries : (zipPath: string) => Promise /** * Return any pending uploads that were previously enqueued but haven't yet