1
0
mirror of https://github.com/upscayl/upscayl.git synced 2024-11-24 07:30:19 +01:00
upscayl/electron/commands/paste-image.ts
2024-10-24 17:38:51 +05:30

46 lines
1.2 KiB
TypeScript

import { getMainWindow } from "../main-window";
import logit from "../utils/logit";
import fs from "fs";
import path from "path";
import { ELECTRON_COMMANDS } from "../../common/electron-commands";
interface IClipboardFileParameters {
name: string;
path: string;
extension: string;
size: number;
type: string;
encodedBuffer: string;
}
const createTempFileFromClipboard = async (
inputFileParams: IClipboardFileParameters,
): Promise<string> => {
const tempFilePath = path.join(inputFileParams.path, inputFileParams.name);
const buffer = Buffer.from(inputFileParams.encodedBuffer, "base64");
await fs.promises.writeFile(tempFilePath, buffer);
return tempFilePath;
};
const pasteImage = async (event, file: IClipboardFileParameters) => {
const mainWindow = getMainWindow();
if (!mainWindow) return;
if (!file || !file.name || !file.encodedBuffer) return;
try {
const imageFilePath = await createTempFileFromClipboard(file);
mainWindow.webContents.send(
ELECTRON_COMMANDS.PASTE_IMAGE_SAVE_SUCCESS,
imageFilePath,
);
} catch (error: any) {
logit(error.message);
mainWindow.webContents.send(
ELECTRON_COMMANDS.PASTE_IMAGE_SAVE_ERROR,
error.message,
);
}
};
export default pasteImage;