1
0
mirror of https://github.com/upscayl/upscayl.git synced 2024-09-24 03:18:28 +02: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

View File

@ -23,6 +23,7 @@ import {
import isDev from "electron-is-dev";
import prepareNext from "electron-next";
import commands from "./commands";
import { spawnUpscayl } from "./upscayl";
// Prepare the renderer once the app is ready
let mainWindow;
@ -314,92 +315,52 @@ ipcMain.on(commands.UPSCAYL, async (event, payload) => {
// If already upscayled, just output that file
mainWindow.webContents.send(commands.UPSCAYL_DONE, outFile);
} 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) {
default:
upscayl = spawn(
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
);
upscayl = spawnUpscayl(defaultArguments, "realesrgan");
break;
case "models-DF2K":
upscayl = spawn(
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
);
upscayl = spawnUpscayl(sharpenArguments, "realsr");
break;
}
let isAlpha = false;
let failed = false;
upscayl?.stderr.on("data", (data: string) => {
const onData = (data: string) => {
console.log(
"🚀 => upscayl.stderr.on => stderr.toString()",
data.toString()
@ -413,16 +374,15 @@ ipcMain.on(commands.UPSCAYL, async (event, payload) => {
console.log("INCLUDES ALPHA CHANNEL, CHANGING OUTFILE NAME!");
isAlpha = true;
}
});
};
upscayl?.on("error", (data) => {
const onError = (data) => {
mainWindow.webContents.send(commands.UPSCAYL_PROGRESS, data.toString());
failed = true;
return;
});
};
// Send done comamnd when
upscayl?.on("close", (code) => {
const onClose = () => {
if (failed !== true) {
console.log("Done upscaling");
mainWindow.webContents.send(
@ -430,7 +390,11 @@ ipcMain.on(commands.UPSCAYL, async (event, payload) => {
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
View 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(),
};
};

View File

@ -26,6 +26,7 @@ const electron_1 = require("electron");
const electron_is_dev_1 = __importDefault(require("electron-is-dev"));
const electron_next_1 = __importDefault(require("electron-next"));
const commands_1 = __importDefault(require("./commands"));
const upscayl_1 = require("./upscayl");
// Prepare the renderer once the app is ready
let mainWindow;
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);
}
else {
let upscayl = null;
let upscayl;
switch (model) {
default:
upscayl = (0, child_process_1.spawn)((0, binaries_1.execPath)("realesrgan"), [
upscayl = (0, upscayl_1.spawnUpscayl)([
"-i",
inputDir + "/" + fullfileName,
"-o",
@ -280,14 +281,10 @@ electron_1.ipcMain.on(commands_1.default.UPSCAYL, (event, payload) => __awaiter(
gpuId ? `-g ${gpuId}` : "",
"-f",
saveImageAs,
], {
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);
], "realesrgan");
break;
case "models-DF2K":
upscayl = (0, child_process_1.spawn)((0, binaries_1.execPath)("realsr"), [
upscayl = (0, upscayl_1.spawnUpscayl)([
"-i",
inputDir + "/" + fullfileName,
"-o",
@ -300,16 +297,12 @@ electron_1.ipcMain.on(commands_1.default.UPSCAYL, (event, payload) => __awaiter(
gpuId ? `-g ${gpuId}` : "",
"-f",
saveImageAs,
], {
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);
], "realsr");
break;
}
let isAlpha = 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());
data = 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!");
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());
failed = true;
return;
});
// Send done comamnd when
upscayl === null || upscayl === void 0 ? void 0 : upscayl.on("close", (code) => {
};
const onClose = () => {
if (failed !== true) {
console.log("Done upscaling");
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-----------------------------//