1
0
mirror of https://github.com/upscayl/upscayl.git synced 2024-12-24 05:14:57 +01:00
upscayl/electron/utils/get-models.ts

76 lines
2.2 KiB
TypeScript
Raw Normal View History

2023-09-10 11:14:04 +02:00
import fs from "fs";
import logit from "./logit";
2023-11-10 12:41:35 +01:00
import { MessageBoxOptions, app, dialog } from "electron";
import settings from "electron-settings";
2023-11-23 06:39:46 +01:00
import { featureFlags } from "../../common/feature-flags";
2023-09-10 11:14:04 +02:00
2023-11-10 12:41:35 +01:00
const getModels = async (folderPath: string | undefined) => {
2023-09-10 11:14:04 +02:00
let models: string[] = [];
let isValid = false;
2023-11-10 12:41:35 +01:00
// SECURITY SCOPED BOOKMARKS
let closeAccess;
const customModelsBookmarks = await settings.get("custom-models-bookmarks");
2023-11-23 06:39:46 +01:00
if (featureFlags.APP_STORE_BUILD && customModelsBookmarks) {
2023-11-10 12:41:35 +01:00
console.log(
"🚀 => file: get-models.ts:18 => customModelsBookmarks:",
customModelsBookmarks
);
try {
closeAccess = app.startAccessingSecurityScopedResource(
customModelsBookmarks as string
);
} catch (error) {
logit("📁 Custom Models Bookmarks Error: ", error);
}
}
2023-09-10 11:14:04 +02:00
if (!folderPath) {
logit("❌ Invalid Custom Model Folder Detected");
const options: MessageBoxOptions = {
type: "error",
title: "Invalid Folder",
message:
"The selected folder does not contain valid model files. Make sure you select the folder that ONLY contains '.param' and '.bin' files.",
buttons: ["OK"],
};
dialog.showMessageBoxSync(options);
return null;
}
// READ CUSTOM MODELS FOLDER
fs.readdirSync(folderPath).forEach((file) => {
// log.log("Files in Folder: ", file);
if (
file.endsWith(".param") ||
file.endsWith(".PARAM") ||
file.endsWith(".bin") ||
file.endsWith(".BIN")
) {
isValid = true;
const modelName = file.substring(0, file.lastIndexOf(".")) || file;
if (!models.includes(modelName)) {
models.push(modelName);
}
}
});
if (!isValid) {
logit("❌ Invalid Custom Model Folder Detected");
const options: MessageBoxOptions = {
type: "error",
title: "Invalid Folder",
message:
"The selected folder does not contain valid model files. Make sure you select the folder that ONLY contains '.param' and '.bin' files.",
buttons: ["OK"],
};
dialog.showMessageBoxSync(options);
return null;
}
logit("🔎 Detected Custom Models: ", models);
return models;
};
export default getModels;