1
0
mirror of https://github.com/upscayl/upscayl.git synced 2025-02-15 10:22:37 +01:00
upscayl/electron/commands/custom-models-select.ts

69 lines
2.1 KiB
TypeScript
Raw Permalink 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 { ELECTRON_COMMANDS } from "../../common/electron-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";
import { FEATURE_FLAGS } 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
if (FEATURE_FLAGS.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);
mainWindow.webContents.send(
ELECTRON_COMMANDS.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;