1
0
mirror of https://github.com/upscayl/upscayl.git synced 2025-02-17 19:19:23 +01:00
upscayl/main/index.js

450 lines
12 KiB
JavaScript
Raw Normal View History

2022-08-15 10:23:14 +05:30
// Native
2022-08-23 19:47:16 +05:30
const { join, parse } = require("path");
2022-08-15 10:23:14 +05:30
const { format } = require("url");
2022-08-16 07:47:27 +05:30
const { spawn } = require("child_process");
2022-08-15 10:23:14 +05:30
const fs = require("fs");
2022-08-18 15:23:23 +05:30
const sizeOf = require("image-size");
2022-08-21 20:41:11 +05:30
const { autoUpdater } = require("electron-updater");
2022-08-26 06:45:43 +05:30
const { getPlatform } = require("./getPlatform");
2022-08-15 10:23:14 +05:30
2022-08-16 07:47:27 +05:30
const { execPath, modelsPath } = require("./binaries");
2022-08-15 10:23:14 +05:30
// Packages
2022-08-17 08:07:50 +05:30
const {
BrowserWindow,
app,
ipcMain,
dialog,
ipcRenderer,
2022-08-18 15:23:23 +05:30
shell,
2022-08-17 08:07:50 +05:30
} = require("electron");
2022-09-25 16:45:43 +05:30
2022-08-15 10:23:14 +05:30
const isDev = require("electron-is-dev");
const prepareNext = require("electron-next");
2022-08-22 14:01:16 +05:30
const commands = require("./commands");
2022-08-15 10:23:14 +05:30
// Prepare the renderer once the app is ready
2022-08-17 17:06:19 +05:30
let mainWindow;
2022-08-15 10:23:14 +05:30
app.on("ready", async () => {
await prepareNext("./renderer");
2022-09-07 08:01:28 +05:30
console.log("🚀 Icon Path: ", join(__dirname, "icon.png"));
console.log("🚀 Development Mode? :", isDev);
2022-09-11 15:57:47 +05:30
console.log("🚀 RS Executable Path: ", execPath(""));
2022-09-07 08:01:28 +05:30
console.log("🚀 Models: ", modelsPath);
2022-08-17 17:06:19 +05:30
mainWindow = new BrowserWindow({
2022-08-24 08:01:43 +05:30
icon: join(__dirname, "build", "icon.png"),
2022-08-15 12:36:31 +05:30
width: 1100,
2022-09-19 16:44:40 +05:30
height: 740,
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: {
autoHideMenuBar: true,
2022-08-16 07:47:27 +05:30
nodeIntegration: true,
2022-08-18 15:23:23 +05:30
webSecurity: false,
2022-08-15 10:23:14 +05:30
preload: join(__dirname, "preload.js"),
},
});
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", () => {
mainWindow.show();
});
2022-08-21 20:41:11 +05:30
if (!isDev) {
autoUpdater.checkForUpdates();
2022-08-23 08:43:08 +05:30
}
2022-08-15 10:23:14 +05:30
});
// Quit the app once all windows are closed
app.on("window-all-closed", app.quit);
2022-09-25 16:45:43 +05:30
console.log(app.getAppPath());
//------------------------Select File-----------------------------//
2022-08-15 12:51:12 +05:30
// ! DONT FORGET TO RESTART THE APP WHEN YOU CHANGE CODE HERE
2022-08-18 15:23:23 +05:30
ipcMain.handle(commands.SELECT_FILE, async () => {
2022-08-16 07:47:27 +05:30
const { canceled, filePaths } = await dialog.showOpenDialog({
properties: ["openFile", "multiSelections"],
});
2022-08-15 15:42:48 +05:30
if (canceled) {
2022-08-16 07:47:27 +05:30
console.log("operation cancelled");
return "cancelled";
} else {
console.log(filePaths[0]);
2022-08-29 19:11:59 +05:30
// CREATE input AND upscaled FOLDER
return filePaths[0];
}
2022-08-18 15:23:23 +05:30
});
2022-09-25 16:45:43 +05:30
//------------------------Select Folder-----------------------------//
2022-08-29 19:11:59 +05:30
ipcMain.handle(commands.SELECT_FOLDER, async (event, message) => {
const { canceled, filePaths } = await dialog.showOpenDialog({
properties: ["openDirectory"],
});
if (canceled) {
console.log("operation cancelled");
return "cancelled";
2022-08-18 15:23:23 +05:30
} else {
2022-08-29 19:11:59 +05:30
console.log(filePaths[0]);
return filePaths[0];
}
2022-08-18 15:23:23 +05:30
});
2022-08-16 07:47:27 +05:30
//------------------------Double Upscayl-----------------------------//
2022-09-17 15:10:47 +05:30
ipcMain.on(commands.DOUBLE_UPSCAYL, async (event, payload) => {
const model = payload.model;
2022-09-10 20:22:53 +05:30
let inputDir = payload.imagePath.match(/(.*)[\/\\]/)[1] || "";
2022-09-17 15:10:47 +05:30
let outputDir = payload.outputPath;
2022-09-10 20:22:53 +05:30
// COPY IMAGE TO TMP FOLDER
2022-09-17 15:10:47 +05:30
const platform = getPlatform();
2022-09-10 20:22:53 +05:30
const fullfileName =
platform === "win"
? payload.imagePath.split("\\").slice(-1)[0]
: payload.imagePath.split("/").slice(-1)[0];
const fileName = parse(fullfileName).name;
const fileExt = parse(fullfileName).ext;
2022-09-17 15:10:47 +05:30
const outFile = outputDir + "/" + fileName + "_upscayl_8x_" + model + fileExt;
2022-09-10 20:22:53 +05:30
2022-09-17 15:10:47 +05:30
// UPSCALE
let upscayl = spawn(
execPath("realesrgan"),
2022-09-11 15:57:47 +05:30
[
"-i",
2022-09-17 15:10:47 +05:30
inputDir + "/" + fullfileName,
2022-09-11 15:57:47 +05:30
"-o",
2022-09-17 15:10:47 +05:30
outFile,
2022-09-11 15:57:47 +05:30
"-s",
4,
"-m",
2022-09-17 15:10:47 +05:30
modelsPath,
"-n",
model,
2022-09-11 15:57:47 +05:30
],
{
cwd: null,
detached: false,
}
);
2022-09-10 20:22:53 +05:30
2022-09-11 15:57:47 +05:30
let failed = false;
2022-09-17 15:10:47 +05:30
// TAKE UPSCAYL OUTPUT
upscayl.stderr.on("data", (data) => {
// CONVERT DATA TO STRING
2022-09-11 15:57:47 +05:30
data = data.toString();
2022-09-17 15:10:47 +05:30
// PRINT TO CONSOLE
console.log(data);
// SEND UPSCAYL PROGRESS TO RENDERER
mainWindow.webContents.send(commands.DOUBLE_UPSCAYL_PROGRESS, data);
// IF PROGRESS HAS ERROR, UPSCAYL FAILED
2022-09-11 15:57:47 +05:30
if (data.includes("invalid gpu") || data.includes("failed")) {
failed = true;
}
});
2022-09-17 15:10:47 +05:30
// IF ERROR
upscayl.on("error", (data) => {
data.toString();
// SEND UPSCAYL PROGRESS TO RENDERER
mainWindow.webContents.send(commands.DOUBLE_UPSCAYL_PROGRESS, data);
// SET FAILED TO TRUE
failed = true;
return;
});
// ON UPSCAYL DONE
upscayl.on("close", (code) => {
// IF NOT FAILED
if (!failed) {
// UPSCALE
let upscayl2 = spawn(
execPath("realesrgan"),
2022-09-17 15:10:47 +05:30
["-i", outFile, "-o", outFile, "-s", 4, "-m", modelsPath, "-n", model],
{
cwd: null,
detached: false,
}
);
2022-09-17 15:10:47 +05:30
let failed2 = false;
// TAKE UPSCAYL OUTPUT
upscayl2.stderr.on("data", (data) => {
// CONVERT DATA TO STRING
data = data.toString();
2022-09-17 15:10:47 +05:30
// PRINT TO CONSOLE
console.log(data);
// 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")) {
2022-09-17 15:10:47 +05:30
failed2 = true;
}
});
2022-09-17 15:10:47 +05:30
// IF ERROR
upscayl2.on("error", (data) => {
data.toString();
// SEND UPSCAYL PROGRESS TO RENDERER
mainWindow.webContents.send(commands.DOUBLE_UPSCAYL_PROGRESS, data);
// SET FAILED TO TRUE
failed2 = true;
return;
});
2022-09-17 15:10:47 +05:30
upscayl2.on("close", (code) => {
if (!failed2) {
console.log("Done upscaling");
2022-09-17 15:10:47 +05:30
mainWindow.webContents.send(commands.DOUBLE_UPSCAYL_DONE, outFile);
}
2022-09-17 15:10:47 +05:30
});
2022-09-11 15:57:47 +05:30
}
2022-09-17 15:10:47 +05:30
});
2022-09-10 20:22:53 +05:30
});
//------------------------Image Upscayl-----------------------------//
2022-08-18 15:23:23 +05:30
ipcMain.on(commands.UPSCAYL, async (event, payload) => {
const model = payload.model;
const scale = payload.scaleFactor;
let inputDir = payload.imagePath.match(/(.*)[\/\\]/)[1] || "";
2022-08-29 19:11:59 +05:30
let outputDir = payload.outputPath;
2022-09-10 20:22:53 +05:30
// COPY IMAGE TO TMP FOLDER
const platform = getPlatform();
const fullfileName =
platform === "win"
? payload.imagePath.split("\\").slice(-1)[0]
: payload.imagePath.split("/").slice(-1)[0];
2022-09-17 15:10:47 +05:30
console.log(fullfileName);
const fileName = parse(fullfileName).name;
const fileExt = parse(fullfileName).ext;
const outFile = model.includes("realesrgan")
? outputDir + "/" + fileName + "_upscayl_" + scale + "x_" + model + fileExt
: outputDir +
"/" +
fileName +
"_upscayl_sharpened_" +
scale +
"x_" +
model +
fileExt;
// UPSCALE
2022-08-29 19:11:59 +05:30
if (fs.existsSync(outFile)) {
2022-09-07 08:01:28 +05:30
// If already upscayled, just output that file
2022-08-30 14:15:06 +05:30
mainWindow.webContents.send(commands.UPSCAYL_DONE, outFile);
} else {
let upscayl = null;
switch (model) {
case "realesrgan-x4plus":
case "realesrgan-x4plus-anime":
upscayl = spawn(
execPath("realesrgan"),
[
"-i",
inputDir + "/" + fullfileName,
"-o",
outFile,
"-s",
scale === 2 ? 4 : scale,
"-m",
modelsPath,
"-n",
model,
],
{
cwd: null,
detached: false,
}
);
break;
case "models-DF2K":
upscayl = spawn(
execPath("realsr"),
[
"-i",
inputDir + "/" + fullfileName,
"-o",
outFile,
"-s",
scale,
"-x",
"-m",
modelsPath + "/" + model,
],
{
cwd: null,
detached: false,
}
);
break;
}
2022-08-30 14:15:06 +05:30
2022-08-29 19:11:59 +05:30
let failed = false;
2022-09-11 17:04:36 +05:30
upscayl.stderr.on("data", (data) => {
console.log(
"🚀 => upscayl.stderr.on => stderr.toString()",
data.toString()
);
data = data.toString();
mainWindow.webContents.send(commands.UPSCAYL_PROGRESS, data.toString());
if (data.includes("invalid gpu") || data.includes("failed")) {
2022-08-29 19:11:59 +05:30
failed = true;
}
});
2022-08-30 14:15:06 +05:30
2022-09-11 17:04:36 +05:30
upscayl.on("error", (data) => {
mainWindow.webContents.send(commands.UPSCAYL_PROGRESS, data.toString());
failed = true;
return;
});
2022-09-07 08:01:28 +05:30
// Send done comamnd when
2022-08-29 19:11:59 +05:30
upscayl.on("close", (code) => {
if (failed !== true) {
console.log("Done upscaling");
2022-08-30 14:15:06 +05:30
mainWindow.webContents.send(commands.UPSCAYL_DONE, outFile);
2022-08-29 19:11:59 +05:30
}
});
}
2022-08-18 15:23:23 +05:30
});
2022-08-21 20:41:11 +05:30
//------------------------Upscayl Folder-----------------------------//
ipcMain.on(commands.FOLDER_UPSCAYL, async (event, payload) => {
const model = payload.model;
let inputDir = payload.batchFolderPath;
let outputDir = model.includes("realesrgan")
? payload.outputPath
: payload.outputPath + "_sharpened";
console.log(outputDir);
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
// UPSCALE
let upscayl = null;
switch (model) {
case "realesrgan-x4plus":
case "realesrgan-x4plus-anime":
upscayl = spawn(
execPath("realesrgan"),
[
"-i",
inputDir,
"-o",
outputDir,
"-s",
4,
"-m",
modelsPath,
"-n",
model,
],
{
cwd: null,
detached: false,
}
);
break;
case "models-DF2K":
upscayl = spawn(
execPath("realsr"),
[
"-i",
inputDir,
"-o",
outputDir,
"-s",
4,
"-x",
"-m",
modelsPath + "/" + model,
],
{
cwd: null,
detached: false,
}
);
break;
}
let failed = false;
upscayl.stderr.on("data", (data) => {
console.log(
"🚀 => upscayl.stderr.on => stderr.toString()",
data.toString()
);
data = data.toString();
mainWindow.webContents.send(
commands.FOLDER_UPSCAYL_PROGRESS,
data.toString()
);
if (data.includes("invalid gpu") || data.includes("failed")) {
failed = true;
}
});
upscayl.on("error", (data) => {
mainWindow.webContents.send(
commands.FOLDER_UPSCAYL_PROGRESS,
data.toString()
);
failed = true;
return;
});
// Send done comamnd when
upscayl.on("close", (code) => {
if (failed !== true) {
console.log("Done upscaling");
mainWindow.webContents.send(commands.FOLDER_UPSCAYL_DONE, outputDir);
}
});
});
ipcMain.on(commands.OPEN_FOLDER, async (event, payload) => {
console.log(payload);
shell.openPath(payload);
});
//------------------------Auto-Update Code-----------------------------//
2022-08-30 14:15:06 +05:30
// ! AUTO UPDATE STUFF
2022-08-21 20:41:11 +05:30
autoUpdater.on("update-available", (_event, releaseNotes, releaseName) => {
const dialogOpts = {
2022-08-23 08:43:08 +05:30
type: "info",
buttons: ["Ok"],
title: "Application Update",
message: process.platform === "win32" ? releaseNotes : releaseName,
detail: "A new version is being downloaded.",
};
dialog.showMessageBox(dialogOpts, (response) => {});
});
2022-08-21 20:41:11 +05:30
autoUpdater.on("update-downloaded", (_event, releaseNotes, releaseName) => {
const dialogOpts = {
2022-08-23 08:43:08 +05:30
type: "info",
buttons: ["Restart", "Later"],
title: "Application Update",
message: process.platform === "win32" ? releaseNotes : releaseName,
detail:
"A new version has been downloaded. Restart the application to apply the updates.",
2022-08-21 20:41:11 +05:30
};
dialog.showMessageBox(dialogOpts).then((returnValue) => {
2022-08-23 08:43:08 +05:30
if (returnValue.response === 0) autoUpdater.quitAndInstall();
});
2022-08-21 20:41:11 +05:30
});