1
0
mirror of https://github.com/upscayl/upscayl.git synced 2025-02-17 11:18:36 +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 fs from "fs";
import path from "path"; import path from "path";
import { ELECTRON_COMMANDS } from "../../common/electron-commands"; import { ELECTRON_COMMANDS } from "../../common/electron-commands";
import { ImageFormat, imageFormats } from "../types/types";
interface IClipboardFileParameters { interface IClipboardFileParameters {
name: string; name: string;
path: string; path: string;
extension: string; extension: ImageFormat;
size: number; size: number;
type: string; type: string;
encodedBuffer: string; encodedBuffer: string;
} }
const isImageFormatValid = (format: string): format is ImageFormat => {
return (imageFormats as readonly string[]).includes(format);
};
const createTempFileFromClipboard = async ( const createTempFileFromClipboard = async (
inputFileParams: IClipboardFileParameters, inputFileParams: IClipboardFileParameters,
): Promise<string> => { ): Promise<string> => {
@ -23,21 +28,31 @@ const createTempFileFromClipboard = async (
return tempFilePath; return tempFilePath;
}; };
const pasteImage = async (event, file: IClipboardFileParameters) => { const pasteImage = async (
event: Electron.IpcMainEvent,
file: IClipboardFileParameters,
) => {
const mainWindow = getMainWindow(); const mainWindow = getMainWindow();
if (!mainWindow) return; if (!mainWindow) return;
if (!file || !file.name || !file.encodedBuffer) return; if (!file || !file.name || !file.encodedBuffer) return;
try { if (isImageFormatValid(file.extension)) {
const imageFilePath = await createTempFileFromClipboard(file); try {
mainWindow.webContents.send( const imageFilePath = await createTempFileFromClipboard(file);
ELECTRON_COMMANDS.PASTE_IMAGE_SAVE_SUCCESS, mainWindow.webContents.send(
imageFilePath, ELECTRON_COMMANDS.PASTE_IMAGE_SAVE_SUCCESS,
); imageFilePath,
} catch (error: any) { );
logit(error.message); } catch (error: any) {
logit(error.message);
mainWindow.webContents.send(
ELECTRON_COMMANDS.PASTE_IMAGE_SAVE_ERROR,
error.message,
);
}
} else {
mainWindow.webContents.send( mainWindow.webContents.send(
ELECTRON_COMMANDS.PASTE_IMAGE_SAVE_ERROR, 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];