1
0
mirror of https://github.com/upscayl/upscayl.git synced 2024-11-13 18:30:54 +01:00
upscayl/main/index.js

102 lines
2.5 KiB
JavaScript
Raw Normal View History

2022-08-15 06:53:14 +02:00
// Native
const { join } = require("path");
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-15 12:12:48 +02:00
const { BrowserWindow, app, ipcMain, dialog } = 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
app.on("ready", async () => {
await prepareNext("./renderer");
const 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-15 09:06:31 +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
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]);
// 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];
console.log("🚀 => ipcMain.handle => fileName", fileName);
fs.copyFile(filePaths[0], inputDir + "/" + fileName, (err) => {
if (err) throw err;
console.log("File Copy Successfully.");
});
// UPSCALE
console.log(execPath);
console.log(modelsPath);
let command = spawn(execPath, [
"-i",
inputDir,
"-o",
outputDir,
"-s",
4,
"-m",
modelsPath,
]);
command.stdout.on("data", (data) => {
console.log("stdout: ", data.toString());
});
command.on("error", (error) => {
console.log(error);
});
command.on("exit", (code, signal) => {
console.log("Exit: ", code, signal);
});
return filePaths[0];
2022-08-15 12:12:48 +02:00
}
2022-08-16 04:17:27 +02:00
});