2023-09-10 11:14:04 +02:00
|
|
|
import { MessageBoxOptions, dialog } from "electron";
|
|
|
|
import {
|
2024-04-09 20:11:24 +02:00
|
|
|
savedCustomModelsPath,
|
|
|
|
setSavedCustomModelsPath,
|
2023-09-10 11:14:04 +02:00
|
|
|
} from "../utils/config-variables";
|
|
|
|
import logit from "../utils/logit";
|
|
|
|
import slash from "../utils/slash";
|
2024-10-06 09:15:44 +02:00
|
|
|
import { ELECTRON_COMMANDS } from "../../common/electron-commands";
|
2023-09-10 11:14:04 +02:00
|
|
|
import getModels from "../utils/get-models";
|
2023-09-10 19:42:18 +02:00
|
|
|
import { getMainWindow } from "../main-window";
|
2023-11-10 12:41:35 +01:00
|
|
|
import settings from "electron-settings";
|
2024-10-04 11:15:54 +02:00
|
|
|
import { FEATURE_FLAGS } from "../../common/feature-flags";
|
2023-09-10 19:42:18 +02:00
|
|
|
|
2023-09-10 11:14:04 +02:00
|
|
|
const customModelsSelect = async (event, message) => {
|
2023-09-10 19:54:08 +02:00
|
|
|
const mainWindow = getMainWindow();
|
|
|
|
|
2023-09-10 11:14:04 +02:00
|
|
|
if (!mainWindow) return;
|
2023-11-02 16:03:21 +01:00
|
|
|
const {
|
|
|
|
canceled,
|
|
|
|
filePaths: folderPaths,
|
|
|
|
bookmarks,
|
|
|
|
} = await dialog.showOpenDialog({
|
2023-09-10 11:14:04 +02:00
|
|
|
properties: ["openDirectory"],
|
|
|
|
title: "Select Custom Models Folder",
|
2024-04-09 20:11:24 +02:00
|
|
|
defaultPath: savedCustomModelsPath,
|
2023-11-02 16:03:21 +01:00
|
|
|
securityScopedBookmarks: true,
|
2023-11-10 12:41:35 +01:00
|
|
|
message: "Select Custom Models Folder that is named 'models'",
|
2023-09-10 11:14:04 +02:00
|
|
|
});
|
2023-11-02 16:03:21 +01:00
|
|
|
|
2024-10-04 11:15:54 +02:00
|
|
|
if (FEATURE_FLAGS.APP_STORE_BUILD && bookmarks && bookmarks.length > 0) {
|
2023-11-10 12:41:35 +01:00
|
|
|
console.log("🚨 Setting Bookmark: ", bookmarks);
|
|
|
|
await settings.set("custom-models-bookmarks", bookmarks[0]);
|
2023-11-02 16:03:21 +01:00
|
|
|
}
|
|
|
|
|
2023-09-10 11:14:04 +02:00
|
|
|
if (canceled) {
|
|
|
|
logit("🚫 Select Custom Models Folder Operation Cancelled");
|
|
|
|
return null;
|
|
|
|
} else {
|
2024-04-09 20:11:24 +02:00
|
|
|
setSavedCustomModelsPath(folderPaths[0]);
|
2023-09-10 11:14:04 +02:00
|
|
|
|
|
|
|
if (
|
|
|
|
!folderPaths[0].endsWith(slash + "models") &&
|
|
|
|
!folderPaths[0].endsWith(slash + "models" + slash)
|
|
|
|
) {
|
|
|
|
logit("❌ Invalid Custom Models Folder Detected: Not a 'models' folder");
|
|
|
|
const options: MessageBoxOptions = {
|
|
|
|
type: "error",
|
|
|
|
title: "Invalid Folder",
|
|
|
|
message:
|
|
|
|
"Please make sure that the folder name is 'models' and nothing else.",
|
|
|
|
buttons: ["OK"],
|
|
|
|
};
|
|
|
|
dialog.showMessageBoxSync(options);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-04-09 20:11:24 +02:00
|
|
|
const models = await getModels(savedCustomModelsPath);
|
2024-10-04 11:15:54 +02:00
|
|
|
mainWindow.webContents.send(
|
|
|
|
ELECTRON_COMMANDS.CUSTOM_MODEL_FILES_LIST,
|
|
|
|
models,
|
|
|
|
);
|
2023-09-10 11:14:04 +02:00
|
|
|
|
2024-04-09 20:11:24 +02:00
|
|
|
logit("📁 Custom Folder Path: ", savedCustomModelsPath);
|
|
|
|
return savedCustomModelsPath;
|
2023-09-10 11:14:04 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export default customModelsSelect;
|