1
0
mirror of https://github.com/upscayl/upscayl.git synced 2024-11-23 23:21:05 +01:00

ADD: File Extension check in Backend

This commit is contained in:
abhishek-gaonkar 2024-10-24 22:29:41 +05:30
parent 177b555a55
commit 5605523df8
2 changed files with 29 additions and 12 deletions

View File

@ -3,16 +3,21 @@ import logit from "../utils/logit";
import fs from "fs";
import path from "path";
import { ELECTRON_COMMANDS } from "../../common/electron-commands";
import { ImageFormat, imageFormats } from "../types/types";
interface IClipboardFileParameters {
name: string;
path: string;
extension: string;
extension: ImageFormat;
size: number;
type: string;
encodedBuffer: string;
}
const isImageFormatValid = (format: string): format is ImageFormat => {
return (imageFormats as readonly string[]).includes(format);
};
const createTempFileFromClipboard = async (
inputFileParams: IClipboardFileParameters,
): Promise<string> => {
@ -23,21 +28,31 @@ const createTempFileFromClipboard = async (
return tempFilePath;
};
const pasteImage = async (event, file: IClipboardFileParameters) => {
const pasteImage = async (
event: Electron.IpcMainEvent,
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);
if (isImageFormatValid(file.extension)) {
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,
);
}
} else {
mainWindow.webContents.send(
ELECTRON_COMMANDS.PASTE_IMAGE_SAVE_ERROR,
error.message,
"Unsupported Image Format",
);
}
};

View File

@ -1 +1,3 @@
export type ImageFormat = "png" | "jpg" | "webp";
export const imageFormats = ["png", "jpg", "jpeg", "webp"] as const;
export type ImageFormat = (typeof imageFormats)[number];