1
0
mirror of https://github.com/upscayl/upscayl.git synced 2025-01-18 17:14:08 +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 getModelScale from "../../common/check-model-scale";
import removeFileExtension from "../utils/remove-file-extension";
import showNotification from "../utils/show-notification";
const batchUpscayl = async (event, payload: BatchUpscaylPayload) => {
const mainWindow = getMainWindow();
@ -35,9 +36,9 @@ const batchUpscayl = async (event, payload: BatchUpscaylPayload) => {
// GET THE IMAGE DIRECTORY
let inputDir = payload.batchFolderPath;
// GET THE OUTPUT DIRECTORY
let outputDir = payload.outputPath;
let outputFolderPath = payload.outputPath;
if (saveOutputFolder === true && outputFolderPath) {
outputDir = outputFolderPath;
outputFolderPath = outputFolderPath;
}
setNoImageProcessing(payload.noImageProcessing);
@ -49,11 +50,12 @@ const batchUpscayl = async (event, payload: BatchUpscaylPayload) => {
const desiredScale = payload.scale as string;
outputDir +=
slash +
`upscayl_${model}_x${noImageProcessing ? initialScale : desiredScale}`;
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
const outputFolderName = `upscayl_${model}_x${
noImageProcessing ? initialScale : desiredScale
}`;
outputFolderPath += slash + outputFolderName;
if (!fs.existsSync(outputFolderPath)) {
fs.mkdirSync(outputFolderPath, { recursive: true });
}
// Delete .DS_Store files
@ -69,7 +71,7 @@ const batchUpscayl = async (event, payload: BatchUpscaylPayload) => {
"realesrgan",
getBatchArguments(
inputDir,
outputDir,
outputFolderPath,
isDefaultModel ? modelsPath : customModelsFolderPath ?? modelsPath,
model,
gpuId,
@ -123,18 +125,22 @@ const batchUpscayl = async (event, payload: BatchUpscaylPayload) => {
if (noImageProcessing) {
logit("🚫 Skipping scaling and converting");
mainWindow.setProgressBar(-1);
mainWindow.webContents.send(COMMAND.FOLDER_UPSCAYL_DONE, outputDir);
mainWindow.webContents.send(
COMMAND.FOLDER_UPSCAYL_DONE,
outputFolderPath
);
return;
}
const files = fs.readdirSync(inputDir);
try {
files.forEach(async (file) => {
if (file.startsWith(".") || file === outputFolderName) return;
console.log("Filename: ", removeFileExtension(file));
await convertAndScale(
inputDir + slash + file,
`${outputDir}${slash}${removeFileExtension(file)}.png`,
`${outputDir}/${removeFileExtension(file)}.${saveImageAs}`,
`${outputFolderPath}${slash}${removeFileExtension(file)}.png`,
`${outputFolderPath}/${removeFileExtension(file)}.${saveImageAs}`,
desiredScale,
saveImageAs,
onError
@ -143,11 +149,15 @@ const batchUpscayl = async (event, payload: BatchUpscaylPayload) => {
if (saveImageAs !== "png") {
logit("Removing output PNG");
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) {
logit("❌ Error processing (scaling and converting) the image.", error);
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
);
showNotification("Upscayl Failure", "Failed to upscale image!");
}
} else {
upscayl.kill();

View File

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

View File

@ -26,6 +26,7 @@ import { ImageUpscaylPayload } from "../../common/types/types";
import { ImageFormat } from "../utils/types";
import getModelScale from "../../common/check-model-scale";
import removeFileExtension from "../utils/remove-file-extension";
import showNotification from "../utils/show-notification";
const imageUpscayl = async (event, payload: ImageUpscaylPayload) => {
const mainWindow = getMainWindow();
@ -191,6 +192,7 @@ const imageUpscayl = async (event, payload: ImageUpscaylPayload) => {
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.",
@ -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
);
showNotification("Upscayl Failure", "Failed to upscale image!");
}
}
};

View File

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

View File

@ -1,3 +1,5 @@
import { Dirent } from "fs";
/**
* Returns the filename without the extension.
* @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();
}