This commit is contained in:
Manav Rathi 2024-04-29 21:06:20 +05:30
parent 38094f317a
commit fca398f296
No known key found for this signature in database
6 changed files with 38 additions and 19 deletions

View file

@ -27,5 +27,3 @@ export const fsIsDir = async (dirPath: string) => {
const stat = await fs.stat(dirPath);
return stat.isDirectory();
};
export const fsSize = (path: string) => fs.stat(path).then((s) => s.size);

View file

@ -14,6 +14,7 @@ import type {
CollectionMapping,
FolderWatch,
PendingUploads,
ZipEntry,
} from "../types/ipc";
import {
selectDirectory,
@ -29,7 +30,6 @@ import {
fsRename,
fsRm,
fsRmdir,
fsSize,
fsWriteFile,
} from "./fs";
import { logToDisk } from "./log";
@ -54,6 +54,7 @@ import {
import {
clearPendingUploads,
listZipEntries,
pathOrZipEntrySize,
markUploadedFiles,
markUploadedZipEntries,
pendingUploads,
@ -141,8 +142,6 @@ export const attachIPCHandlers = () => {
ipcMain.handle("fsIsDir", (_, dirPath: string) => fsIsDir(dirPath));
ipcMain.handle("fsSize", (_, path: string) => fsSize(path));
// - Conversion
ipcMain.handle("convertToJPEG", (_, imageData: Uint8Array) =>
@ -204,6 +203,12 @@ export const attachIPCHandlers = () => {
listZipEntries(zipPath),
);
ipcMain.handle(
"pathOrZipEntrySize",
(_, pathOrZipEntry: string | ZipEntry) =>
pathOrZipEntrySize(pathOrZipEntry),
);
ipcMain.handle("pendingUploads", () => pendingUploads());
ipcMain.handle("setPendingUploads", (_, pendingUploads: PendingUploads) =>

View file

@ -1,4 +1,5 @@
import StreamZip from "node-stream-zip";
import fs from "node:fs/promises";
import { existsSync } from "original-fs";
import path from "path";
import type { ElectronFile, PendingUploads, ZipEntry } from "../../types/ipc";
@ -23,6 +24,20 @@ export const listZipEntries = async (zipPath: string): Promise<ZipEntry[]> => {
return entryNames.map((entryName) => [zipPath, entryName]);
};
export const pathOrZipEntrySize = async (
pathOrZipEntry: string | ZipEntry,
): Promise<number> => {
if (typeof pathOrZipEntry == "string") {
const stat = await fs.stat(pathOrZipEntry);
return stat.size;
} else {
const [zipPath, entryName] = pathOrZipEntry;
const zip = new StreamZip.async({ file: zipPath });
const entry = await zip.entry(entryName);
return entry.size;
}
};
export const pendingUploads = async (): Promise<PendingUploads | undefined> => {
const collectionName = uploadStatusStore.get("collectionName");

View file

@ -123,9 +123,6 @@ const fsWriteFile = (path: string, contents: string): Promise<void> =>
const fsIsDir = (dirPath: string): Promise<boolean> =>
ipcRenderer.invoke("fsIsDir", dirPath);
const fsSize = (path: string): Promise<number> =>
ipcRenderer.invoke("fsSize", path);
// - Conversion
const convertToJPEG = (imageData: Uint8Array): Promise<Uint8Array> =>
@ -247,6 +244,10 @@ const pathForFile = (file: File) => webUtils.getPathForFile(file);
const listZipEntries = (zipPath: string): Promise<ZipEntry[]> =>
ipcRenderer.invoke("listZipEntries", zipPath);
const pathOrZipEntrySize = (
pathOrZipEntry: string | ZipEntry,
): Promise<number> => ipcRenderer.invoke("pathOrZipEntrySize", pathOrZipEntry);
const pendingUploads = (): Promise<PendingUploads | undefined> =>
ipcRenderer.invoke("pendingUploads");
@ -333,7 +334,6 @@ contextBridge.exposeInMainWorld("electron", {
readTextFile: fsReadTextFile,
writeFile: fsWriteFile,
isDir: fsIsDir,
size: fsSize,
},
// - Conversion
@ -374,6 +374,7 @@ contextBridge.exposeInMainWorld("electron", {
pathForFile,
listZipEntries,
pathOrZipEntrySize,
pendingUploads,
setPendingUploads,
markUploadedFiles,

View file

@ -197,13 +197,12 @@ export const uploadItemSize = async (
uploadItem: UploadItem,
): Promise<number> => {
if (uploadItem instanceof File) return uploadItem.size;
if (typeof uploadItem == "string") return basename(uploadItem);
if (Array.isArray(uploadItem)) return basename(uploadItem[1]);
if (typeof uploadItem == "string")
return ensureElectron().pathOrZipEntrySize(uploadItem);
if (Array.isArray(uploadItem))
return ensureElectron().pathOrZipEntrySize(uploadItem);
return uploadItem.file.size;
};
uploadItem instanceof File
? uploadItem.size
: await ensureElectron().fs.size(uploadItem);
/* -- Various intermediate type used during upload -- */

View file

@ -189,11 +189,6 @@ export interface Electron {
* directory.
*/
isDir: (dirPath: string) => Promise<boolean>;
/**
* Return the size in bytes of the file at {@link path}.
*/
size: (path: string) => Promise<number>;
};
// - Conversion
@ -492,6 +487,12 @@ export interface Electron {
*/
listZipEntries: (zipPath: string) => Promise<ZipEntry[]>;
/**
* Return the size in bytes of the file at the given path or of a particular
* entry within a zip file.
*/
pathOrZipEntrySize: (pathOrZipEntry: string | ZipEntry) => Promise<number>;
/**
* Return any pending uploads that were previously enqueued but haven't yet
* been completed.