1
0
mirror of https://github.com/upscayl/upscayl.git synced 2024-11-24 15:40:21 +01:00
upscayl/main/index.js
2022-08-17 08:07:50 +05:30

116 lines
2.6 KiB
JavaScript

// Native
const { join } = require("path");
const { format } = require("url");
const { spawn } = require("child_process");
const fs = require("fs");
const { execPath, modelsPath } = require("./binaries");
// Packages
const {
BrowserWindow,
app,
ipcMain,
dialog,
ipcRenderer,
} = require("electron");
const isDev = require("electron-is-dev");
const prepareNext = require("electron-next");
// Prepare the renderer once the app is ready
app.on("ready", async () => {
await prepareNext("./renderer");
const mainWindow = new BrowserWindow({
width: 1100,
height: 700,
webPreferences: {
autoHideMenuBar: true,
nodeIntegration: true,
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.maximize();
mainWindow.loadURL(url);
});
// Quit the app once all windows are closed
app.on("window-all-closed", app.quit);
// ! DONT FORGET TO RESTART THE APP WHEN YOU CHANGE CODE HERE
ipcMain.on("sendMessage", (_, message) => {
console.log(message);
});
ipcMain.on("open", async () => {
const { canceled, filePaths } = await dialog.showOpenDialog({
properties: ["openFile", "multiSelections"],
});
if (canceled) {
console.log("operation cancelled");
return "cancelled";
} else {
console.log(filePaths[0]);
// CREATE input AND upscaled FOLDER
let inputDir = "./input";
if (!fs.existsSync(inputDir)) {
fs.mkdirSync(inputDir);
}
let outputDir = "./upscaled";
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
}
// COPY IMAGE TO upscaled FOLDER
const fileName = filePaths[0].split("/").slice(-1)[0];
fs.copyFile(filePaths[0], inputDir + "/" + fileName, (err) => {
if (err) throw err;
});
// UPSCALE
let upscayl = spawn(
execPath,
[
"-i",
inputDir,
"-o",
outputDir,
"-s",
"4",
"-m",
modelsPath,
"-n",
"realesrgan-x4plus",
],
{
cwd: null,
detached: false,
}
);
upscayl.stdout.on("data", (stdout) => {
console.log("UPSCAYL: ", stdout.toString());
// TODO: SEND THE STDOUT TO RENDERER FROM HERE
});
upscayl.stderr.on("data", (stderr) => {
console.log(stderr.toString());
});
upscayl.on("close", (code) => {
console.log("Done upscaling", code);
});
return filePaths[0];
}
});