diff --git a/electron/commands/paste-image.ts b/electron/commands/paste-image.ts index 1107647..323c9a0 100644 --- a/electron/commands/paste-image.ts +++ b/electron/commands/paste-image.ts @@ -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 => { @@ -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", ); } }; diff --git a/electron/types/types.d.ts b/electron/types/types.d.ts index 8a7f4c0..77c1da5 100644 --- a/electron/types/types.d.ts +++ b/electron/types/types.d.ts @@ -1 +1,3 @@ -export type ImageFormat = "png" | "jpg" | "webp"; +export const imageFormats = ["png", "jpg", "jpeg", "webp"] as const; + +export type ImageFormat = (typeof imageFormats)[number];