2022-08-15 06:53:14 +02:00
|
|
|
// Native
|
2022-08-17 11:18:49 +02:00
|
|
|
const { join, parse } = require("path");
|
2022-08-15 06:53:14 +02:00
|
|
|
const { format } = require("url");
|
2022-08-16 04:17:27 +02:00
|
|
|
const { spawn } = require("child_process");
|
2022-08-15 06:53:14 +02:00
|
|
|
const fs = require("fs");
|
|
|
|
|
2022-08-16 04:17:27 +02:00
|
|
|
const { execPath, modelsPath } = require("./binaries");
|
|
|
|
|
2022-08-15 06:53:14 +02:00
|
|
|
// Packages
|
2022-08-17 04:37:50 +02:00
|
|
|
const {
|
|
|
|
BrowserWindow,
|
|
|
|
app,
|
|
|
|
ipcMain,
|
|
|
|
dialog,
|
|
|
|
ipcRenderer,
|
|
|
|
} = require("electron");
|
2022-08-15 06:53:14 +02:00
|
|
|
const isDev = require("electron-is-dev");
|
|
|
|
const prepareNext = require("electron-next");
|
|
|
|
|
|
|
|
// Prepare the renderer once the app is ready
|
2022-08-17 13:36:19 +02:00
|
|
|
let mainWindow;
|
2022-08-15 06:53:14 +02:00
|
|
|
app.on("ready", async () => {
|
|
|
|
await prepareNext("./renderer");
|
|
|
|
|
2022-08-17 13:36:19 +02:00
|
|
|
mainWindow = new BrowserWindow({
|
2022-08-15 09:06:31 +02:00
|
|
|
width: 1100,
|
|
|
|
height: 700,
|
2022-08-15 06:53:14 +02:00
|
|
|
webPreferences: {
|
|
|
|
autoHideMenuBar: true,
|
2022-08-16 04:17:27 +02:00
|
|
|
nodeIntegration: true,
|
2022-08-15 06:53:14 +02:00
|
|
|
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);
|
2022-08-17 04:37:50 +02:00
|
|
|
// mainWindow.maximize();
|
2022-08-15 06:53:14 +02:00
|
|
|
mainWindow.loadURL(url);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Quit the app once all windows are closed
|
|
|
|
app.on("window-all-closed", app.quit);
|
|
|
|
|
2022-08-15 09:21:12 +02:00
|
|
|
// ! DONT FORGET TO RESTART THE APP WHEN YOU CHANGE CODE HERE
|
2022-08-15 09:06:31 +02:00
|
|
|
ipcMain.on("sendMessage", (_, message) => {
|
2022-08-15 06:53:14 +02:00
|
|
|
console.log(message);
|
|
|
|
});
|
2022-08-15 12:12:48 +02:00
|
|
|
|
2022-08-17 11:18:49 +02:00
|
|
|
ipcMain.handle("open", async () => {
|
2022-08-16 04:17:27 +02:00
|
|
|
const { canceled, filePaths } = await dialog.showOpenDialog({
|
|
|
|
properties: ["openFile", "multiSelections"],
|
|
|
|
});
|
|
|
|
|
2022-08-15 12:12:48 +02:00
|
|
|
if (canceled) {
|
2022-08-16 04:17:27 +02:00
|
|
|
console.log("operation cancelled");
|
|
|
|
return "cancelled";
|
|
|
|
} else {
|
|
|
|
console.log(filePaths[0]);
|
2022-08-17 11:18:49 +02:00
|
|
|
// CREATE input AND upscaled FOLDER
|
|
|
|
return filePaths[0];
|
|
|
|
}
|
|
|
|
})
|
|
|
|
ipcMain.handle("output", async (event, message) => {
|
|
|
|
const { canceled, filePaths } = await dialog.showOpenDialog({
|
|
|
|
properties: ["openDirectory"],
|
|
|
|
});
|
|
|
|
if (canceled) {
|
|
|
|
console.log("operation cancelled");
|
|
|
|
return "cancelled";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.log(filePaths[0])
|
|
|
|
return filePaths[0];
|
|
|
|
}
|
|
|
|
})
|
2022-08-16 04:17:27 +02:00
|
|
|
|
2022-08-17 11:18:49 +02:00
|
|
|
ipcMain.on("upscayl", async (event, paths) => {
|
|
|
|
const scale = "4";
|
|
|
|
let inputDir = paths[0].match(/(.*)[\/\\]/)[1]||'';
|
|
|
|
/*if (!fs.existsSync(inputDir)) {
|
|
|
|
fs.mkdirSync(inputDir);
|
|
|
|
}*/
|
|
|
|
let outputDir = paths[1];
|
|
|
|
/*if (!fs.existsSync(outputDir)) {
|
|
|
|
fs.mkdirSync(outputDir);
|
|
|
|
}*/
|
2022-08-16 04:17:27 +02:00
|
|
|
|
2022-08-17 11:18:49 +02:00
|
|
|
// COPY IMAGE TO upscaled FOLDER
|
|
|
|
const fullfileName = paths[0].split("/").slice(-1)[0];
|
|
|
|
const fileName = parse(fullfileName).name;
|
|
|
|
const fileExt = parse(fullfileName).ext;
|
2022-08-17 13:36:19 +02:00
|
|
|
|
2022-08-17 11:18:49 +02:00
|
|
|
// UPSCALE
|
|
|
|
let upscayl = spawn(
|
|
|
|
execPath,
|
|
|
|
[
|
|
|
|
"-i",
|
|
|
|
inputDir+'/'+fullfileName,
|
|
|
|
"-o",
|
|
|
|
outputDir+'/'+fileName+"_upscaled_"+scale+'x'+fileExt,
|
|
|
|
"-s",
|
|
|
|
scale,
|
|
|
|
"-m",
|
|
|
|
modelsPath,
|
|
|
|
"-n",
|
|
|
|
"realesrgan-x4plus",
|
|
|
|
],
|
|
|
|
{
|
|
|
|
cwd: null,
|
|
|
|
detached: false,
|
|
|
|
}
|
|
|
|
);
|
2022-08-17 04:37:50 +02:00
|
|
|
|
2022-08-17 11:18:49 +02:00
|
|
|
upscayl.stderr.on("data", (stderr) => {
|
|
|
|
console.log(stderr.toString());
|
|
|
|
stderr = stderr.toString();
|
|
|
|
mainWindow.webContents.send("output", stderr.toString());
|
|
|
|
});
|
2022-08-16 04:17:27 +02:00
|
|
|
|
2022-08-17 11:18:49 +02:00
|
|
|
upscayl.on("close", (code) => {
|
|
|
|
console.log("Done upscaling");
|
|
|
|
mainWindow.webContents.send("done");
|
|
|
|
});
|
|
|
|
})
|