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:
parent
177b555a55
commit
5605523df8
@ -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",
|
||||
);
|
||||
}
|
||||
};
|
||||
|
4
electron/types/types.d.ts
vendored
4
electron/types/types.d.ts
vendored
@ -1 +1,3 @@
|
||||
export type ImageFormat = "png" | "jpg" | "webp";
|
||||
export const imageFormats = ["png", "jpg", "jpeg", "webp"] as const;
|
||||
|
||||
export type ImageFormat = (typeof imageFormats)[number];
|
||||
|
Loading…
Reference in New Issue
Block a user