Convert remaining upload functions

This commit is contained in:
Manav Rathi 2024-03-25 15:29:39 +05:30
parent f53b1361e8
commit 892bf852a5
No known key found for this signature in database
8 changed files with 95 additions and 35 deletions

View file

@ -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;
}

View file

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

View file

@ -27,13 +27,6 @@
*/
import { contextBridge, ipcRenderer } from "electron";
import { getDirFiles } from "./api/fs";
import {
getElectronFilesFromGoogleZip,
getPendingUploads,
setToUploadCollection,
setToUploadFiles,
} from "./api/upload";
import { setupLogging } from "./main/log";
import type { ElectronFile } from "./types";
@ -159,7 +152,7 @@ const runFFmpegCmd = (
// - ML
/* preload: duplicated Model */
export enum Model {
enum Model {
GGML_CLIP = "ggml-clip",
ONNX_CLIP = "onnx-clip",
}
@ -236,7 +229,7 @@ interface WatchMappingSyncedFile {
}
/* preload: duplicated WatchMapping */
export interface WatchMapping {
interface WatchMapping {
rootFolderName: string;
uploadStrategy: number;
folderPath: string;
@ -290,6 +283,36 @@ const deleteFile = (path: string): Promise<void> =>
const rename = (oldPath: string, newPath: string): Promise<void> =>
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
// renderer (the web/ code) as `window.ElectronAPIs.*`
//
@ -381,7 +404,7 @@ contextBridge.exposeInMainWorld("ElectronAPIs", {
deleteFile,
rename,
// - Export
// - Upload
getPendingUploads,
setToUploadFiles,

View file

@ -7,6 +7,12 @@ import { ElectronFile } from "../types";
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
export const getDirFilePaths = async (dirPath: string) => {
if (!(await fs.stat(dirPath)).isDirectory()) {

View file

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

View file

@ -40,10 +40,10 @@ class ImportService {
if (collections.length === 1) {
collectionName = collections[0].name;
}
ElectronAPIs.setToUploadCollection(collectionName);
await ElectronAPIs.setToUploadCollection(collectionName);
}
updatePendingUploads(files: FileWithCollection[]) {
async updatePendingUploads(files: FileWithCollection[]) {
const filePaths = [];
for (const fileWithCollection of files) {
if (fileWithCollection.isLivePhoto) {
@ -57,13 +57,13 @@ class ImportService {
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, []);
}
}

View file

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

View file

@ -11,6 +11,11 @@ export enum Model {
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
*
@ -192,17 +197,20 @@ export interface ElectronAPIsType {
deleteFile: (path: string) => Promise<void>;
rename: (oldPath: string, newPath: string) => Promise<void>;
/** TODO: FIXME or migrate below this */
// - Upload
getPendingUploads: () => Promise<{
files: ElectronFile[];
collectionName: string;
type: string;
}>;
setToUploadFiles: (type: string, filePaths: string[]) => void;
setToUploadFiles: (
type: FILE_PATH_TYPE,
filePaths: string[],
) => Promise<void>;
getElectronFilesFromGoogleZip: (
filePath: string,
) => Promise<ElectronFile[]>;
setToUploadCollection: (collectionName: string) => void;
setToUploadCollection: (collectionName: string) => Promise<void>;
getDirFiles: (dirPath: string) => Promise<ElectronFile[]>;
}