1
0
mirror of https://github.com/upscayl/upscayl.git synced 2025-01-19 01:24:09 +01:00

Add showNotification function and update output folder path

This commit is contained in:
Nayam Amarshe 2024-01-15 17:00:59 +05:30
parent a6f21c429a
commit 57810f4d77
6 changed files with 49 additions and 18 deletions

View File

@ -23,6 +23,7 @@ import { BatchUpscaylPayload } from "../../common/types/types";
import { ImageFormat } from "../utils/types"; import { ImageFormat } from "../utils/types";
import getModelScale from "../../common/check-model-scale"; import getModelScale from "../../common/check-model-scale";
import removeFileExtension from "../utils/remove-file-extension"; import removeFileExtension from "../utils/remove-file-extension";
import showNotification from "../utils/show-notification";
const batchUpscayl = async (event, payload: BatchUpscaylPayload) => { const batchUpscayl = async (event, payload: BatchUpscaylPayload) => {
const mainWindow = getMainWindow(); const mainWindow = getMainWindow();
@ -35,9 +36,9 @@ const batchUpscayl = async (event, payload: BatchUpscaylPayload) => {
// GET THE IMAGE DIRECTORY // GET THE IMAGE DIRECTORY
let inputDir = payload.batchFolderPath; let inputDir = payload.batchFolderPath;
// GET THE OUTPUT DIRECTORY // GET THE OUTPUT DIRECTORY
let outputDir = payload.outputPath; let outputFolderPath = payload.outputPath;
if (saveOutputFolder === true && outputFolderPath) { if (saveOutputFolder === true && outputFolderPath) {
outputDir = outputFolderPath; outputFolderPath = outputFolderPath;
} }
setNoImageProcessing(payload.noImageProcessing); setNoImageProcessing(payload.noImageProcessing);
@ -49,11 +50,12 @@ const batchUpscayl = async (event, payload: BatchUpscaylPayload) => {
const desiredScale = payload.scale as string; const desiredScale = payload.scale as string;
outputDir += const outputFolderName = `upscayl_${model}_x${
slash + noImageProcessing ? initialScale : desiredScale
`upscayl_${model}_x${noImageProcessing ? initialScale : desiredScale}`; }`;
if (!fs.existsSync(outputDir)) { outputFolderPath += slash + outputFolderName;
fs.mkdirSync(outputDir, { recursive: true }); if (!fs.existsSync(outputFolderPath)) {
fs.mkdirSync(outputFolderPath, { recursive: true });
} }
// Delete .DS_Store files // Delete .DS_Store files
@ -69,7 +71,7 @@ const batchUpscayl = async (event, payload: BatchUpscaylPayload) => {
"realesrgan", "realesrgan",
getBatchArguments( getBatchArguments(
inputDir, inputDir,
outputDir, outputFolderPath,
isDefaultModel ? modelsPath : customModelsFolderPath ?? modelsPath, isDefaultModel ? modelsPath : customModelsFolderPath ?? modelsPath,
model, model,
gpuId, gpuId,
@ -123,18 +125,22 @@ const batchUpscayl = async (event, payload: BatchUpscaylPayload) => {
if (noImageProcessing) { if (noImageProcessing) {
logit("🚫 Skipping scaling and converting"); logit("🚫 Skipping scaling and converting");
mainWindow.setProgressBar(-1); mainWindow.setProgressBar(-1);
mainWindow.webContents.send(COMMAND.FOLDER_UPSCAYL_DONE, outputDir); mainWindow.webContents.send(
COMMAND.FOLDER_UPSCAYL_DONE,
outputFolderPath
);
return; return;
} }
const files = fs.readdirSync(inputDir); const files = fs.readdirSync(inputDir);
try { try {
files.forEach(async (file) => { files.forEach(async (file) => {
if (file.startsWith(".") || file === outputFolderName) return;
console.log("Filename: ", removeFileExtension(file)); console.log("Filename: ", removeFileExtension(file));
await convertAndScale( await convertAndScale(
inputDir + slash + file, inputDir + slash + file,
`${outputDir}${slash}${removeFileExtension(file)}.png`, `${outputFolderPath}${slash}${removeFileExtension(file)}.png`,
`${outputDir}/${removeFileExtension(file)}.${saveImageAs}`, `${outputFolderPath}/${removeFileExtension(file)}.${saveImageAs}`,
desiredScale, desiredScale,
saveImageAs, saveImageAs,
onError onError
@ -143,11 +149,15 @@ const batchUpscayl = async (event, payload: BatchUpscaylPayload) => {
if (saveImageAs !== "png") { if (saveImageAs !== "png") {
logit("Removing output PNG"); logit("Removing output PNG");
fs.unlinkSync( fs.unlinkSync(
`${outputDir}${slash}${removeFileExtension(file)}.png` `${outputFolderPath}${slash}${removeFileExtension(file)}.png`
); );
} }
}); });
mainWindow.webContents.send(COMMAND.FOLDER_UPSCAYL_DONE, outputDir); mainWindow.webContents.send(
COMMAND.FOLDER_UPSCAYL_DONE,
outputFolderPath
);
showNotification("Upscayled", "Image upscayled successfully!");
} catch (error) { } catch (error) {
logit("❌ Error processing (scaling and converting) the image.", error); logit("❌ Error processing (scaling and converting) the image.", error);
upscayl.kill(); upscayl.kill();
@ -157,6 +167,7 @@ const batchUpscayl = async (event, payload: BatchUpscaylPayload) => {
"Error processing (scaling and converting) the image. Please report this error on Upscayl GitHub Issues page.\n" + "Error processing (scaling and converting) the image. Please report this error on Upscayl GitHub Issues page.\n" +
error error
); );
showNotification("Upscayl Failure", "Failed to upscale image!");
} }
} else { } else {
upscayl.kill(); upscayl.kill();

View File

@ -25,6 +25,7 @@ import convertAndScale from "../utils/convert-and-scale";
import { DoubleUpscaylPayload } from "../../common/types/types"; import { DoubleUpscaylPayload } from "../../common/types/types";
import { ImageFormat } from "../utils/types"; import { ImageFormat } from "../utils/types";
import getModelScale from "../../common/check-model-scale"; import getModelScale from "../../common/check-model-scale";
import showNotification from "../utils/show-notification";
const doubleUpscayl = async (event, payload: DoubleUpscaylPayload) => { const doubleUpscayl = async (event, payload: DoubleUpscaylPayload) => {
const mainWindow = getMainWindow(); const mainWindow = getMainWindow();
@ -120,6 +121,7 @@ const doubleUpscayl = async (event, payload: DoubleUpscaylPayload) => {
COMMAND.UPSCAYL_ERROR, COMMAND.UPSCAYL_ERROR,
"Error upscaling image. Error: " + data "Error upscaling image. Error: " + data
); );
showNotification("Upscayl Failure", "Failed to upscale image!");
upscayl.kill(); upscayl.kill();
return; return;
}; };
@ -170,6 +172,7 @@ const doubleUpscayl = async (event, payload: DoubleUpscaylPayload) => {
encodeURIComponent(outFile.match(/[^/\\]+$/i)![0]) encodeURIComponent(outFile.match(/[^/\\]+$/i)![0])
) )
); );
showNotification("Upscayled", "Image upscayled successfully!");
} catch (error) { } catch (error) {
logit("❌ Error reading original image metadata", error); logit("❌ Error reading original image metadata", error);
mainWindow && mainWindow &&
@ -178,6 +181,7 @@ const doubleUpscayl = async (event, payload: DoubleUpscaylPayload) => {
"Error processing (scaling and converting) the image. Please report this error on Upscayl GitHub Issues page.\n" + "Error processing (scaling and converting) the image. Please report this error on Upscayl GitHub Issues page.\n" +
error error
); );
showNotification("Upscayl Failure", "Failed to upscale image!");
upscayl.kill(); upscayl.kill();
} }
} }
@ -229,6 +233,7 @@ const doubleUpscayl = async (event, payload: DoubleUpscaylPayload) => {
COMMAND.UPSCAYL_ERROR, COMMAND.UPSCAYL_ERROR,
"Error upscaling image. Error: " + data "Error upscaling image. Error: " + data
); );
showNotification("Upscayl Failure", "Failed to upscale image!");
upscayl2.kill(); upscayl2.kill();
return; return;
}); });

View File

@ -26,6 +26,7 @@ import { ImageUpscaylPayload } from "../../common/types/types";
import { ImageFormat } from "../utils/types"; import { ImageFormat } from "../utils/types";
import getModelScale from "../../common/check-model-scale"; import getModelScale from "../../common/check-model-scale";
import removeFileExtension from "../utils/remove-file-extension"; import removeFileExtension from "../utils/remove-file-extension";
import showNotification from "../utils/show-notification";
const imageUpscayl = async (event, payload: ImageUpscaylPayload) => { const imageUpscayl = async (event, payload: ImageUpscaylPayload) => {
const mainWindow = getMainWindow(); const mainWindow = getMainWindow();
@ -191,6 +192,7 @@ const imageUpscayl = async (event, payload: ImageUpscaylPayload) => {
encodeURIComponent(outFile.match(/[^/\\]+$/i)![0]) encodeURIComponent(outFile.match(/[^/\\]+$/i)![0])
) )
); );
showNotification("Upscayl", "Image upscayled successfully!");
} catch (error) { } catch (error) {
logit( logit(
"❌ Error processing (scaling and converting) the image. Please report this error on GitHub.", "❌ Error processing (scaling and converting) the image. Please report this error on GitHub.",
@ -202,6 +204,7 @@ const imageUpscayl = async (event, payload: ImageUpscaylPayload) => {
"Error processing (scaling and converting) the image. Please report this error on Upscayl GitHub Issues page.\n" + "Error processing (scaling and converting) the image. Please report this error on Upscayl GitHub Issues page.\n" +
error error
); );
showNotification("Upscayl Failure", "Failed to upscale image!");
} }
} }
}; };

View File

@ -1,7 +1,6 @@
import fs from "fs"; import fs from "fs";
import sharp, { FormatEnum, Metadata } from "sharp"; import sharp, { FormatEnum, Metadata } from "sharp";
import logit from "./logit"; import logit from "./logit";
import { getMainWindow } from "../main-window";
import { compression } from "./config-variables"; import { compression } from "./config-variables";
import { ImageFormat } from "./types"; import { ImageFormat } from "./types";
@ -17,22 +16,24 @@ const convertAndScale = async (
logit("Skipping png compression for 4x scale"); logit("Skipping png compression for 4x scale");
return; return;
} }
const mainWindow = getMainWindow();
let originalImage: Metadata | undefined; let originalImage: Metadata | undefined;
try { try {
originalImage = await sharp(originalImagePath).metadata(); originalImage = await sharp(originalImagePath).metadata();
} catch (error) { } catch (error) {
logit("❌ Error with original Image: ", error); logit("❌ Error with original Image: ", error, " - ", originalImagePath);
} }
fs.access(originalImagePath, fs.constants.F_OK, (err) => { fs.access(originalImagePath, fs.constants.F_OK, (err) => {
logit("🖼️ Checking if original image exists: ", originalImagePath);
if (err) { if (err) {
throw new Error("Could not grab the original image!"); throw new Error(
"Could not grab the original image from the path provided! - " + err
);
} }
}); });
if (!mainWindow || !originalImage) { if (!originalImage) {
throw new Error("Could not grab the original image!"); throw new Error("Could not grab the original image!");
} }

View File

@ -1,3 +1,5 @@
import { Dirent } from "fs";
/** /**
* Returns the filename without the extension. * Returns the filename without the extension.
* @param filename The filename to remove the extension from. * @param filename The filename to remove the extension from.

View File

@ -0,0 +1,9 @@
import { Notification } from "electron/main";
export default function showNotification(title: string, body: string) {
new Notification({
title,
body,
closeButtonText: "Close",
}).show();
}