1
0
mirror of https://github.com/upscayl/upscayl.git synced 2024-12-19 19:05:56 +01:00
upscayl/electron/commands/batch-upscayl.ts

157 lines
4.6 KiB
TypeScript
Raw Normal View History

import fs from "fs";
import { getMainWindow } from "../main-window";
import {
childProcesses,
2024-04-09 20:11:24 +02:00
savedCustomModelsPath,
2023-09-19 17:04:31 +02:00
noImageProcessing,
2024-04-09 20:11:24 +02:00
rememberOutputFolder,
2023-11-23 06:39:46 +01:00
setCompression,
2023-11-22 16:54:02 +01:00
setNoImageProcessing,
setStopped,
stopped,
} from "../utils/config-variables";
import logit from "../utils/logit";
import { spawnUpscayl } from "../utils/spawn-upscayl";
import { getBatchArguments } from "../utils/get-arguments";
import slash from "../utils/slash";
import { modelsPath } from "../utils/get-resource-paths";
import COMMAND from "../../common/commands";
2023-11-22 16:54:02 +01:00
import { BatchUpscaylPayload } from "../../common/types/types";
import { ImageFormat } from "../utils/types";
import showNotification from "../utils/show-notification";
import { DEFAULT_MODELS } from "../../common/models-list";
2023-11-22 16:54:02 +01:00
const batchUpscayl = async (event, payload: BatchUpscaylPayload) => {
const mainWindow = getMainWindow();
if (!mainWindow) return;
// GET THE MODEL
const model = payload.model;
const gpuId = payload.gpuId;
const saveImageAs = payload.saveImageAs as ImageFormat;
2024-01-16 10:22:31 +01:00
console.log("PAYLOAD: ", payload);
// GET THE IMAGE DIRECTORY
let inputDir = payload.batchFolderPath;
// GET THE OUTPUT DIRECTORY
let outputFolderPath = payload.outputPath;
2024-04-09 20:11:24 +02:00
if (rememberOutputFolder === true && outputFolderPath) {
outputFolderPath = outputFolderPath;
}
2024-01-16 10:22:31 +01:00
// ! Don't do fetchLocalStorage() again, it causes the values to be reset
2023-11-22 16:54:02 +01:00
setNoImageProcessing(payload.noImageProcessing);
2023-11-23 06:39:46 +01:00
setCompression(parseInt(payload.compression));
2023-11-22 16:54:02 +01:00
const isDefaultModel = DEFAULT_MODELS.includes(model);
2024-04-09 20:11:24 +02:00
const scale = payload.scale;
2024-04-20 17:44:42 +02:00
const useCustomWidth = payload.useCustomWidth;
const customWidth = useCustomWidth ? payload.customWidth : "";
2024-04-20 17:44:42 +02:00
2024-04-21 19:39:13 +02:00
const outputFolderName = `upscayl_${saveImageAs}_${model}_${
useCustomWidth ? `${customWidth}px` : `${scale}x`
}`;
2024-01-15 10:25:29 +01:00
outputFolderPath += slash + outputFolderName;
if (!fs.existsSync(outputFolderPath)) {
fs.mkdirSync(outputFolderPath, { recursive: true });
}
2023-12-03 07:36:00 +01:00
// Delete .DS_Store files
fs.readdirSync(inputDir).forEach((file) => {
if (
file === ".DS_Store" ||
file.toLowerCase() === "desktop.ini" ||
file.startsWith(".")
) {
2023-12-03 07:36:00 +01:00
logit("🗑️ Deleting .DS_Store file");
fs.unlinkSync(inputDir + slash + file);
}
});
// UPSCALE
const upscayl = spawnUpscayl(
2024-04-09 20:11:24 +02:00
getBatchArguments({
2023-12-03 07:36:00 +01:00
inputDir,
2024-04-09 20:11:24 +02:00
outputDir: outputFolderPath,
modelsPath: isDefaultModel
? modelsPath
: savedCustomModelsPath ?? modelsPath,
model,
gpuId,
saveImageAs,
2024-04-09 20:11:24 +02:00
scale,
2024-04-20 17:44:42 +02:00
customWidth,
2024-04-09 20:11:24 +02:00
}),
2024-02-08 15:57:35 +01:00
logit,
);
childProcesses.push(upscayl);
setStopped(false);
let failed = false;
const onData = (data: any) => {
if (!mainWindow) return;
data = data.toString();
mainWindow.webContents.send(
COMMAND.FOLDER_UPSCAYL_PROGRESS,
2024-02-08 15:57:35 +01:00
data.toString(),
);
2024-04-21 16:04:59 +02:00
if ((data as string).includes("Error")) {
logit("❌ ", data);
failed = true;
upscayl.kill();
} else if (data.includes("Resizing")) {
mainWindow.webContents.send(COMMAND.SCALING_AND_CONVERTING);
}
};
const onError = (data: any) => {
if (!mainWindow) return;
mainWindow.setProgressBar(-1);
mainWindow.webContents.send(
COMMAND.FOLDER_UPSCAYL_PROGRESS,
2024-02-08 15:57:35 +01:00
data.toString(),
);
failed = true;
upscayl.kill();
mainWindow &&
mainWindow.webContents.send(
COMMAND.UPSCAYL_ERROR,
2024-04-21 16:04:59 +02:00
`Error upscaling image! ${data}`,
);
return;
};
const onClose = () => {
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,
2024-02-08 15:57:35 +01:00
outputFolderPath,
);
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,
2023-10-14 10:41:57 +02:00
"Error processing (scaling and converting) the image. Please report this error on Upscayl GitHub Issues page.\n" +
2024-02-08 15:57:35 +01:00
error,
);
showNotification("Upscayl Failure", "Failed to upscale image!");
}
} else {
upscayl.kill();
}
};
upscayl.process.stderr.on("data", onData);
upscayl.process.on("error", onError);
upscayl.process.on("close", onClose);
};
export default batchUpscayl;