From 2b1d45a2af2f0e48a6a7e9491314dc525ef12a27 Mon Sep 17 00:00:00 2001 From: TGS963 Date: Mon, 29 Aug 2022 19:11:59 +0530 Subject: [PATCH] rolled back some changes --- main/commands.js | 5 +- main/index.js | 138 +++++++++++++++------------------------ renderer/pages/index.jsx | 84 ++++++++---------------- 3 files changed, 84 insertions(+), 143 deletions(-) diff --git a/main/commands.js b/main/commands.js index f127a80..caa150f 100644 --- a/main/commands.js +++ b/main/commands.js @@ -1,8 +1,7 @@ const commands = { SELECT_FILE: "Select a File", - SELECT_OUTPUT: "Save as", - SET_FILE: "Set file", - REPLACE_ORIGINAL: "Replace original", + SELECT_FOLDER: "Select a Folder", + FILE_EXISTING: "File exists", UPSCAYL: "Upscale the Image", UPSCAYL_DONE: "Upscaling Done", UPSCAYL_PROGRESS: "Send Progress from Main to Renderer", diff --git a/main/index.js b/main/index.js index db11b8d..21bf718 100644 --- a/main/index.js +++ b/main/index.js @@ -4,7 +4,6 @@ const { format } = require("url"); const { spawn } = require("child_process"); const fs = require("fs"); const sizeOf = require("image-size"); -const path = require('path'); const { autoUpdater } = require("electron-updater"); const { getPlatform } = require("./getPlatform"); @@ -22,7 +21,6 @@ const { const isDev = require("electron-is-dev"); const prepareNext = require("electron-next"); const commands = require("./commands"); -const tmpPath = path.join(app.getPath("userData"), "\\tmp\\"); // Prepare the renderer once the app is ready let mainWindow; @@ -73,16 +71,6 @@ app.on("ready", async () => { // Quit the app once all windows are closed app.on("window-all-closed", app.quit); -// 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]); - }); -}); - // ! DONT FORGET TO RESTART THE APP WHEN YOU CHANGE CODE HERE ipcMain.handle(commands.SELECT_FILE, async () => { @@ -95,49 +83,21 @@ ipcMain.handle(commands.SELECT_FILE, async () => { return "cancelled"; } else { console.log(filePaths[0]); - // CREATE original copy - if(!fs.existsSync(tmpPath)) { - fs.mkdirSync(tmpPath); - } - fs.copyFileSync(filePaths[0], path.join(tmpPath, "original" + parse(filePaths[0]).ext)); + // CREATE input AND upscaled FOLDER return filePaths[0]; } }); -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)]}] +ipcMain.handle(commands.SELECT_FOLDER, async (event, message) => { + const { canceled, filePaths } = await dialog.showOpenDialog({ + properties: ["openDirectory"], }); if (canceled) { console.log("operation cancelled"); return "cancelled"; } else { - console.log(filePath); - if(fs.existsSync(tmpPath + "scaled" + fileExt)) { - fs.copyFileSync(tmpPath + "scaled" + fileExt, filePath); - } - return filePath; - } -}); - -ipcMain.handle(commands.REPLACE_ORIGINAL, async (event, payload) => { - const original = payload.original; - const fileExt = parse(original).ext; - if(fs.existsSync(tmpPath + "scaled" + fileExt)) { - fs.copyFileSync(tmpPath + "scaled" + fileExt, original); + console.log(filePaths[0]); + return filePaths[0]; } }); @@ -146,7 +106,7 @@ ipcMain.on(commands.UPSCAYL, async (event, payload) => { const scale = payload.scaleFactor; let inputDir = payload.imagePath.match(/(.*)[\/\\]/)[1] || ""; - let outputDir = tmpPath + let outputDir = payload.outputPath; console.log("🚀 => ipcMain.on => outputDir", outputDir); // COPY IMAGE TO TMP FOLDER @@ -157,48 +117,56 @@ ipcMain.on(commands.UPSCAYL, async (event, payload) => { : payload.imagePath.split("/").slice(-1)[0]; const fileName = parse(fullfileName).name; const fileExt = parse(fullfileName).ext; + const outFile = outputDir + "/" + fileName + "_upscayled_" + scale + "x_" + model + fileExt; // UPSCALE console.log("PRODUCTION? :", isDev); console.log("EXEC: ", execPath); - let upscayl = spawn( - execPath, - [ - "-i", - tmpPath + "original" + fileExt, - "-o", - tmpPath + "scaled" + fileExt, - "-s", - scale === 2 ? 4 : scale, - "-m", - modelsPath, - "-n", - model, - ], - { - cwd: null, - detached: false, - } - ); - let failed = false; - upscayl.stderr.on("data", (stderr) => { - console.log(stderr.toString()); - stderr = stderr.toString(); - if (stderr.includes("invalid gpu")) { - failed = true; - } - mainWindow.webContents.send(commands.UPSCAYL_PROGRESS, stderr.toString()); - }); - - upscayl.on("close", (code) => { - if (failed !== true) { - console.log("Done upscaling"); - mainWindow.webContents.send( - commands.UPSCAYL_DONE, - outputDir + "scaled" + fileExt - ); - } - }); + if (fs.existsSync(outFile)) { + mainWindow.webContents.send( + commands.UPSCAYL_DONE, + outFile + ); + } + else { + let upscayl = spawn( + execPath, + [ + "-i", + inputDir + "/" + fullfileName, + "-o", + outFile, + "-s", + scale === 2 ? 4 : scale, + "-m", + modelsPath, + "-n", + model, + ], + { + cwd: null, + detached: false, + } + ); + let failed = false; + upscayl.stderr.on("data", (stderr) => { + console.log(stderr.toString()); + stderr = stderr.toString(); + if (stderr.includes("invalid gpu")) { + failed = true; + } + mainWindow.webContents.send(commands.UPSCAYL_PROGRESS, stderr.toString()); + }); + upscayl.on("close", (code) => { + if (failed !== true) { + console.log("Done upscaling"); + mainWindow.webContents.send( + commands.UPSCAYL_DONE, + outFile + ); + } + }); + } }); autoUpdater.on("update-available", (_event, releaseNotes, releaseName) => { diff --git a/renderer/pages/index.jsx b/renderer/pages/index.jsx index af3183d..84cd0df 100644 --- a/renderer/pages/index.jsx +++ b/renderer/pages/index.jsx @@ -14,7 +14,6 @@ const Home = () => { const [outputPath, SetOutputPath] = useState(""); const [scaleFactor, setScaleFactor] = useState(4); const [progress, setProgress] = useState(""); - const [curStep, setStep] = useState(1); const [model, setModel] = useState("realesrgan-x4plus"); const [loaded, setLoaded] = useState(false); const [version, setVersion] = useState(""); @@ -23,13 +22,8 @@ const Home = () => { setProgress(""); SetImagePath(""); setUpscaledImagePath(""); - setStep(1); }; - const stepStyle = (thisStep) => { - return { display: (thisStep > curStep ? "none" : "block") }; - } - useEffect(() => { setVersion(navigator.userAgent.match(/Upscayl\/([\d\.]+\d+)/)[1]); }, []); @@ -54,7 +48,6 @@ const Home = () => { window.electron.on(commands.UPSCAYL_DONE, (_, data) => { setProgress(""); setUpscaledImagePath(data); - setStep(4); }); }, []); @@ -66,7 +59,6 @@ const Home = () => { SetImagePath(path); var dirname = path.match(/(.*)[\/\\]/)[1] || ""; SetOutputPath(dirname); - setStep(3); } }; @@ -109,7 +101,6 @@ const Home = () => { SetImagePath(filePath); var dirname = filePath.match(/(.*)[\/\\]/)[1] || ""; SetOutputPath(dirname); - setStep(3); window.electron.invoke(commands.SET_FILE, {original: filePath}); } }; @@ -131,21 +122,11 @@ const Home = () => { SetImagePath(filePath); var dirname = filePath.match(/(.*)[\/\\]/)[1] || ""; SetOutputPath(dirname); - setStep(3); } }; const outputHandler = async () => { - var path = await window.electron.invoke(commands.SELECT_OUTPUT, { original: imagePath }); - if (path !== "cancelled") { - SetOutputPath(path); - } else { - console.log("Getting output path from input file"); - } - }; - - const replaceHandler = async () => { - var path = await window.electron.invoke(commands.REPLACE_ORIGINAL, { original: imagePath }); + var path = await window.electron.invoke(commands.SELECT_FOLDER); if (path !== "cancelled") { SetOutputPath(path); } else { @@ -192,14 +173,14 @@ const Home = () => {

Step 1

{/* STEP 2 */} -
+

Step 2

Select Upscaling Type @@ -207,26 +188,14 @@ const Home = () => {

- {/* STEP 3 */} -
-

Step 3

- -
- {/* STEP 3

Step 3

@@ -259,27 +228,32 @@ const Home = () => {
*/} - {/* STEP 4 */} -
-

Step 4

+ {/* STEP 3 */} +
+

Step 3

- Save file + Defaults to Image's path

- +
+ + {/* STEP 4 */} +
+

Step 4

+
-

Copyright © 2022 -{" "} @@ -321,8 +295,8 @@ const Home = () => { onDragLeave={(e) => handleDragLeave(e)} onPaste={(e) => handlePaste(e)} > - {progress.length > 0 && ( -

+ {progress.length > 0 && upscaledImagePath.length === 0 && ( +

{progress}

@@ -341,7 +315,7 @@ const Home = () => { { { } itemTwo={