1
0
mirror of https://github.com/upscayl/upscayl.git synced 2025-02-23 13:59:26 +01:00
upscayl/electron/index.ts

819 lines
23 KiB
TypeScript
Raw Normal View History

2022-08-15 10:23:14 +05:30
// Native
2022-11-12 02:09:28 +05:30
import { autoUpdater } from "electron-updater";
2022-11-12 02:17:00 +05:30
import getPlatform from "./getPlatform";
2023-08-10 15:14:44 +05:30
import { join, parse } from "path";
2023-03-31 16:03:48 +05:30
import log from "electron-log";
2023-03-18 17:51:02 +05:30
import { format } from "url";
import fs from "fs";
2022-08-15 10:23:14 +05:30
2022-11-12 02:09:28 +05:30
import { execPath, modelsPath } from "./binaries";
2022-08-15 10:23:14 +05:30
// Packages
2022-11-12 02:09:28 +05:30
import {
2022-08-17 08:07:50 +05:30
BrowserWindow,
app,
ipcMain,
dialog,
2022-08-18 15:23:23 +05:30
shell,
2022-11-12 02:09:28 +05:30
MessageBoxOptions,
2023-07-22 18:30:46 +05:30
protocol,
2022-11-12 02:09:28 +05:30
} from "electron";
2022-09-25 16:45:43 +05:30
2022-11-12 02:09:28 +05:30
import prepareNext from "electron-next";
2023-03-18 17:51:02 +05:30
import isDev from "electron-is-dev";
2022-11-12 02:09:28 +05:30
import commands from "./commands";
2023-04-28 23:51:42 +05:30
import { ChildProcessWithoutNullStreams } from "child_process";
2023-08-10 15:14:44 +05:30
import {
getBatchArguments,
getDoubleUpscaleArguments,
getDoubleUpscaleSecondPassArguments,
getSingleImageArguments,
} from "./utils/getArguments";
import { spawnUpscayl } from "./upscayl";
import Jimp from "jimp";
2023-04-28 23:51:42 +05:30
2023-04-30 07:04:25 +05:30
let childProcesses: {
process: ChildProcessWithoutNullStreams;
kill: () => boolean;
}[] = [];
2022-08-15 10:23:14 +05:30
2023-03-31 16:03:48 +05:30
log.initialize({ preload: true });
2023-04-23 13:26:49 +05:30
function escapeRegExp(string) {
2023-04-27 23:48:44 +05:30
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
2023-04-23 13:26:49 +05:30
}
2023-04-28 18:17:51 +05:30
// Path variables for file and folder selection
let imagePath: string | undefined = undefined;
let folderPath: string | undefined = undefined;
let customModelsFolderPath: string | undefined = undefined;
let outputFolderPath: string | undefined = undefined;
let saveOutputFolder = false;
let quality = 100;
2023-08-10 15:49:29 +05:30
let overwrite = false;
2023-04-28 18:17:51 +05:30
2023-04-30 07:04:25 +05:30
let stopped = false;
2023-04-28 20:18:19 -04:00
// Slashes for use in directory names
const slash: string = getPlatform() === "win" ? "\\" : "/";
2022-08-15 10:23:14 +05:30
// Prepare the renderer once the app is ready
2023-08-06 18:19:22 +05:30
let mainWindow: BrowserWindow | null = null;
2022-08-15 10:23:14 +05:30
app.on("ready", async () => {
await prepareNext("./renderer");
2023-03-31 16:03:48 +05:30
log.info("🚀 UPSCAYL EXEC PATH: ", execPath(""));
log.info("🚀 MODELS PATH: ", modelsPath);
2022-09-07 08:01:28 +05:30
2022-08-17 17:06:19 +05:30
mainWindow = new BrowserWindow({
2022-09-30 22:09:14 +05:30
icon: join(__dirname, "build", "icon.png"),
2022-12-22 20:29:11 +05:30
width: 1300,
height: 940,
2022-08-18 15:23:23 +05:30
minHeight: 500,
minWidth: 500,
show: false,
2022-08-27 19:49:19 -04:00
backgroundColor: "#171717",
2022-08-15 10:23:14 +05:30
webPreferences: {
2022-08-16 07:47:27 +05:30
nodeIntegration: true,
2023-07-22 18:30:46 +05:30
nodeIntegrationInWorker: true,
2022-08-18 15:23:23 +05:30
webSecurity: false,
2022-08-15 10:23:14 +05:30
preload: join(__dirname, "preload.js"),
},
2023-08-30 10:24:16 +05:30
titleBarStyle: getPlatform() === "mac" ? "hiddenInset" : "default",
2022-08-15 10:23:14 +05:30
});
const url = isDev
? "http://localhost:8000"
: format({
pathname: join(__dirname, "../renderer/out/index.html"),
protocol: "file:",
slashes: true,
});
mainWindow.setMenuBarVisibility(false);
mainWindow.loadURL(url);
2022-08-18 15:23:23 +05:30
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
shell.openExternal(url);
return { action: "deny" };
});
2022-08-21 20:41:11 +05:30
2022-08-30 14:15:06 +05:30
mainWindow.once("ready-to-show", () => {
2023-08-06 18:19:22 +05:30
if (!mainWindow) return;
2022-08-30 14:15:06 +05:30
mainWindow.show();
2022-09-30 22:06:16 +05:30
mainWindow.webContents.setZoomFactor(1);
2022-08-30 14:15:06 +05:30
});
2023-07-22 18:30:46 +05:30
app.whenReady().then(() => {
protocol.registerFileProtocol("file", (request, callback) => {
const pathname = decodeURI(request.url.replace("file:///", ""));
callback(pathname);
});
});
2022-08-21 20:41:11 +05:30
if (!isDev) {
autoUpdater.checkForUpdates();
2022-08-23 08:43:08 +05:30
}
2023-04-15 10:45:58 +05:30
2023-08-06 18:19:22 +05:30
// <------------------------Save Last Paths----------------------------->
2023-04-28 18:17:51 +05:30
// GET LAST IMAGE PATH TO LOCAL STORAGE
2023-04-22 23:14:16 +05:30
mainWindow.webContents
.executeJavaScript('localStorage.getItem("lastImagePath");', true)
.then((lastImagePath: string | null) => {
if (lastImagePath && lastImagePath.length > 0) {
imagePath = lastImagePath;
}
});
2023-04-28 18:17:51 +05:30
// GET LAST FOLDER PATH TO LOCAL STORAGE
2023-04-22 23:14:16 +05:30
mainWindow.webContents
.executeJavaScript('localStorage.getItem("lastFolderPath");', true)
.then((lastFolderPath: string | null) => {
if (lastFolderPath && lastFolderPath.length > 0) {
folderPath = lastFolderPath;
}
});
2023-04-28 18:17:51 +05:30
// GET LAST CUSTOM MODELS FOLDER PATH TO LOCAL STORAGE
2023-04-22 23:14:16 +05:30
mainWindow.webContents
.executeJavaScript(
'localStorage.getItem("lastCustomModelsFolderPath");',
true
)
.then((lastCustomModelsFolderPath: string | null) => {
if (lastCustomModelsFolderPath && lastCustomModelsFolderPath.length > 0) {
customModelsFolderPath = lastCustomModelsFolderPath;
}
});
2023-04-28 18:17:51 +05:30
// GET LAST CUSTOM MODELS FOLDER PATH TO LOCAL STORAGE
2023-04-22 23:14:16 +05:30
mainWindow.webContents
.executeJavaScript('localStorage.getItem("lastOutputFolderPath");', true)
.then((lastOutputFolderPath: string | null) => {
if (lastOutputFolderPath && lastOutputFolderPath.length > 0) {
outputFolderPath = lastOutputFolderPath;
}
});
2023-04-28 18:17:51 +05:30
// GET LAST SAVE OUTPUT FOLDER (BOOLEAN) TO LOCAL STORAGE
mainWindow.webContents
.executeJavaScript('localStorage.getItem("rememberOutputFolder");', true)
.then((lastSaveOutputFolder: boolean | null) => {
if (lastSaveOutputFolder !== null) {
saveOutputFolder = lastSaveOutputFolder;
}
});
// GET IMAGE QUALITY (NUMBER) TO LOCAL STORAGE
mainWindow.webContents
.executeJavaScript('localStorage.getItem("quality");', true)
2023-08-30 10:24:16 +05:30
.then((lastSavedQuality: string | null) => {
if (lastSavedQuality !== null) {
quality = parseInt(lastSavedQuality);
2023-08-10 15:49:29 +05:30
}
});
2023-08-14 16:25:30 +05:30
mainWindow.webContents.send(commands.OS, getPlatform());
2022-08-15 10:23:14 +05:30
});
// Quit the app once all windows are closed
app.on("window-all-closed", app.quit);
2023-05-01 12:25:56 +05:30
log.log("🚃 App Path: ", app.getAppPath());
2023-03-31 16:03:48 +05:30
2023-04-14 16:01:37 +05:30
const logit = (...args: any) => {
log.log(...args);
2023-08-06 18:19:22 +05:30
if (!mainWindow) return;
2023-04-14 16:01:37 +05:30
mainWindow.webContents.send(commands.LOG, args.join(" "));
};
2023-04-09 11:34:36 +05:30
// Default models
const defaultModels = [
"realesrgan-x4plus",
"remacri",
"ultramix_balanced",
"ultrasharp",
"realesrgan-x4plus-anime",
];
2022-08-15 12:51:12 +05:30
// ! DONT FORGET TO RESTART THE APP WHEN YOU CHANGE CODE HERE
2023-04-09 09:23:01 +05:30
2023-04-09 10:46:15 +05:30
//------------------------Get Model Names-----------------------------//
const getModels = (folderPath: string) => {
let models: string[] = [];
let isValid = false;
// 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) {
2023-05-01 12:25:56 +05:30
logit("❌ Invalid Custom Model Folder Detected");
2023-04-09 10:46:15 +05:30
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);
2023-04-09 10:46:15 +05:30
return models;
};
2022-12-02 19:51:42 +05:30
//------------------------Open Folder-----------------------------//
ipcMain.on(commands.OPEN_FOLDER, async (event, payload) => {
logit("📂 Opening Folder: ", payload);
2022-12-02 19:51:42 +05:30
shell.openPath(payload);
});
2023-04-30 07:04:25 +05:30
//------------------------Stop Command-----------------------------//
2023-04-28 23:51:42 +05:30
ipcMain.on(commands.STOP, async (event, payload) => {
2023-04-30 07:04:25 +05:30
stopped = true;
childProcesses.forEach((child) => {
logit("🛑 Stopping Upscaling Process", child.process.pid);
2023-04-28 23:51:42 +05:30
child.kill();
2023-04-30 07:04:25 +05:30
});
2023-04-28 23:51:42 +05:30
});
2023-08-10 15:14:44 +05:30
//------------------------Select Folder-----------------------------//
ipcMain.handle(commands.SELECT_FOLDER, async (event, message) => {
const { canceled, filePaths: folderPaths } = await dialog.showOpenDialog({
properties: ["openDirectory"],
defaultPath: folderPath,
2023-08-06 18:19:22 +05:30
});
2023-08-10 15:14:44 +05:30
if (canceled) {
logit("🚫 Select Folder Operation Cancelled");
return null;
} else {
folderPath = folderPaths[0];
logit("📁 Selected Folder Path: ", folderPath);
return folderPaths[0];
}
});
//------------------------Select File-----------------------------//
ipcMain.handle(commands.SELECT_FILE, async () => {
if (!mainWindow) return;
const { canceled, filePaths } = await dialog.showOpenDialog({
properties: ["openFile", "multiSelections"],
title: "Select Image",
defaultPath: imagePath,
2023-08-06 18:19:22 +05:30
});
2023-08-10 15:14:44 +05:30
if (canceled) {
logit("🚫 File Operation Cancelled");
return null;
} else {
imagePath = filePaths[0];
let isValid = false;
// READ SELECTED FILES
filePaths.forEach((file) => {
// log.log("Files in Folder: ", file);
if (
file.endsWith(".png") ||
file.endsWith(".jpg") ||
file.endsWith(".jpeg") ||
file.endsWith(".webp") ||
file.endsWith(".JPG") ||
file.endsWith(".PNG") ||
file.endsWith(".JPEG") ||
file.endsWith(".WEBP")
) {
isValid = true;
}
});
if (!isValid) {
logit("❌ Invalid File Detected");
const options: MessageBoxOptions = {
type: "error",
title: "Invalid File",
message:
"The selected file is not a valid image. Make sure you select a '.png', '.jpg', or '.webp' file.",
};
dialog.showMessageBoxSync(mainWindow, options);
return null;
}
logit("📄 Selected File Path: ", filePaths[0]);
// CREATE input AND upscaled FOLDER
return filePaths[0];
}
});
//------------------------Get Models List-----------------------------//
ipcMain.on(commands.GET_MODELS_LIST, async (event, payload) => {
if (!mainWindow) return;
if (payload) {
customModelsFolderPath = payload;
logit("📁 Custom Models Folder Path: ", customModelsFolderPath);
mainWindow.webContents.send(
commands.CUSTOM_MODEL_FILES_LIST,
getModels(payload)
);
}
});
//------------------------Custom Models Select-----------------------------//
ipcMain.handle(commands.SELECT_CUSTOM_MODEL_FOLDER, async (event, message) => {
if (!mainWindow) return;
const { canceled, filePaths: folderPaths } = await dialog.showOpenDialog({
properties: ["openDirectory"],
title: "Select Custom Models Folder",
defaultPath: customModelsFolderPath,
2023-08-06 18:19:22 +05:30
});
2023-08-10 15:14:44 +05:30
if (canceled) {
logit("🚫 Select Custom Models Folder Operation Cancelled");
return null;
} else {
customModelsFolderPath = folderPaths[0];
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;
}
mainWindow.webContents.send(
commands.CUSTOM_MODEL_FILES_LIST,
getModels(customModelsFolderPath)
);
logit("📁 Custom Folder Path: ", customModelsFolderPath);
return customModelsFolderPath;
}
});
//------------------------Image Upscayl-----------------------------//
ipcMain.on(commands.UPSCAYL, async (event, payload) => {
if (!mainWindow) return;
2023-08-27 21:41:01 +05:30
overwrite = payload.overwrite;
2023-08-10 15:14:44 +05:30
const model = payload.model as string;
const gpuId = payload.gpuId as string;
const saveImageAs = payload.saveImageAs as string;
let inputDir = (payload.imagePath.match(/(.*)[\/\\]/)[1] || "") as string;
let outputDir = folderPath || (payload.outputPath as string);
if (saveOutputFolder === true && outputFolderPath) {
outputDir = outputFolderPath;
}
const isDefaultModel = defaultModels.includes(model);
const fullfileName = payload.imagePath.replace(/^.*[\\\/]/, "") as string;
const fileName = parse(fullfileName).name;
const fileExt = parse(fullfileName).ext;
let scale = "4";
if (model.includes("x2")) {
scale = "2";
} else if (model.includes("x3")) {
scale = "3";
} else {
scale = "4";
}
const outFile =
outputDir +
slash +
fileName +
"_upscayl_" +
payload.scale +
"x_" +
model +
"." +
saveImageAs;
2023-08-30 10:24:16 +05:30
// GET OVERWRITE SETTINGS FROM LOCAL STORAGE
mainWindow.webContents
.executeJavaScript('localStorage.getItem("overwrite");', true)
.then((lastSavedOverwrite: boolean | null) => {
if (lastSavedOverwrite !== null) {
console.log("Overwrite: ", lastSavedOverwrite);
overwrite = lastSavedOverwrite;
}
});
2023-08-10 15:14:44 +05:30
// UPSCALE
2023-08-30 10:24:16 +05:30
if (fs.existsSync(outFile) && overwrite === false) {
2023-08-10 15:14:44 +05:30
// If already upscayled, just output that file
logit("✅ Already upscayled at: ", outFile);
mainWindow.webContents.send(commands.UPSCAYL_DONE, outFile);
} else {
const upscayl = spawnUpscayl(
"realesrgan",
getSingleImageArguments(
inputDir,
fullfileName,
outFile,
isDefaultModel ? modelsPath : customModelsFolderPath ?? modelsPath,
model,
scale,
gpuId,
saveImageAs
),
logit
);
childProcesses.push(upscayl);
stopped = false;
let isAlpha = false;
let failed = false;
const onData = (data: string) => {
if (!mainWindow) return;
logit("image upscayl: ", data.toString());
mainWindow.setProgressBar(parseFloat(data.slice(0, data.length)) / 100);
data = data.toString();
mainWindow.webContents.send(commands.UPSCAYL_PROGRESS, data.toString());
if (data.includes("invalid gpu") || data.includes("failed")) {
logit("❌ INVALID GPU OR FAILED");
2023-08-11 13:45:43 +05:30
upscayl.kill();
2023-08-10 15:14:44 +05:30
failed = true;
}
if (data.includes("has alpha channel")) {
logit("📢 INCLUDES ALPHA CHANNEL, CHANGING OUTFILE NAME!");
isAlpha = true;
}
};
const onError = (data) => {
if (!mainWindow) return;
mainWindow.webContents.send(commands.UPSCAYL_PROGRESS, data.toString());
failed = true;
2023-08-11 13:45:43 +05:30
upscayl.kill();
2023-08-10 15:14:44 +05:30
return;
};
const onClose = async () => {
if (!failed && !stopped) {
logit("💯 Done upscaling");
logit("♻ Scaling and converting now...");
const originalImage = await Jimp.read(inputDir + slash + fullfileName);
try {
const newImage = await Jimp.read(
isAlpha ? outFile + ".png" : outFile
);
try {
if (!mainWindow) return;
newImage
2023-08-10 15:47:35 +05:30
.quality(100 - quality)
2023-08-10 15:14:44 +05:30
.scaleToFit(
originalImage.getWidth() * parseInt(payload.scale),
originalImage.getHeight() * parseInt(payload.scale)
)
.write(isAlpha ? outFile + ".png" : outFile);
mainWindow.setProgressBar(-1);
mainWindow.webContents.send(
commands.UPSCAYL_DONE,
isAlpha ? outFile + ".png" : outFile
);
} catch (error) {
logit("❌ Error converting to PNG: ", error);
onError(error);
}
} catch (error) {
logit("❌ Error reading original image metadata", error);
onError(error);
}
}
};
upscayl.process.stderr.on("data", onData);
upscayl.process.on("error", onError);
upscayl.process.on("close", onClose);
}
});
//------------------------Folder Upscayl-----------------------------//
ipcMain.on(commands.FOLDER_UPSCAYL, async (event, payload) => {
if (!mainWindow) return;
// GET THE MODEL
const model = payload.model;
const gpuId = payload.gpuId;
const saveImageAs = payload.saveImageAs;
2023-08-11 15:15:27 +05:30
// const scale = payload.scale as string;
2023-08-10 15:14:44 +05:30
// GET THE IMAGE DIRECTORY
let inputDir = payload.batchFolderPath;
// GET THE OUTPUT DIRECTORY
let outputDir = payload.outputPath;
if (saveOutputFolder === true && outputFolderPath) {
outputDir = outputFolderPath;
}
2023-08-11 15:15:27 +05:30
const isDefaultModel = defaultModels.includes(model);
let scale = "4";
if (model.includes("x2")) {
scale = "2";
} else if (model.includes("x3")) {
scale = "3";
} else {
scale = "4";
}
outputDir += `_${model}_x${payload.scale}`;
2023-08-10 15:14:44 +05:30
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
2023-08-11 15:15:27 +05:30
// Delete .DS_Store files
fs.readdirSync(inputDir).forEach((file) => {
if (file === ".DS_Store") {
logit("🗑️ Deleting .DS_Store file");
fs.unlinkSync(inputDir + slash + file);
}
});
2023-08-10 15:14:44 +05:30
// UPSCALE
const upscayl = spawnUpscayl(
"realesrgan",
getBatchArguments(
inputDir,
outputDir,
isDefaultModel ? modelsPath : customModelsFolderPath ?? modelsPath,
model,
gpuId,
2023-08-11 15:15:27 +05:30
"png",
2023-08-10 15:14:44 +05:30
scale
),
logit
);
childProcesses.push(upscayl);
stopped = false;
let failed = false;
const onData = (data: any) => {
if (!mainWindow) return;
data = data.toString();
mainWindow.webContents.send(
commands.FOLDER_UPSCAYL_PROGRESS,
data.toString()
);
2023-08-11 15:15:27 +05:30
if (data.includes("invalid") || data.includes("failed")) {
2023-08-10 15:14:44 +05:30
logit("❌ INVALID GPU OR INVALID FILES IN FOLDER - FAILED");
failed = true;
upscayl.kill();
}
};
const onError = (data: any) => {
if (!mainWindow) return;
mainWindow.webContents.send(
commands.FOLDER_UPSCAYL_PROGRESS,
data.toString()
);
failed = true;
upscayl.kill();
return;
};
const onClose = () => {
if (!mainWindow) return;
if (!failed && !stopped) {
logit("💯 Done upscaling");
2023-08-11 15:15:27 +05:30
logit("♻ Scaling and converting now...");
// Get number of files in output folder
const files = fs.readdirSync(inputDir);
files.forEach(async (file) => {
console.log("Filename: ", file.slice(0, -3));
// Resize the image to the original size
const originalImage = await Jimp.read(inputDir + slash + file);
const newImage = await Jimp.read(
outputDir + slash + file.slice(0, -3) + "png"
);
newImage
.quality(100 - quality)
.scaleToFit(
originalImage.getWidth() * parseInt(payload.scale),
originalImage.getHeight() * parseInt(payload.scale)
)
.write(outputDir + slash + file);
if (saveImageAs !== "png") {
fs.unlinkSync(outputDir + slash + file.slice(0, -3) + "png");
}
});
2023-08-10 15:14:44 +05:30
mainWindow.webContents.send(commands.FOLDER_UPSCAYL_DONE, outputDir);
} else {
upscayl.kill();
}
};
upscayl.process.stderr.on("data", onData);
upscayl.process.on("error", onError);
upscayl.process.on("close", onClose);
});
//------------------------Double Upscayl-----------------------------//
ipcMain.on(commands.DOUBLE_UPSCAYL, async (event, payload) => {
if (!mainWindow) return;
const model = payload.model as string;
let inputDir = (payload.imagePath.match(/(.*)[\/\\]/)[1] || "") as string;
let outputDir = payload.outputPath as string;
if (saveOutputFolder === true && outputFolderPath) {
outputDir = outputFolderPath;
}
const gpuId = payload.gpuId as string;
const saveImageAs = payload.saveImageAs as string;
const isDefaultModel = defaultModels.includes(model);
// COPY IMAGE TO TMP FOLDER
const fullfileName = payload.imagePath.split(slash).slice(-1)[0] as string;
const fileName = parse(fullfileName).name;
const outFile =
outputDir + slash + fileName + "_upscayl_16x_" + model + "." + saveImageAs;
let scale = "4";
if (model.includes("x2")) {
scale = "2";
} else if (model.includes("x3")) {
scale = "3";
} else {
scale = "4";
}
// UPSCALE
let upscayl = spawnUpscayl(
"realesrgan",
getDoubleUpscaleArguments(
inputDir,
fullfileName,
outFile,
isDefaultModel ? modelsPath : customModelsFolderPath ?? modelsPath,
model,
gpuId,
saveImageAs,
scale
),
logit
);
childProcesses.push(upscayl);
stopped = false;
let failed = false;
let isAlpha = false;
let failed2 = false;
const onData = (data) => {
if (!mainWindow) return;
// CONVERT DATA TO STRING
data = data.toString();
// SEND UPSCAYL PROGRESS TO RENDERER
mainWindow.webContents.send(commands.DOUBLE_UPSCAYL_PROGRESS, data);
// IF PROGRESS HAS ERROR, UPSCAYL FAILED
if (data.includes("invalid gpu") || data.includes("failed")) {
2023-08-11 13:45:43 +05:30
upscayl.kill();
2023-08-10 15:14:44 +05:30
failed = true;
}
if (data.includes("has alpha channel")) {
isAlpha = true;
}
};
const onError = (data) => {
if (!mainWindow) return;
data.toString();
// SEND UPSCAYL PROGRESS TO RENDERER
mainWindow.webContents.send(commands.DOUBLE_UPSCAYL_PROGRESS, data);
// SET FAILED TO TRUE
failed = true;
2023-08-11 13:45:43 +05:30
upscayl.kill();
2023-08-10 15:14:44 +05:30
return;
};
const onClose2 = async (code) => {
if (!mainWindow) return;
if (!failed2 && !stopped) {
logit("💯 Done upscaling");
logit("♻ Scaling and converting now...");
const originalImage = await Jimp.read(inputDir + slash + fullfileName);
try {
const newImage = await Jimp.read(isAlpha ? outFile + ".png" : outFile);
try {
newImage
2023-08-11 13:45:43 +05:30
.quality(100 - quality)
2023-08-10 15:14:44 +05:30
.scaleToFit(
originalImage.getWidth() * parseInt(payload.scale),
originalImage.getHeight() * parseInt(payload.scale)
)
.write(isAlpha ? outFile + ".png" : outFile);
mainWindow.setProgressBar(-1);
mainWindow.webContents.send(
commands.DOUBLE_UPSCAYL_DONE,
isAlpha ? outFile + ".png" : outFile
);
} catch (error) {
logit("❌ Error converting to PNG: ", error);
onError(error);
}
} catch (error) {
logit("❌ Error reading original image metadata", error);
onError(error);
}
}
};
upscayl.process.stderr.on("data", onData);
upscayl.process.on("error", onError);
upscayl.process.on("close", (code) => {
// IF NOT FAILED
if (!failed && !stopped) {
// UPSCALE
let upscayl2 = spawnUpscayl(
"realesrgan",
getDoubleUpscaleSecondPassArguments(
isAlpha,
outFile,
isDefaultModel ? modelsPath : customModelsFolderPath ?? modelsPath,
model,
gpuId,
saveImageAs,
scale
),
logit
);
childProcesses.push(upscayl2);
2023-08-11 13:45:43 +05:30
upscayl2.process.stderr.on("data", (data) => {
if (!mainWindow) return;
// CONVERT DATA TO STRING
data = data.toString();
// SEND UPSCAYL PROGRESS TO RENDERER
mainWindow.webContents.send(commands.DOUBLE_UPSCAYL_PROGRESS, data);
// IF PROGRESS HAS ERROR, UPSCAYL FAILED
if (data.includes("invalid gpu") || data.includes("failed")) {
upscayl2.kill();
failed2 = true;
}
});
upscayl2.process.on("error", (data) => {
if (!mainWindow) return;
data.toString();
// SEND UPSCAYL PROGRESS TO RENDERER
mainWindow.webContents.send(commands.DOUBLE_UPSCAYL_PROGRESS, data);
// SET FAILED TO TRUE
failed2 = true;
upscayl2.kill();
return;
});
2023-08-10 15:14:44 +05:30
upscayl2.process.on("close", onClose2);
}
2023-08-06 18:19:22 +05:30
});
2023-08-10 15:14:44 +05:30
});
//------------------------Auto-Update Code-----------------------------//
2023-06-04 13:22:18 +05:30
autoUpdater.autoInstallOnAppQuit = false;
2022-11-12 02:09:28 +05:30
autoUpdater.on("update-downloaded", (event) => {
2023-06-04 13:22:18 +05:30
autoUpdater.autoInstallOnAppQuit = false;
2022-11-12 02:09:28 +05:30
const dialogOpts: MessageBoxOptions = {
2022-08-23 08:43:08 +05:30
type: "info",
2023-06-04 13:22:18 +05:30
buttons: ["Install update", "No Thanks"],
2023-03-18 17:50:32 +05:30
title: "New Upscayl Update",
2023-03-18 17:47:06 +05:30
message: event.releaseName as string,
2022-08-23 08:43:08 +05:30
detail:
"A new version has been downloaded. Restart the application to apply the updates.",
2022-08-21 20:41:11 +05:30
};
logit("✅ Update Downloaded");
2022-08-21 20:41:11 +05:30
dialog.showMessageBox(dialogOpts).then((returnValue) => {
2023-06-04 13:22:18 +05:30
if (returnValue.response === 0) {
autoUpdater.quitAndInstall();
} else {
logit("🚫 Update Installation Cancelled");
}
2022-08-23 08:43:08 +05:30
});
2022-08-21 20:41:11 +05:30
});