π Upscayl - #1 Free and Open Source AI Image Upscaler for Linux, MacOS and Windows.
TypeScript aielectronesrgangigapixelgigapixel-imagesimageimage-upscalingtopazupscaleupscalerimageupscayl
Add error resume for batch upscayl, and refactor code
Nayam Amarshe5cc9bd0df5be
diff --git a/electron/commands/batch-upscayl.ts b/electron/commands/batch-upscayl.tsindex c83c056..4d65342 100644--- a/electron/commands/batch-upscayl.ts+++ b/electron/commands/batch-upscayl.ts@@ -87,6 +87,7 @@ const batchUpscayl = async (event, payload: BatchUpscaylPayload) => {setStopped(false);let failed = false;+ let encounteredError = false;const onData = (data: any) => {if (!mainWindow) return;@@ -97,8 +98,7 @@ const batchUpscayl = async (event, payload: BatchUpscaylPayload) => {);if ((data as string).includes("Error")) {logit("β ", data);- failed = true;- upscayl.kill();+ encounteredError = true;} else if (data.includes("Resizing")) {mainWindow.webContents.send(COMMAND.SCALING_AND_CONVERTING);}@@ -115,7 +115,7 @@ const batchUpscayl = async (event, payload: BatchUpscaylPayload) => {mainWindow &&mainWindow.webContents.send(COMMAND.UPSCAYL_ERROR,- `Error upscaling image! ${data}`,+ `Error upscaling images! ${data}`,);return;};@@ -123,25 +123,18 @@ const batchUpscayl = async (event, payload: BatchUpscaylPayload) => {if (!mainWindow) return;if (!failed && !stopped) {logit("π― Done upscaling");- logit("β» Scaling and converting now...");upscayl.kill();- mainWindow && mainWindow.webContents.send(COMMAND.SCALING_AND_CONVERTING);- try {- mainWindow.webContents.send(- COMMAND.FOLDER_UPSCAYL_DONE,- outputFolderPath,+ mainWindow.webContents.send(+ COMMAND.FOLDER_UPSCAYL_DONE,+ outputFolderPath,+ );+ if (!encounteredError) {+ showNotification("Upscayled", "Images upscayled successfully!");+ } else {+ showNotification(+ "Upscayled",+ "Images were upscayled but encountered some errors!",);- showNotification("Upscayled", "Image upscayled successfully!");- } catch (error) {- logit("β Error processing (scaling and converting) the image.", error);- upscayl.kill();- mainWindow &&- mainWindow.webContents.send(- COMMAND.UPSCAYL_ERROR,- "Error processing (scaling and converting) the image. Please report this error on Upscayl GitHub Issues page.\n" +- error,- );- showNotification("Upscayl Failure", "Failed to upscale image!");}} else {upscayl.kill();diff --git a/electron/commands/double-upscayl.ts b/electron/commands/double-upscayl.tsindex db16b43..af1d4ae 100644--- a/electron/commands/double-upscayl.ts+++ b/electron/commands/double-upscayl.ts@@ -79,30 +79,63 @@ const doubleUpscayl = async (event, payload: DoubleUpscaylPayload) => {logit,);+ let upscayl2: ReturnType<typeof spawnUpscayl>;+childProcesses.push(upscayl);setStopped(false);let failed = false;let failed2 = false;- const onData = (data) => {+ // SECOND PASS FUNCTIONS+ const onError2 = (data) => {+ if (!mainWindow) return;+ data.toString();+ // SEND UPSCAYL PROGRESS TO RENDERER+ mainWindow.webContents.send(COMMAND.DOUBLE_UPSCAYL_PROGRESS, data);+ // SET FAILED TO TRUE+ failed2 = true;+ mainWindow &&+ mainWindow.webContents.send(+ COMMAND.UPSCAYL_ERROR,+ "Error upscaling image. Error: " + data,+ );+ showNotification("Upscayl Failure", "Failed to upscale image!");+ upscayl2.kill();+ return;+ };++ const onData2 = (data) => {if (!mainWindow) return;// CONVERT DATA TO STRINGdata = data.toString();// SEND UPSCAYL PROGRESS TO RENDERERmainWindow.webContents.send(COMMAND.DOUBLE_UPSCAYL_PROGRESS, data);// IF PROGRESS HAS ERROR, UPSCAYL FAILED- if (data.includes("Error") || data.includes("failed")) {- upscayl.kill();- failed = true;- } else if (data.includes("Resizing")) {- mainWindow.webContents.send(COMMAND.SCALING_AND_CONVERTING);+ if (data.includes("invalid gpu") || data.includes("failed")) {+ upscayl2.kill();+ failed2 = true;+ }+ };++ const onClose2 = async (code) => {+ if (!mainWindow) return;+ if (!failed2 && !stopped) {+ logit("π― Done upscaling");++ mainWindow.setProgressBar(-1);+ mainWindow.webContents.send(+ COMMAND.DOUBLE_UPSCAYL_DONE,+ outFile.replace(+ /([^/\\]+)$/i,+ encodeURIComponent(outFile.match(/[^/\\]+$/i)![0]),+ ),+ );+ showNotification("Upscayled", "Image upscayled successfully!");}- // if (data.includes("alpha channel")) {- // isAlpha = true;- // }};+ // FIRST PASS FUNCTIONSconst onError = (data) => {if (!mainWindow) return;mainWindow.setProgressBar(-1);@@ -121,42 +154,26 @@ const doubleUpscayl = async (event, payload: DoubleUpscaylPayload) => {return;};- const onClose2 = async (code) => {+ const onData = (data) => {if (!mainWindow) return;- if (!failed2 && !stopped) {- logit("π― Done upscaling");-- try {- mainWindow.setProgressBar(-1);- mainWindow.webContents.send(- COMMAND.DOUBLE_UPSCAYL_DONE,- outFile.replace(- /([^/\\]+)$/i,- encodeURIComponent(outFile.match(/[^/\\]+$/i)![0]),- ),- );- showNotification("Upscayled", "Image upscayled successfully!");- } catch (error) {- logit("β Error reading original image metadata", error);- mainWindow &&- mainWindow.webContents.send(- COMMAND.UPSCAYL_ERROR,- "Error processing (scaling and converting) the image. Please report this error on Upscayl GitHub Issues page.\n" +- error,- );- showNotification("Upscayl Failure", "Failed to upscale image!");- upscayl.kill();- }+ // CONVERT DATA TO STRING+ data = data.toString();+ // SEND UPSCAYL PROGRESS TO RENDERER+ mainWindow.webContents.send(COMMAND.DOUBLE_UPSCAYL_PROGRESS, data);+ // IF PROGRESS HAS ERROR, UPSCAYL FAILED+ if (data.includes("Error") || data.includes("failed")) {+ upscayl.kill();+ failed = true;+ } else if (data.includes("Resizing")) {+ mainWindow.webContents.send(COMMAND.SCALING_AND_CONVERTING);}};- upscayl.process.stderr.on("data", onData);- upscayl.process.on("error", onError);- upscayl.process.on("close", (code) => {+ const onClose = (code) => {// IF NOT FAILEDif (!failed && !stopped) {- // UPSCALE- let upscayl2 = spawnUpscayl(+ // SPAWN A SECOND PASS+ upscayl2 = spawnUpscayl(getDoubleUpscaleSecondPassArguments({outFile,modelsPath: isDefaultModel@@ -170,40 +187,16 @@ const doubleUpscayl = async (event, payload: DoubleUpscaylPayload) => {}),logit,);-childProcesses.push(upscayl2);-- upscayl2.process.stderr.on("data", (data) => {- if (!mainWindow) return;- // CONVERT DATA TO STRING- data = data.toString();- // SEND UPSCAYL PROGRESS TO RENDERER- mainWindow.webContents.send(COMMAND.DOUBLE_UPSCAYL_PROGRESS, data);- // IF PROGRESS HAS ERROR, UPSCAYL FAILED- if (data.includes("invalid gpu") || data.includes("failed")) {- upscayl2.kill();- failed2 = true;- }- });- upscayl2.process.on("error", (data) => {- if (!mainWindow) return;- data.toString();- // SEND UPSCAYL PROGRESS TO RENDERER- mainWindow.webContents.send(COMMAND.DOUBLE_UPSCAYL_PROGRESS, data);- // SET FAILED TO TRUE- failed2 = true;- mainWindow &&- mainWindow.webContents.send(- COMMAND.UPSCAYL_ERROR,- "Error upscaling image. Error: " + data,- );- showNotification("Upscayl Failure", "Failed to upscale image!");- upscayl2.kill();- return;- });+ upscayl2.process.stderr.on("data", onData2);+ upscayl2.process.on("error", onError2);upscayl2.process.on("close", onClose2);}- });+ };++ upscayl.process.stderr.on("data", onData);+ upscayl.process.on("error", onError);+ upscayl.process.on("close", onClose);};export default doubleUpscayl;diff --git a/electron/commands/image-upscayl.ts b/electron/commands/image-upscayl.tsindex 04e41f1..de3e0f4 100644--- a/electron/commands/image-upscayl.ts+++ b/electron/commands/image-upscayl.ts@@ -122,17 +122,13 @@ const imageUpscayl = async (event, payload: ImageUpscaylPayload) => {let failed = false;const onData = (data: string) => {- logit("image upscayl: ", data.toString());+ logit(data.toString());mainWindow.setProgressBar(parseFloat(data.slice(0, data.length)) / 100);data = data.toString();mainWindow.webContents.send(COMMAND.UPSCAYL_PROGRESS, data.toString());if (data.includes("invalid gpu") || data.includes("failed")) {- logit("β INVALID GPU OR FAILED");upscayl.kill();failed = true;- } else if (data.includes("has alpha channel")) {- logit("π’ INCLUDES ALPHA CHANNEL, CHANGING OUTFILE NAME!");- isAlpha = true;} else if (data.includes("Resizing")) {mainWindow.webContents.send(COMMAND.SCALING_AND_CONVERTING);}@@ -140,7 +136,7 @@ const imageUpscayl = async (event, payload: ImageUpscaylPayload) => {const onError = (data) => {if (!mainWindow) return;mainWindow.setProgressBar(-1);- mainWindow.webContents.send(COMMAND.UPSCAYL_PROGRESS, data.toString());+ mainWindow.webContents.send(COMMAND.UPSCAYL_ERROR, data.toString());failed = true;upscayl.kill();return;@@ -148,32 +144,17 @@ const imageUpscayl = async (event, payload: ImageUpscaylPayload) => {const onClose = async () => {if (!failed && !stopped) {logit("π― Done upscaling");- logit("β» Scaling and converting now...");// Free up memoryupscayl.kill();- try {- mainWindow.setProgressBar(-1);- mainWindow.webContents.send(- COMMAND.UPSCAYL_DONE,- outFile.replace(- /([^/\\]+)$/i,- encodeURIComponent(outFile.match(/[^/\\]+$/i)![0]),- ),- );- showNotification("Upscayl", "Image upscayled successfully!");- } catch (error) {- logit(- "β Error processing (scaling and converting) the image. Please report this error on GitHub.",- error,- );- upscayl.kill();- mainWindow.webContents.send(- COMMAND.UPSCAYL_ERROR,- "Error processing (scaling and converting) the image. Please report this error on Upscayl GitHub Issues page.\n" +- error,- );- showNotification("Upscayl Failure", "Failed to upscale image!");- }+ mainWindow.setProgressBar(-1);+ mainWindow.webContents.send(+ COMMAND.UPSCAYL_DONE,+ outFile.replace(+ /([^/\\]+)$/i,+ encodeURIComponent(outFile.match(/[^/\\]+$/i)![0]),+ ),+ );+ showNotification("Upscayl", "Image upscayled successfully!");}};