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:
parent
177b555a55
commit
5605523df8
@ -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",
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
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…
x
Reference in New Issue
Block a user