This commit is contained in:
Manav Rathi 2024-04-29 14:34:05 +05:30
parent 3d298a9cd4
commit 2fa1fcac65
No known key found for this signature in database
4 changed files with 21 additions and 68 deletions

View file

@ -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());

View file

@ -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<ZipEntry[]> => {
export const listZipEntries = async (zipPath: string): Promise<ZipEntry[]> => {
const zip = new StreamZip.async({ file: zipPath });
const entries = await zip.entries();
@ -33,7 +30,9 @@ export const pendingUploads = async (): Promise<PendingUploads | undefined> => {
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<PendingUploads | undefined> => {
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,

View file

@ -242,8 +242,8 @@ const watchFindFiles = (folderPath: string): Promise<string[]> =>
// - Upload
const zipEntries = (zipPath: string): Promise<ZipEntry[]> =>
ipcRenderer.invoke("zipEntries", zipPath);
const listZipEntries = (zipPath: string): Promise<ZipEntry[]> =>
ipcRenderer.invoke("listZipEntries", zipPath);
const pendingUploads = (): Promise<PendingUploads | undefined> =>
ipcRenderer.invoke("pendingUploads");
@ -370,7 +370,7 @@ contextBridge.exposeInMainWorld("electron", {
// - Upload
zipEntries,
listZipEntries,
pendingUploads,
setPendingUploads,
markUploadedFiles,

View file

@ -478,7 +478,7 @@ export interface Electron {
*
* To read the contents of the files themselves, see [Note: IPC streams].
*/
zipEntries : (zipPath: string) => Promise<ZipEntry[]>
listZipEntries : (zipPath: string) => Promise<ZipEntry[]>
/**
* Return any pending uploads that were previously enqueued but haven't yet