1
0
mirror of https://github.com/upscayl/upscayl.git synced 2025-01-24 23:13:45 +01:00
upscayl/electron/commands/custom-models-select.ts

66 lines
2.0 KiB
TypeScript
Raw Normal View History

2023-09-10 14:44:04 +05:30
import { MessageBoxOptions, dialog } from "electron";
import {
2024-04-09 23:41:24 +05:30
savedCustomModelsPath,
setSavedCustomModelsPath,
2023-09-10 14:44:04 +05:30
} from "../utils/config-variables";
import logit from "../utils/logit";
import slash from "../utils/slash";
import COMMAND from "../../common/commands";
2023-09-10 14:44:04 +05:30
import getModels from "../utils/get-models";
2023-09-10 23:12:18 +05:30
import { getMainWindow } from "../main-window";
2023-11-10 17:11:35 +05:30
import settings from "electron-settings";
2023-11-23 11:09:46 +05:30
import { featureFlags } from "../../common/feature-flags";
2023-09-10 23:12:18 +05:30
2023-09-10 14:44:04 +05:30
const customModelsSelect = async (event, message) => {
2023-09-10 23:24:08 +05:30
const mainWindow = getMainWindow();
2023-09-10 14:44:04 +05:30
if (!mainWindow) return;
2023-11-02 20:33:21 +05:30
const {
canceled,
filePaths: folderPaths,
bookmarks,
} = await dialog.showOpenDialog({
2023-09-10 14:44:04 +05:30
properties: ["openDirectory"],
title: "Select Custom Models Folder",
2024-04-09 23:41:24 +05:30
defaultPath: savedCustomModelsPath,
2023-11-02 20:33:21 +05:30
securityScopedBookmarks: true,
2023-11-10 17:11:35 +05:30
message: "Select Custom Models Folder that is named 'models'",
2023-09-10 14:44:04 +05:30
});
2023-11-02 20:33:21 +05:30
2023-11-23 11:09:46 +05:30
if (featureFlags.APP_STORE_BUILD && bookmarks && bookmarks.length > 0) {
2023-11-10 17:11:35 +05:30
console.log("🚨 Setting Bookmark: ", bookmarks);
await settings.set("custom-models-bookmarks", bookmarks[0]);
2023-11-02 20:33:21 +05:30
}
2023-09-10 14:44:04 +05:30
if (canceled) {
logit("🚫 Select Custom Models Folder Operation Cancelled");
return null;
} else {
2024-04-09 23:41:24 +05:30
setSavedCustomModelsPath(folderPaths[0]);
2023-09-10 14:44:04 +05:30
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 23:41:24 +05:30
const models = await getModels(savedCustomModelsPath);
2023-11-10 17:11:35 +05:30
mainWindow.webContents.send(COMMAND.CUSTOM_MODEL_FILES_LIST, models);
2023-09-10 14:44:04 +05:30
2024-04-09 23:41:24 +05:30
logit("📁 Custom Folder Path: ", savedCustomModelsPath);
return savedCustomModelsPath;
2023-09-10 14:44:04 +05:30
}
};
export default customModelsSelect;