1
0
mirror of https://github.com/upscayl/upscayl.git synced 2024-12-14 08:32:55 +01:00
upscayl/electron/commands/custom-models-select.ts
2023-11-23 11:09:46 +05:30

66 lines
2.0 KiB
TypeScript

import { MessageBoxOptions, dialog } from "electron";
import {
customModelsFolderPath,
setCustomModelsFolderPath,
} from "../utils/config-variables";
import logit from "../utils/logit";
import slash from "../utils/slash";
import COMMAND from "../constants/commands";
import getModels from "../utils/get-models";
import { getMainWindow } from "../main-window";
import settings from "electron-settings";
import { featureFlags } from "../../common/feature-flags";
const customModelsSelect = async (event, message) => {
const mainWindow = getMainWindow();
if (!mainWindow) return;
const {
canceled,
filePaths: folderPaths,
bookmarks,
} = await dialog.showOpenDialog({
properties: ["openDirectory"],
title: "Select Custom Models Folder",
defaultPath: customModelsFolderPath,
securityScopedBookmarks: true,
message: "Select Custom Models Folder that is named 'models'",
});
if (featureFlags.APP_STORE_BUILD && bookmarks && bookmarks.length > 0) {
console.log("🚨 Setting Bookmark: ", bookmarks);
await settings.set("custom-models-bookmarks", bookmarks[0]);
}
if (canceled) {
logit("🚫 Select Custom Models Folder Operation Cancelled");
return null;
} else {
setCustomModelsFolderPath(folderPaths[0]);
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;
}
const models = await getModels(customModelsFolderPath);
mainWindow.webContents.send(COMMAND.CUSTOM_MODEL_FILES_LIST, models);
logit("📁 Custom Folder Path: ", customModelsFolderPath);
return customModelsFolderPath;
}
};
export default customModelsSelect;