2022-08-15 06:53:14 +02:00
|
|
|
// Native
|
2022-08-23 16:17:16 +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-18 11:53:23 +02:00
|
|
|
const sizeOf = require("image-size");
|
2022-08-27 23:25:32 +02:00
|
|
|
const path = require('path');
|
2022-08-21 17:11:11 +02:00
|
|
|
const { autoUpdater } = require("electron-updater");
|
2022-08-26 03:15:43 +02:00
|
|
|
const { getPlatform } = require("./getPlatform");
|
2022-08-15 06:53:14 +02:00
|
|
|
|
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,
|
2022-08-18 11:53:23 +02:00
|
|
|
shell,
|
2022-08-17 04:37:50 +02:00
|
|
|
} = require("electron");
|
2022-08-15 06:53:14 +02:00
|
|
|
const isDev = require("electron-is-dev");
|
|
|
|
const prepareNext = require("electron-next");
|
2022-08-22 10:31:16 +02:00
|
|
|
const commands = require("./commands");
|
2022-08-27 23:25:32 +02:00
|
|
|
const tmpPath = path.join(app.getPath("userData"), "\\tmp\\");
|
2022-08-15 06:53:14 +02:00
|
|
|
|
|
|
|
// 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-24 04:31:43 +02:00
|
|
|
console.log("ICON: ", join(__dirname, "icon.png"));
|
2022-08-17 13:36:19 +02:00
|
|
|
mainWindow = new BrowserWindow({
|
2022-08-24 04:31:43 +02:00
|
|
|
icon: join(__dirname, "build", "icon.png"),
|
2022-08-15 09:06:31 +02:00
|
|
|
width: 1100,
|
2022-08-23 15:17:34 +02:00
|
|
|
height: 700,
|
2022-08-18 11:53:23 +02:00
|
|
|
minHeight: 500,
|
|
|
|
minWidth: 500,
|
2022-08-28 02:12:17 +02:00
|
|
|
show: false,
|
2022-08-28 01:49:19 +02:00
|
|
|
backgroundColor: "#171717",
|
2022-08-15 06:53:14 +02:00
|
|
|
webPreferences: {
|
2022-08-23 14:19:29 +02:00
|
|
|
devTools: isDev,
|
2022-08-15 06:53:14 +02:00
|
|
|
autoHideMenuBar: true,
|
2022-08-16 04:17:27 +02:00
|
|
|
nodeIntegration: true,
|
2022-08-18 11:53:23 +02:00
|
|
|
webSecurity: false,
|
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);
|
2022-08-18 11:53:23 +02:00
|
|
|
|
|
|
|
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
|
|
|
|
shell.openExternal(url);
|
|
|
|
return { action: "deny" };
|
|
|
|
});
|
2022-08-21 17:11:11 +02:00
|
|
|
|
2022-08-28 02:12:17 +02:00
|
|
|
mainWindow.once("ready-to-show", () => { mainWindow.show(); })
|
|
|
|
|
2022-08-21 17:11:11 +02:00
|
|
|
if (!isDev) {
|
|
|
|
autoUpdater.checkForUpdates();
|
2022-08-23 05:13:08 +02:00
|
|
|
}
|
2022-08-15 06:53:14 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// Quit the app once all windows are closed
|
|
|
|
app.on("window-all-closed", app.quit);
|
|
|
|
|
2022-08-28 01:50:20 +02:00
|
|
|
// Fix file:// + ? by registering a new protocol
|
|
|
|
app.whenReady().then(() => {
|
|
|
|
const { protocol } = require("electron");
|
|
|
|
protocol.registerFileProtocol('local', (request, callback) => {
|
|
|
|
const pathname = decodeURIComponent(request.url.replace('local://', ''));
|
|
|
|
const parts = pathname.split('?');
|
|
|
|
callback(parts[0]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-08-15 09:21:12 +02:00
|
|
|
// ! DONT FORGET TO RESTART THE APP WHEN YOU CHANGE CODE HERE
|
2022-08-15 12:12:48 +02:00
|
|
|
|
2022-08-18 11:53:23 +02:00
|
|
|
ipcMain.handle(commands.SELECT_FILE, 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-27 23:25:32 +02:00
|
|
|
// CREATE original copy
|
|
|
|
if(!fs.existsSync(tmpPath)) {
|
|
|
|
fs.mkdirSync(tmpPath);
|
|
|
|
}
|
|
|
|
fs.copyFileSync(filePaths[0], path.join(tmpPath, "original" + parse(filePaths[0]).ext));
|
2022-08-17 11:18:49 +02:00
|
|
|
return filePaths[0];
|
|
|
|
}
|
2022-08-18 11:53:23 +02:00
|
|
|
});
|
|
|
|
|
2022-08-27 23:25:32 +02:00
|
|
|
ipcMain.handle(commands.SET_FILE, async (event, payload) => {
|
|
|
|
const original = payload.original;
|
|
|
|
const fileExt = parse(original).ext;
|
|
|
|
// CREATE original copy
|
|
|
|
if(!fs.existsSync(tmpPath)) {
|
|
|
|
fs.mkdirSync(tmpPath);
|
|
|
|
}
|
|
|
|
fs.copyFileSync(original, path.join(tmpPath, "original" + fileExt));
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
ipcMain.handle(commands.SELECT_OUTPUT, async (event, payload) => {
|
|
|
|
const original = payload.original;
|
|
|
|
const fileExt = parse(original).ext;
|
|
|
|
const { canceled, filePath } = await dialog.showSaveDialog({
|
|
|
|
filters: [{name: fileExt, extensions: [fileExt.substring(1)]}]
|
2022-08-17 11:18:49 +02:00
|
|
|
});
|
|
|
|
if (canceled) {
|
|
|
|
console.log("operation cancelled");
|
|
|
|
return "cancelled";
|
2022-08-18 11:53:23 +02:00
|
|
|
} else {
|
2022-08-27 23:25:32 +02:00
|
|
|
console.log(filePath);
|
2022-08-28 01:50:20 +02:00
|
|
|
if(fs.existsSync(tmpPath + "scaled" + fileExt)) {
|
|
|
|
fs.copyFileSync(tmpPath + "scaled" + fileExt, filePath);
|
2022-08-27 23:25:32 +02:00
|
|
|
}
|
|
|
|
return filePath;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
ipcMain.handle(commands.REPLACE_ORIGINAL, async (event, payload) => {
|
|
|
|
const original = payload.original;
|
|
|
|
const fileExt = parse(original).ext;
|
2022-08-28 01:50:20 +02:00
|
|
|
if(fs.existsSync(tmpPath + "scaled" + fileExt)) {
|
|
|
|
fs.copyFileSync(tmpPath + "scaled" + fileExt, original);
|
2022-08-17 11:18:49 +02:00
|
|
|
}
|
2022-08-18 11:53:23 +02:00
|
|
|
});
|
2022-08-16 04:17:27 +02:00
|
|
|
|
2022-08-18 11:53:23 +02:00
|
|
|
ipcMain.on(commands.UPSCAYL, async (event, payload) => {
|
|
|
|
const model = payload.model;
|
|
|
|
const scale = payload.scaleFactor;
|
2022-08-23 15:17:34 +02:00
|
|
|
|
2022-08-18 11:53:23 +02:00
|
|
|
let inputDir = payload.imagePath.match(/(.*)[\/\\]/)[1] || "";
|
2022-08-27 23:25:32 +02:00
|
|
|
let outputDir = tmpPath
|
2022-08-26 15:26:40 +02:00
|
|
|
console.log("🚀 => ipcMain.on => outputDir", outputDir);
|
2022-08-18 11:53:23 +02:00
|
|
|
|
2022-08-27 23:25:32 +02:00
|
|
|
// COPY IMAGE TO TMP FOLDER
|
2022-08-26 15:26:40 +02:00
|
|
|
const platform = getPlatform();
|
|
|
|
const fullfileName =
|
|
|
|
platform === "win"
|
|
|
|
? payload.imagePath.split("\\").slice(-1)[0]
|
|
|
|
: payload.imagePath.split("/").slice(-1)[0];
|
2022-08-17 11:18:49 +02:00
|
|
|
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
|
2022-08-23 16:17:16 +02:00
|
|
|
console.log("PRODUCTION? :", isDev);
|
2022-08-23 16:07:40 +02:00
|
|
|
console.log("EXEC: ", execPath);
|
2022-08-17 11:18:49 +02:00
|
|
|
let upscayl = spawn(
|
|
|
|
execPath,
|
|
|
|
[
|
|
|
|
"-i",
|
2022-08-28 01:50:20 +02:00
|
|
|
tmpPath + "original" + fileExt,
|
2022-08-17 11:18:49 +02:00
|
|
|
"-o",
|
2022-08-28 01:50:20 +02:00
|
|
|
tmpPath + "scaled" + fileExt,
|
2022-08-17 11:18:49 +02:00
|
|
|
"-s",
|
2022-08-18 11:53:23 +02:00
|
|
|
scale === 2 ? 4 : scale,
|
2022-08-17 11:18:49 +02:00
|
|
|
"-m",
|
2022-08-23 15:17:34 +02:00
|
|
|
modelsPath,
|
2022-08-17 11:18:49 +02:00
|
|
|
"-n",
|
2022-08-18 11:53:23 +02:00
|
|
|
model,
|
2022-08-17 11:18:49 +02:00
|
|
|
],
|
|
|
|
{
|
|
|
|
cwd: null,
|
|
|
|
detached: false,
|
|
|
|
}
|
|
|
|
);
|
2022-08-25 14:35:38 +02:00
|
|
|
let failed = false;
|
2022-08-17 11:18:49 +02:00
|
|
|
upscayl.stderr.on("data", (stderr) => {
|
|
|
|
console.log(stderr.toString());
|
|
|
|
stderr = stderr.toString();
|
2022-08-25 14:35:38 +02:00
|
|
|
if (stderr.includes("invalid gpu")) {
|
|
|
|
failed = true;
|
|
|
|
}
|
2022-08-18 11:53:23 +02:00
|
|
|
mainWindow.webContents.send(commands.UPSCAYL_PROGRESS, stderr.toString());
|
2022-08-17 11:18:49 +02:00
|
|
|
});
|
2022-08-16 04:17:27 +02:00
|
|
|
|
2022-08-17 11:18:49 +02:00
|
|
|
upscayl.on("close", (code) => {
|
2022-08-25 14:35:38 +02:00
|
|
|
if (failed !== true) {
|
|
|
|
console.log("Done upscaling");
|
|
|
|
mainWindow.webContents.send(
|
|
|
|
commands.UPSCAYL_DONE,
|
2022-08-28 01:50:20 +02:00
|
|
|
outputDir + "scaled" + fileExt
|
2022-08-25 14:35:38 +02:00
|
|
|
);
|
|
|
|
}
|
2022-08-17 11:18:49 +02:00
|
|
|
});
|
2022-08-18 11:53:23 +02:00
|
|
|
});
|
2022-08-21 17:11:11 +02:00
|
|
|
|
|
|
|
autoUpdater.on("update-available", (_event, releaseNotes, releaseName) => {
|
|
|
|
const dialogOpts = {
|
2022-08-23 05:13:08 +02:00
|
|
|
type: "info",
|
|
|
|
buttons: ["Ok"],
|
|
|
|
title: "Application Update",
|
|
|
|
message: process.platform === "win32" ? releaseNotes : releaseName,
|
|
|
|
detail: "A new version is being downloaded.",
|
|
|
|
};
|
|
|
|
dialog.showMessageBox(dialogOpts, (response) => {});
|
|
|
|
});
|
2022-08-21 17:11:11 +02:00
|
|
|
|
|
|
|
autoUpdater.on("update-downloaded", (_event, releaseNotes, releaseName) => {
|
|
|
|
const dialogOpts = {
|
2022-08-23 05:13:08 +02:00
|
|
|
type: "info",
|
|
|
|
buttons: ["Restart", "Later"],
|
|
|
|
title: "Application Update",
|
|
|
|
message: process.platform === "win32" ? releaseNotes : releaseName,
|
|
|
|
detail:
|
|
|
|
"A new version has been downloaded. Restart the application to apply the updates.",
|
2022-08-21 17:11:11 +02:00
|
|
|
};
|
|
|
|
dialog.showMessageBox(dialogOpts).then((returnValue) => {
|
2022-08-23 05:13:08 +02:00
|
|
|
if (returnValue.response === 0) autoUpdater.quitAndInstall();
|
|
|
|
});
|
2022-08-21 17:11:11 +02:00
|
|
|
});
|