1
0
mirror of https://github.com/upscayl/upscayl.git synced 2024-12-18 10:26:04 +01:00
upscayl/electron/commands/image-upscayl.ts

161 lines
4.8 KiB
TypeScript
Raw Normal View History

2023-09-10 11:14:04 +02:00
import fs from "fs";
2023-09-10 20:00:49 +02:00
import { modelsPath } from "../utils/get-resource-paths";
2024-10-06 09:15:44 +02:00
import { ELECTRON_COMMANDS } from "../../common/electron-commands";
2023-09-10 11:14:04 +02:00
import {
2024-04-09 20:11:24 +02:00
savedCustomModelsPath,
2023-09-10 11:14:04 +02:00
setChildProcesses,
2023-09-10 19:42:18 +02:00
setStopped,
stopped,
2023-09-10 11:14:04 +02:00
} from "../utils/config-variables";
import { getSingleImageArguments } from "../utils/get-arguments";
import logit from "../utils/logit";
import slash from "../utils/slash";
import { spawnUpscayl } from "../utils/spawn-upscayl";
import { parse } from "path";
2023-09-10 19:42:18 +02:00
import { getMainWindow } from "../main-window";
2023-11-22 16:54:02 +01:00
import { ImageUpscaylPayload } from "../../common/types/types";
2024-04-24 12:34:32 +02:00
import { ImageFormat } from "../types/types";
import showNotification from "../utils/show-notification";
2024-10-06 09:15:44 +02:00
import { DEFAULT_MODELS_ID_LIST } from "../../common/models-list";
import getFilenameFromPath from "../../common/get-file-name";
import decodePath from "../../common/decode-path";
import getDirectoryFromPath from "../../common/get-directory-from-path";
2023-09-10 11:14:04 +02:00
2023-11-22 16:54:02 +01:00
const imageUpscayl = async (event, payload: ImageUpscaylPayload) => {
2023-09-10 19:42:18 +02:00
const mainWindow = getMainWindow();
if (!mainWindow) {
2023-09-11 04:30:50 +02:00
logit("No main window found");
2023-09-10 19:42:18 +02:00
return;
}
2023-09-10 19:59:35 +02:00
2024-04-09 20:11:24 +02:00
// GET VARIABLES
const tileSize = payload.tileSize;
const compression = payload.compression;
const scale = payload.scale;
const useCustomWidth = payload.useCustomWidth;
const customWidth = useCustomWidth ? payload.customWidth : "";
2023-09-10 11:14:04 +02:00
const model = payload.model as string;
const gpuId = payload.gpuId as string;
const saveImageAs = payload.saveImageAs as ImageFormat;
const overwrite = payload.overwrite as boolean;
const imagePath = decodePath(payload.imagePath);
let inputDir = getDirectoryFromPath(imagePath);
let outputDir = decodePath(payload.outputPath);
const fileNameWithExt = getFilenameFromPath(imagePath);
const fileName = parse(fileNameWithExt).name;
2023-09-18 20:30:43 +02:00
2023-09-10 11:14:04 +02:00
const outFile =
2024-04-21 19:40:54 +02:00
outputDir +
slash +
fileName +
"_upscayl_" +
(useCustomWidth ? `${customWidth}px_` : `${scale}x_`) +
model +
"." +
saveImageAs;
2023-09-10 11:14:04 +02:00
2024-10-06 09:15:44 +02:00
const isDefaultModel = DEFAULT_MODELS_ID_LIST.includes(model);
// Check if windows can write the new filename to the file system
if (outFile.length >= 255) {
logit("Filename too long for Windows.");
mainWindow.webContents.send(
ELECTRON_COMMANDS.UPSCAYL_ERROR,
"The filename exceeds the maximum path length allowed by Windows. Please shorten the filename or choose a different save location.",
);
}
2023-09-10 11:14:04 +02:00
// UPSCALE
2023-09-10 19:54:08 +02:00
if (fs.existsSync(outFile) && !overwrite) {
2023-09-10 11:14:04 +02:00
// If already upscayled, just output that file
logit("✅ Already upscayled at: ", outFile);
mainWindow.webContents.send(ELECTRON_COMMANDS.UPSCAYL_DONE, outFile);
2023-09-10 11:14:04 +02:00
} else {
2023-11-27 03:21:17 +01:00
logit(
"✅ Upscayl Variables: ",
JSON.stringify({
model,
gpuId,
saveImageAs,
inputDir,
fileNameWithExt,
2023-11-27 03:21:17 +01:00
outputDir,
outFile,
2023-11-27 03:21:17 +01:00
fileName,
2024-04-21 19:39:13 +02:00
scale,
compression,
customWidth,
useCustomWidth,
tileSize,
2024-02-08 15:57:35 +01:00
}),
2023-11-27 03:21:17 +01:00
);
2023-09-10 11:14:04 +02:00
const upscayl = spawnUpscayl(
2024-04-09 20:11:24 +02:00
getSingleImageArguments({
inputDir: decodeURIComponent(inputDir),
fileNameWithExt: decodeURIComponent(fileNameWithExt),
outFile,
2024-04-09 20:11:24 +02:00
modelsPath: isDefaultModel
? modelsPath
: savedCustomModelsPath ?? modelsPath,
2023-09-10 11:14:04 +02:00
model,
2024-04-21 19:39:13 +02:00
scale,
2023-09-10 11:14:04 +02:00
gpuId,
2024-02-08 15:57:35 +01:00
saveImageAs,
2024-04-20 17:44:42 +02:00
customWidth,
compression,
tileSize,
2024-04-09 20:11:24 +02:00
}),
2024-02-08 15:57:35 +01:00
logit,
2023-09-10 11:14:04 +02:00
);
setChildProcesses(upscayl);
2023-09-10 19:42:18 +02:00
setStopped(false);
2023-09-10 11:14:04 +02:00
let failed = false;
const onData = (data: string) => {
logit(data.toString());
2023-09-10 11:14:04 +02:00
mainWindow.setProgressBar(parseFloat(data.slice(0, data.length)) / 100);
data = data.toString();
mainWindow.webContents.send(
ELECTRON_COMMANDS.UPSCAYL_PROGRESS,
data.toString(),
);
if (data.includes("Error")) {
2023-09-10 11:14:04 +02:00
upscayl.kill();
failed = true;
} else if (data.includes("Resizing")) {
mainWindow.webContents.send(ELECTRON_COMMANDS.SCALING_AND_CONVERTING);
2023-09-10 11:14:04 +02:00
}
};
const onError = (data) => {
if (!mainWindow) return;
mainWindow.setProgressBar(-1);
mainWindow.webContents.send(
ELECTRON_COMMANDS.UPSCAYL_ERROR,
data.toString(),
);
2023-09-10 11:14:04 +02:00
failed = true;
upscayl.kill();
return;
};
const onClose = async () => {
2023-09-10 19:42:18 +02:00
if (!failed && !stopped) {
2023-09-10 11:14:04 +02:00
logit("💯 Done upscaling");
2023-09-19 17:04:31 +02:00
// Free up memory
upscayl.kill();
mainWindow.setProgressBar(-1);
mainWindow.webContents.send(ELECTRON_COMMANDS.UPSCAYL_DONE, outFile);
showNotification("Upscayl", "Image upscayled successfully!");
2023-09-10 11:14:04 +02:00
}
};
upscayl.process.stderr.on("data", onData);
upscayl.process.on("error", onError);
upscayl.process.on("close", onClose);
}
};
export default imageUpscayl;