1
0
mirror of https://github.com/upscayl/upscayl.git synced 2024-12-18 10:26:04 +01:00
upscayl/electron/utils/get-models.ts
NayamAmarshe 95843ded88
Refactor Renderer Code (#987)
* Initial refactor

* Remove unused imports

* Update code

* Refactor and Update Code

- Change file names to kebab-caase
- Add new useTranslation Hook
- Change useLog hook name to useLogger
- Update translation hook to provide autocomplete

* Update import and component name

* Rename files and components

* Update locales

* Update electron commands

* Update var

* Change Lowercase

* Replace filter with map

* Add props

* Update flag check

* Add validate paths

* Update formats

* Update import

* Update function

* Update function and translation

* Update handlePaste
2024-10-04 14:45:54 +05:30

76 lines
2.2 KiB
TypeScript

import fs from "fs";
import logit from "./logit";
import { MessageBoxOptions, app, dialog } from "electron";
import settings from "electron-settings";
import { FEATURE_FLAGS } from "../../common/feature-flags";
const getModels = async (folderPath: string | undefined) => {
let models: string[] = [];
let isValid = false;
// SECURITY SCOPED BOOKMARKS
let closeAccess;
const customModelsBookmarks = await settings.get("custom-models-bookmarks");
if (FEATURE_FLAGS.APP_STORE_BUILD && customModelsBookmarks) {
console.log(
"🚀 => file: get-models.ts:18 => customModelsBookmarks:",
customModelsBookmarks,
);
try {
closeAccess = app.startAccessingSecurityScopedResource(
customModelsBookmarks as string,
);
} catch (error) {
logit("📁 Custom Models Bookmarks Error: ", error);
}
}
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;