impl
This commit is contained in:
parent
38094f317a
commit
fca398f296
|
@ -27,5 +27,3 @@ export const fsIsDir = async (dirPath: string) => {
|
||||||
const stat = await fs.stat(dirPath);
|
const stat = await fs.stat(dirPath);
|
||||||
return stat.isDirectory();
|
return stat.isDirectory();
|
||||||
};
|
};
|
||||||
|
|
||||||
export const fsSize = (path: string) => fs.stat(path).then((s) => s.size);
|
|
||||||
|
|
|
@ -14,6 +14,7 @@ import type {
|
||||||
CollectionMapping,
|
CollectionMapping,
|
||||||
FolderWatch,
|
FolderWatch,
|
||||||
PendingUploads,
|
PendingUploads,
|
||||||
|
ZipEntry,
|
||||||
} from "../types/ipc";
|
} from "../types/ipc";
|
||||||
import {
|
import {
|
||||||
selectDirectory,
|
selectDirectory,
|
||||||
|
@ -29,7 +30,6 @@ import {
|
||||||
fsRename,
|
fsRename,
|
||||||
fsRm,
|
fsRm,
|
||||||
fsRmdir,
|
fsRmdir,
|
||||||
fsSize,
|
|
||||||
fsWriteFile,
|
fsWriteFile,
|
||||||
} from "./fs";
|
} from "./fs";
|
||||||
import { logToDisk } from "./log";
|
import { logToDisk } from "./log";
|
||||||
|
@ -54,6 +54,7 @@ import {
|
||||||
import {
|
import {
|
||||||
clearPendingUploads,
|
clearPendingUploads,
|
||||||
listZipEntries,
|
listZipEntries,
|
||||||
|
pathOrZipEntrySize,
|
||||||
markUploadedFiles,
|
markUploadedFiles,
|
||||||
markUploadedZipEntries,
|
markUploadedZipEntries,
|
||||||
pendingUploads,
|
pendingUploads,
|
||||||
|
@ -141,8 +142,6 @@ export const attachIPCHandlers = () => {
|
||||||
|
|
||||||
ipcMain.handle("fsIsDir", (_, dirPath: string) => fsIsDir(dirPath));
|
ipcMain.handle("fsIsDir", (_, dirPath: string) => fsIsDir(dirPath));
|
||||||
|
|
||||||
ipcMain.handle("fsSize", (_, path: string) => fsSize(path));
|
|
||||||
|
|
||||||
// - Conversion
|
// - Conversion
|
||||||
|
|
||||||
ipcMain.handle("convertToJPEG", (_, imageData: Uint8Array) =>
|
ipcMain.handle("convertToJPEG", (_, imageData: Uint8Array) =>
|
||||||
|
@ -204,6 +203,12 @@ export const attachIPCHandlers = () => {
|
||||||
listZipEntries(zipPath),
|
listZipEntries(zipPath),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
ipcMain.handle(
|
||||||
|
"pathOrZipEntrySize",
|
||||||
|
(_, pathOrZipEntry: string | ZipEntry) =>
|
||||||
|
pathOrZipEntrySize(pathOrZipEntry),
|
||||||
|
);
|
||||||
|
|
||||||
ipcMain.handle("pendingUploads", () => pendingUploads());
|
ipcMain.handle("pendingUploads", () => pendingUploads());
|
||||||
|
|
||||||
ipcMain.handle("setPendingUploads", (_, pendingUploads: PendingUploads) =>
|
ipcMain.handle("setPendingUploads", (_, pendingUploads: PendingUploads) =>
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import StreamZip from "node-stream-zip";
|
import StreamZip from "node-stream-zip";
|
||||||
|
import fs from "node:fs/promises";
|
||||||
import { existsSync } from "original-fs";
|
import { existsSync } from "original-fs";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import type { ElectronFile, PendingUploads, ZipEntry } from "../../types/ipc";
|
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]);
|
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> => {
|
export const pendingUploads = async (): Promise<PendingUploads | undefined> => {
|
||||||
const collectionName = uploadStatusStore.get("collectionName");
|
const collectionName = uploadStatusStore.get("collectionName");
|
||||||
|
|
||||||
|
|
|
@ -123,9 +123,6 @@ const fsWriteFile = (path: string, contents: string): Promise<void> =>
|
||||||
const fsIsDir = (dirPath: string): Promise<boolean> =>
|
const fsIsDir = (dirPath: string): Promise<boolean> =>
|
||||||
ipcRenderer.invoke("fsIsDir", dirPath);
|
ipcRenderer.invoke("fsIsDir", dirPath);
|
||||||
|
|
||||||
const fsSize = (path: string): Promise<number> =>
|
|
||||||
ipcRenderer.invoke("fsSize", path);
|
|
||||||
|
|
||||||
// - Conversion
|
// - Conversion
|
||||||
|
|
||||||
const convertToJPEG = (imageData: Uint8Array): Promise<Uint8Array> =>
|
const convertToJPEG = (imageData: Uint8Array): Promise<Uint8Array> =>
|
||||||
|
@ -247,6 +244,10 @@ const pathForFile = (file: File) => webUtils.getPathForFile(file);
|
||||||
const listZipEntries = (zipPath: string): Promise<ZipEntry[]> =>
|
const listZipEntries = (zipPath: string): Promise<ZipEntry[]> =>
|
||||||
ipcRenderer.invoke("listZipEntries", zipPath);
|
ipcRenderer.invoke("listZipEntries", zipPath);
|
||||||
|
|
||||||
|
const pathOrZipEntrySize = (
|
||||||
|
pathOrZipEntry: string | ZipEntry,
|
||||||
|
): Promise<number> => ipcRenderer.invoke("pathOrZipEntrySize", pathOrZipEntry);
|
||||||
|
|
||||||
const pendingUploads = (): Promise<PendingUploads | undefined> =>
|
const pendingUploads = (): Promise<PendingUploads | undefined> =>
|
||||||
ipcRenderer.invoke("pendingUploads");
|
ipcRenderer.invoke("pendingUploads");
|
||||||
|
|
||||||
|
@ -333,7 +334,6 @@ contextBridge.exposeInMainWorld("electron", {
|
||||||
readTextFile: fsReadTextFile,
|
readTextFile: fsReadTextFile,
|
||||||
writeFile: fsWriteFile,
|
writeFile: fsWriteFile,
|
||||||
isDir: fsIsDir,
|
isDir: fsIsDir,
|
||||||
size: fsSize,
|
|
||||||
},
|
},
|
||||||
|
|
||||||
// - Conversion
|
// - Conversion
|
||||||
|
@ -374,6 +374,7 @@ contextBridge.exposeInMainWorld("electron", {
|
||||||
|
|
||||||
pathForFile,
|
pathForFile,
|
||||||
listZipEntries,
|
listZipEntries,
|
||||||
|
pathOrZipEntrySize,
|
||||||
pendingUploads,
|
pendingUploads,
|
||||||
setPendingUploads,
|
setPendingUploads,
|
||||||
markUploadedFiles,
|
markUploadedFiles,
|
||||||
|
|
|
@ -197,13 +197,12 @@ export const uploadItemSize = async (
|
||||||
uploadItem: UploadItem,
|
uploadItem: UploadItem,
|
||||||
): Promise<number> => {
|
): Promise<number> => {
|
||||||
if (uploadItem instanceof File) return uploadItem.size;
|
if (uploadItem instanceof File) return uploadItem.size;
|
||||||
if (typeof uploadItem == "string") return basename(uploadItem);
|
if (typeof uploadItem == "string")
|
||||||
if (Array.isArray(uploadItem)) return basename(uploadItem[1]);
|
return ensureElectron().pathOrZipEntrySize(uploadItem);
|
||||||
|
if (Array.isArray(uploadItem))
|
||||||
|
return ensureElectron().pathOrZipEntrySize(uploadItem);
|
||||||
return uploadItem.file.size;
|
return uploadItem.file.size;
|
||||||
};
|
};
|
||||||
uploadItem instanceof File
|
|
||||||
? uploadItem.size
|
|
||||||
: await ensureElectron().fs.size(uploadItem);
|
|
||||||
|
|
||||||
/* -- Various intermediate type used during upload -- */
|
/* -- Various intermediate type used during upload -- */
|
||||||
|
|
||||||
|
|
|
@ -189,11 +189,6 @@ export interface Electron {
|
||||||
* directory.
|
* directory.
|
||||||
*/
|
*/
|
||||||
isDir: (dirPath: string) => Promise<boolean>;
|
isDir: (dirPath: string) => Promise<boolean>;
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the size in bytes of the file at {@link path}.
|
|
||||||
*/
|
|
||||||
size: (path: string) => Promise<number>;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// - Conversion
|
// - Conversion
|
||||||
|
@ -492,6 +487,12 @@ export interface Electron {
|
||||||
*/
|
*/
|
||||||
listZipEntries: (zipPath: string) => Promise<ZipEntry[]>;
|
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
|
* Return any pending uploads that were previously enqueued but haven't yet
|
||||||
* been completed.
|
* been completed.
|
||||||
|
|
Loading…
Reference in a new issue