2024-01-15 12:31:40 +01:00
|
|
|
import fs from "fs";
|
2023-09-11 04:25:14 +02:00
|
|
|
import { getMainWindow } from "../main-window";
|
|
|
|
import {
|
|
|
|
childProcesses,
|
2024-04-09 20:11:24 +02:00
|
|
|
savedCustomModelsPath,
|
2023-09-11 04:25:14 +02:00
|
|
|
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";
|
2024-01-16 07:30:07 +01:00
|
|
|
import COMMAND from "../../common/commands";
|
2023-11-22 16:54:02 +01:00
|
|
|
import { BatchUpscaylPayload } from "../../common/types/types";
|
2024-01-15 12:30:59 +01:00
|
|
|
import showNotification from "../utils/show-notification";
|
2024-01-16 07:30:07 +01:00
|
|
|
import { DEFAULT_MODELS } from "../../common/models-list";
|
2023-09-11 04:25:14 +02:00
|
|
|
|
2023-11-22 16:54:02 +01:00
|
|
|
const batchUpscayl = async (event, payload: BatchUpscaylPayload) => {
|
2023-09-11 04:25:14 +02:00
|
|
|
const mainWindow = getMainWindow();
|
|
|
|
if (!mainWindow) return;
|
|
|
|
|
2024-04-24 21:40:48 +02:00
|
|
|
const tileSize = payload.tileSize;
|
|
|
|
const compression = payload.compression;
|
2024-04-09 20:11:24 +02:00
|
|
|
const scale = payload.scale;
|
2024-04-20 17:44:42 +02:00
|
|
|
const useCustomWidth = payload.useCustomWidth;
|
2024-04-21 19:14:39 +02:00
|
|
|
const customWidth = useCustomWidth ? payload.customWidth : "";
|
2024-04-24 21:29:51 +02:00
|
|
|
const model = payload.model;
|
|
|
|
const gpuId = payload.gpuId;
|
|
|
|
const saveImageAs = payload.saveImageAs;
|
|
|
|
// GET THE IMAGE DIRECTORY
|
|
|
|
let inputDir = decodeURIComponent(payload.batchFolderPath);
|
|
|
|
// GET THE OUTPUT DIRECTORY
|
|
|
|
let outputFolderPath = decodeURIComponent(payload.outputPath);
|
2024-04-21 19:39:13 +02:00
|
|
|
const outputFolderName = `upscayl_${saveImageAs}_${model}_${
|
|
|
|
useCustomWidth ? `${customWidth}px` : `${scale}x`
|
|
|
|
}`;
|
2024-01-15 12:30:59 +01:00
|
|
|
outputFolderPath += slash + outputFolderName;
|
2024-04-24 21:29:51 +02:00
|
|
|
// CREATE THE OUTPUT DIRECTORY
|
2024-01-15 12:30:59 +01:00
|
|
|
if (!fs.existsSync(outputFolderPath)) {
|
|
|
|
fs.mkdirSync(outputFolderPath, { recursive: true });
|
2023-09-11 04:25:14 +02:00
|
|
|
}
|
2023-12-03 07:36:00 +01:00
|
|
|
|
2024-04-24 21:29:51 +02:00
|
|
|
const isDefaultModel = DEFAULT_MODELS.includes(model);
|
2023-09-11 04:25:14 +02:00
|
|
|
|
|
|
|
// 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,
|
2023-09-11 04:25:14 +02:00
|
|
|
model,
|
|
|
|
gpuId,
|
2024-01-15 13:46:21 +01:00
|
|
|
saveImageAs,
|
2024-04-09 20:11:24 +02:00
|
|
|
scale,
|
2024-04-20 17:44:42 +02:00
|
|
|
customWidth,
|
2024-04-24 21:40:48 +02:00
|
|
|
compression,
|
|
|
|
tileSize,
|
2024-04-09 20:11:24 +02:00
|
|
|
}),
|
2024-02-08 15:57:35 +01:00
|
|
|
logit,
|
2023-09-11 04:25:14 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
childProcesses.push(upscayl);
|
|
|
|
|
|
|
|
setStopped(false);
|
|
|
|
let failed = false;
|
2024-04-24 15:16:35 +02:00
|
|
|
let encounteredError = false;
|
2023-09-11 04:25:14 +02:00
|
|
|
|
|
|
|
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(),
|
2023-09-11 04:25:14 +02:00
|
|
|
);
|
2024-04-21 16:04:59 +02:00
|
|
|
if ((data as string).includes("Error")) {
|
|
|
|
logit("❌ ", data);
|
2024-04-24 15:16:35 +02:00
|
|
|
encounteredError = true;
|
2024-04-21 19:14:39 +02:00
|
|
|
} else if (data.includes("Resizing")) {
|
|
|
|
mainWindow.webContents.send(COMMAND.SCALING_AND_CONVERTING);
|
2023-09-11 04:25:14 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
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(),
|
2023-09-11 04:25:14 +02:00
|
|
|
);
|
|
|
|
failed = true;
|
|
|
|
upscayl.kill();
|
|
|
|
mainWindow &&
|
|
|
|
mainWindow.webContents.send(
|
|
|
|
COMMAND.UPSCAYL_ERROR,
|
2024-04-24 15:16:35 +02:00
|
|
|
`Error upscaling images! ${data}`,
|
2023-09-11 04:25:14 +02:00
|
|
|
);
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
const onClose = () => {
|
|
|
|
if (!mainWindow) return;
|
|
|
|
if (!failed && !stopped) {
|
|
|
|
logit("💯 Done upscaling");
|
|
|
|
upscayl.kill();
|
2024-04-24 15:16:35 +02:00
|
|
|
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!",
|
2024-01-15 12:30:59 +01:00
|
|
|
);
|
2023-09-11 04:25:14 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
upscayl.kill();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
upscayl.process.stderr.on("data", onData);
|
|
|
|
upscayl.process.on("error", onError);
|
|
|
|
upscayl.process.on("close", onClose);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default batchUpscayl;
|