1
0
mirror of https://github.com/upscayl/upscayl.git synced 2025-02-27 23:30:24 +01:00

Refactored Code

This commit is contained in:
Feenix 2023-03-12 15:19:02 +05:30
parent 3f1470adb5
commit 2888887fa8
3 changed files with 77 additions and 102 deletions

@ -23,6 +23,7 @@ import {
import isDev from "electron-is-dev"; import isDev from "electron-is-dev";
import prepareNext from "electron-next"; import prepareNext from "electron-next";
import commands from "./commands"; import commands from "./commands";
import { spawnUpscayl } from "./upscayl";
// Prepare the renderer once the app is ready // Prepare the renderer once the app is ready
let mainWindow; let mainWindow;
@ -314,92 +315,52 @@ ipcMain.on(commands.UPSCAYL, async (event, payload) => {
// If already upscayled, just output that file // If already upscayled, just output that file
mainWindow.webContents.send(commands.UPSCAYL_DONE, outFile); mainWindow.webContents.send(commands.UPSCAYL_DONE, outFile);
} else { } else {
let upscayl: ChildProcessWithoutNullStreams | null = null; let upscayl: ReturnType<typeof spawnUpscayl>;
const defaultArguments = [
"-i",
inputDir + "/" + fullfileName,
"-o",
outFile,
"-s",
scale === 2 ? 4 : scale,
"-m",
modelsPath,
"-n",
model,
gpuId ? `-g ${gpuId}` : "",
"-f",
saveImageAs,
];
const sharpenArguments = [
"-i",
inputDir + "/" + fullfileName,
"-o",
outFile,
"-s",
scale,
"-x",
"-m",
modelsPath + "/" + model,
gpuId ? `-g ${gpuId}` : "",
"-f",
saveImageAs,
];
switch (model) { switch (model) {
default: default:
upscayl = spawn( upscayl = spawnUpscayl(defaultArguments, "realesrgan");
execPath("realesrgan"),
[
"-i",
inputDir + "/" + fullfileName,
"-o",
outFile,
"-s",
scale === 2 ? 4 : scale,
"-m",
modelsPath,
"-n",
model,
gpuId ? `-g ${gpuId}` : "",
"-f",
saveImageAs,
],
{
cwd: undefined,
detached: false,
}
);
console.log(
"🆙 COMMAND: ",
"-i",
inputDir + "/" + fullfileName,
"-o",
outFile,
"-s",
scale === 2 ? 4 : scale,
"-m",
modelsPath,
"-n",
model,
gpuId ? `-g ${gpuId}` : "",
"-f",
saveImageAs
);
break; break;
case "models-DF2K": case "models-DF2K":
upscayl = spawn( upscayl = spawnUpscayl(sharpenArguments, "realsr");
execPath("realsr"),
[
"-i",
inputDir + "/" + fullfileName,
"-o",
outFile,
"-s",
scale,
"-x",
"-m",
modelsPath + "/" + model,
gpuId ? `-g ${gpuId}` : "",
"-f",
saveImageAs,
],
{
cwd: undefined,
detached: false,
}
);
console.log(
"🆙 COMMAND: ",
"-i",
inputDir + "/" + fullfileName,
"-o",
outFile,
"-s",
scale,
"-x",
"-m",
modelsPath + "/" + model,
gpuId ? `-g ${gpuId}` : "",
"-f",
saveImageAs
);
break; break;
} }
let isAlpha = false; let isAlpha = false;
let failed = false; let failed = false;
upscayl?.stderr.on("data", (data: string) => { const onData = (data: string) => {
console.log( console.log(
"🚀 => upscayl.stderr.on => stderr.toString()", "🚀 => upscayl.stderr.on => stderr.toString()",
data.toString() data.toString()
@ -413,16 +374,15 @@ ipcMain.on(commands.UPSCAYL, async (event, payload) => {
console.log("INCLUDES ALPHA CHANNEL, CHANGING OUTFILE NAME!"); console.log("INCLUDES ALPHA CHANNEL, CHANGING OUTFILE NAME!");
isAlpha = true; isAlpha = true;
} }
}); };
upscayl?.on("error", (data) => { const onError = (data) => {
mainWindow.webContents.send(commands.UPSCAYL_PROGRESS, data.toString()); mainWindow.webContents.send(commands.UPSCAYL_PROGRESS, data.toString());
failed = true; failed = true;
return; return;
}); };
// Send done comamnd when const onClose = () => {
upscayl?.on("close", (code) => {
if (failed !== true) { if (failed !== true) {
console.log("Done upscaling"); console.log("Done upscaling");
mainWindow.webContents.send( mainWindow.webContents.send(
@ -430,7 +390,11 @@ ipcMain.on(commands.UPSCAYL, async (event, payload) => {
isAlpha ? outFile + ".png" : outFile isAlpha ? outFile + ".png" : outFile
); );
} }
}); };
upscayl.process.stderr.on("data", onData);
upscayl.process.on("error", onError);
upscayl.process.on("close", onClose);
} }
}); });

16
electron/upscayl.ts Normal file

@ -0,0 +1,16 @@
import { spawn } from "child_process";
import { execPath } from "./binaries";
export const spawnUpscayl = (command: string[], binaryName: string) => {
console.log(" Command: ", command);
const spawnedProcess = spawn(execPath(binaryName), command, {
cwd: undefined,
detached: false,
});
return {
process: spawnedProcess,
kill: () => spawnedProcess.kill(),
};
};

@ -26,6 +26,7 @@ const electron_1 = require("electron");
const electron_is_dev_1 = __importDefault(require("electron-is-dev")); const electron_is_dev_1 = __importDefault(require("electron-is-dev"));
const electron_next_1 = __importDefault(require("electron-next")); const electron_next_1 = __importDefault(require("electron-next"));
const commands_1 = __importDefault(require("./commands")); const commands_1 = __importDefault(require("./commands"));
const upscayl_1 = require("./upscayl");
// Prepare the renderer once the app is ready // Prepare the renderer once the app is ready
let mainWindow; let mainWindow;
electron_1.app.on("ready", () => __awaiter(void 0, void 0, void 0, function* () { electron_1.app.on("ready", () => __awaiter(void 0, void 0, void 0, function* () {
@ -263,10 +264,10 @@ electron_1.ipcMain.on(commands_1.default.UPSCAYL, (event, payload) => __awaiter(
mainWindow.webContents.send(commands_1.default.UPSCAYL_DONE, outFile); mainWindow.webContents.send(commands_1.default.UPSCAYL_DONE, outFile);
} }
else { else {
let upscayl = null; let upscayl;
switch (model) { switch (model) {
default: default:
upscayl = (0, child_process_1.spawn)((0, binaries_1.execPath)("realesrgan"), [ upscayl = (0, upscayl_1.spawnUpscayl)([
"-i", "-i",
inputDir + "/" + fullfileName, inputDir + "/" + fullfileName,
"-o", "-o",
@ -280,14 +281,10 @@ electron_1.ipcMain.on(commands_1.default.UPSCAYL, (event, payload) => __awaiter(
gpuId ? `-g ${gpuId}` : "", gpuId ? `-g ${gpuId}` : "",
"-f", "-f",
saveImageAs, saveImageAs,
], { ], "realesrgan");
cwd: undefined,
detached: false,
});
console.log("🆙 COMMAND: ", "-i", inputDir + "/" + fullfileName, "-o", outFile, "-s", scale === 2 ? 4 : scale, "-m", binaries_1.modelsPath, "-n", model, gpuId ? `-g ${gpuId}` : "", "-f", saveImageAs);
break; break;
case "models-DF2K": case "models-DF2K":
upscayl = (0, child_process_1.spawn)((0, binaries_1.execPath)("realsr"), [ upscayl = (0, upscayl_1.spawnUpscayl)([
"-i", "-i",
inputDir + "/" + fullfileName, inputDir + "/" + fullfileName,
"-o", "-o",
@ -300,16 +297,12 @@ electron_1.ipcMain.on(commands_1.default.UPSCAYL, (event, payload) => __awaiter(
gpuId ? `-g ${gpuId}` : "", gpuId ? `-g ${gpuId}` : "",
"-f", "-f",
saveImageAs, saveImageAs,
], { ], "realsr");
cwd: undefined,
detached: false,
});
console.log("🆙 COMMAND: ", "-i", inputDir + "/" + fullfileName, "-o", outFile, "-s", scale, "-x", "-m", binaries_1.modelsPath + "/" + model, gpuId ? `-g ${gpuId}` : "", "-f", saveImageAs);
break; break;
} }
let isAlpha = false; let isAlpha = false;
let failed = false; let failed = false;
upscayl === null || upscayl === void 0 ? void 0 : upscayl.stderr.on("data", (data) => { const onData = (data) => {
console.log("🚀 => upscayl.stderr.on => stderr.toString()", data.toString()); console.log("🚀 => upscayl.stderr.on => stderr.toString()", data.toString());
data = data.toString(); data = data.toString();
mainWindow.webContents.send(commands_1.default.UPSCAYL_PROGRESS, data.toString()); mainWindow.webContents.send(commands_1.default.UPSCAYL_PROGRESS, data.toString());
@ -320,19 +313,21 @@ electron_1.ipcMain.on(commands_1.default.UPSCAYL, (event, payload) => __awaiter(
console.log("INCLUDES ALPHA CHANNEL, CHANGING OUTFILE NAME!"); console.log("INCLUDES ALPHA CHANNEL, CHANGING OUTFILE NAME!");
isAlpha = true; isAlpha = true;
} }
}); };
upscayl === null || upscayl === void 0 ? void 0 : upscayl.on("error", (data) => { const onError = (data) => {
mainWindow.webContents.send(commands_1.default.UPSCAYL_PROGRESS, data.toString()); mainWindow.webContents.send(commands_1.default.UPSCAYL_PROGRESS, data.toString());
failed = true; failed = true;
return; return;
}); };
// Send done comamnd when const onClose = () => {
upscayl === null || upscayl === void 0 ? void 0 : upscayl.on("close", (code) => {
if (failed !== true) { if (failed !== true) {
console.log("Done upscaling"); console.log("Done upscaling");
mainWindow.webContents.send(commands_1.default.UPSCAYL_DONE, isAlpha ? outFile + ".png" : outFile); mainWindow.webContents.send(commands_1.default.UPSCAYL_DONE, isAlpha ? outFile + ".png" : outFile);
} }
}); };
upscayl.process.stderr.on("data", onData);
upscayl.process.on("error", onError);
upscayl.process.on("close", onClose);
} }
})); }));
//------------------------Upscayl Folder-----------------------------// //------------------------Upscayl Folder-----------------------------//