1
0
mirror of https://github.com/upscayl/upscayl.git synced 2024-12-21 03:45:52 +01:00
upscayl/electron/commands/custom-models-select.ts

66 lines
2.0 KiB
TypeScript
Raw Normal View History

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";
import COMMAND from "../../common/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";
2023-11-23 06:39:46 +01:00
import { featureFlags } 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
2023-11-23 06:39:46 +01:00
if (featureFlags.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);
2023-11-10 12:41:35 +01:00
mainWindow.webContents.send(COMMAND.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;