Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"@types/diff": "^7",
"@types/electron-squirrel-startup": "^1.0.2",
"@types/eslint__js": "^8.42.3",
"@types/mv": "^2",
"@types/node": "^22.10.2",
"@types/tar": "6.1.13",
"@types/tmp": "^0.2.6",
Expand Down Expand Up @@ -111,6 +112,7 @@
"electron-store": "8.2.0",
"lodash": "4.17.21",
"mixpanel": "^0.18.0",
"mv": "^2.1.1",
"node-pty": "^1.0.0",
"systeminformation": "^5.24.8",
"tar": "^7.4.3",
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export const IPC_CHANNELS = {
CAN_ACCESS_URL: 'can-access-url',
START_TROUBLESHOOTING: 'start-troubleshooting',
DISABLE_CUSTOM_NODES: 'disable-custom-nodes',
IMPORT_MODEL: 'import-model',
} as const;

export enum ProgressStatus {
Expand Down
46 changes: 46 additions & 0 deletions src/handlers/importModelHandlers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { ipcMain } from 'electron';
import log from 'electron-log/main';
import mv from 'mv';
import { existsSync } from 'node:fs';
import { copyFile, mkdir } from 'node:fs/promises';
import path from 'node:path';

import { useDesktopConfig } from '@/store/desktopConfig';

import { IPC_CHANNELS } from '../constants';

export function registerImportModelHandlers() {
ipcMain.handle(
IPC_CHANNELS.IMPORT_MODEL,
async (_, filePath: string, type: string, mode: 'move' | 'copy'): Promise<void> => {
try {
const basePath = useDesktopConfig().get('basePath');

if (!basePath) {
throw new Error('Base path is not set');
}

const destinationDir = path.join(basePath, 'models', type);
const destinationPath = path.join(destinationDir, path.basename(filePath));

if (!existsSync(destinationDir)) {
await mkdir(destinationDir, { recursive: true });
}

log.info(mode, filePath, '->', destinationPath);

await (mode == 'move'
? new Promise((resolve, reject) => {
mv(filePath, destinationPath, (err) => {
if (err) reject(err instanceof Error ? err : new Error(String(err)));
else resolve(true);
});
})
: copyFile(filePath, destinationPath));
} catch (error) {
console.error(error);
throw error;
}
}
);
}
2 changes: 2 additions & 0 deletions src/main-process/comfyDesktopApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { app, ipcMain } from 'electron';
import log from 'electron-log/main';

import { useComfySettings } from '@/config/comfySettings';
import { registerImportModelHandlers } from '@/handlers/importModelHandlers';

import { DEFAULT_SERVER_ARGS, IPC_CHANNELS, ProgressStatus, ServerArgs } from '../constants';
import { DownloadManager } from '../models/DownloadManager';
Expand Down Expand Up @@ -76,6 +77,7 @@ export class ComfyDesktopApp implements HasTelemetry {
registerIPCHandlers(): void {
// Restart core
ipcMain.handle(IPC_CHANNELS.RESTART_CORE, async (): Promise<boolean> => await this.restartComfyServer());
registerImportModelHandlers();

app.on('before-quit', () => {
if (!this.comfyServer) return;
Expand Down
19 changes: 18 additions & 1 deletion src/preload.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { contextBridge, ipcRenderer } from 'electron';
import { contextBridge, ipcRenderer, webUtils } from 'electron';
import path from 'node:path';

import { DownloadStatus, ELECTRON_BRIDGE_API, IPC_CHANNELS, ProgressStatus } from './constants';
Expand Down Expand Up @@ -295,6 +295,23 @@ const electronAPI = {
showContextMenu: (options?: ElectronContextMenuOptions): void => {
return ipcRenderer.send(IPC_CHANNELS.SHOW_CONTEXT_MENU, options);
},
/**
* Get the path for a file.
* @param file The file to get the path for.
* @returns The path for the file.
*/
getFilePath: (file: File) => {
return webUtils.getPathForFile(file);
},
/**
* Imports a model file.
* @param file The file to import.
* @param type The type of model to import.
* @param mode If the file should be moved or copied.
*/
importModel: (file: File, type: string, mode: 'move' | 'copy') => {
return ipcRenderer.invoke(IPC_CHANNELS.IMPORT_MODEL, webUtils.getPathForFile(file), type, mode);
},
Config: {
/**
* Finds the name of the last detected GPU type. Detection only runs during installation.
Expand Down
11 changes: 10 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ __metadata:
"@types/electron-squirrel-startup": "npm:^1.0.2"
"@types/eslint__js": "npm:^8.42.3"
"@types/lodash": "npm:4.17.15"
"@types/mv": "npm:^2"
"@types/node": "npm:^22.10.2"
"@types/tar": "npm:6.1.13"
"@types/tmp": "npm:^0.2.6"
Expand All @@ -264,6 +265,7 @@ __metadata:
lint-staged: "npm:^15.2.10"
lodash: "npm:4.17.21"
mixpanel: "npm:^0.18.0"
mv: "npm:^2.1.1"
node-pty: "npm:^1.0.0"
prettier: "npm:^3.3.3"
rimraf: "npm:^6.0.1"
Expand Down Expand Up @@ -2882,6 +2884,13 @@ __metadata:
languageName: node
linkType: hard

"@types/mv@npm:^2":
version: 2.1.4
resolution: "@types/mv@npm:2.1.4"
checksum: 10c0/9372401a4381aacc0dd004b8ba37c954a26c98b1937952cb0dc9c8933c904dceb509e2e697c1f9d21c62eb0d4de965f669d157ad1e4a8c8ea1d28252c26f8faa
languageName: node
linkType: hard

"@types/mysql@npm:2.15.26":
version: 2.15.26
resolution: "@types/mysql@npm:2.15.26"
Expand Down Expand Up @@ -8895,7 +8904,7 @@ __metadata:
languageName: node
linkType: hard

"mv@npm:~2":
"mv@npm:^2.1.1, mv@npm:~2":
version: 2.1.1
resolution: "mv@npm:2.1.1"
dependencies:
Expand Down
Loading