mirror of
https://github.com/upscayl/upscayl.git
synced 2024-11-23 23:21:05 +01:00
MOD: Refactor few codes; Suggestions
This commit is contained in:
parent
a6157bfc80
commit
177b555a55
@ -1,63 +1,45 @@
|
||||
import { getMainWindow } from "../main-window";
|
||||
import logit from "../utils/logit";
|
||||
import fs from "fs";
|
||||
import { tmpdir, homedir } from "os";
|
||||
import path from "path";
|
||||
import { ELECTRON_COMMANDS } from "../../common/electron-commands";
|
||||
|
||||
interface IClipboardFileParameters {
|
||||
name: string;
|
||||
path: string;
|
||||
extension: string;
|
||||
size: number;
|
||||
type: string;
|
||||
encodedBuffer: string;
|
||||
}
|
||||
|
||||
const createTempFileFromClipboard = (
|
||||
appFolderPrefix: string,
|
||||
const createTempFileFromClipboard = async (
|
||||
inputFileParams: IClipboardFileParameters,
|
||||
onSuccessCallback: (inputFilePath: string, outputFilePath: string) => void,
|
||||
onErrorCallback: (error: Error) => void,
|
||||
) => {
|
||||
let tempDirectory = fs.mkdtempSync(path.join(tmpdir(), appFolderPrefix));
|
||||
let tempFilePath = path.join(tempDirectory, inputFileParams.name);
|
||||
): Promise<string> => {
|
||||
const tempFilePath = path.join(inputFileParams.path, inputFileParams.name);
|
||||
const buffer = Buffer.from(inputFileParams.encodedBuffer, "base64");
|
||||
|
||||
fs.writeFile(
|
||||
tempFilePath,
|
||||
Buffer.from(inputFileParams.encodedBuffer, "base64"),
|
||||
(err) => {
|
||||
if (err) {
|
||||
onErrorCallback(new Error("No permission to temp folder"));
|
||||
return;
|
||||
}
|
||||
let homeDirectoryAsOutputFolder = homedir();
|
||||
onSuccessCallback(tempFilePath, homeDirectoryAsOutputFolder);
|
||||
},
|
||||
);
|
||||
await fs.promises.writeFile(tempFilePath, buffer);
|
||||
return tempFilePath;
|
||||
};
|
||||
|
||||
const pasteImage = (event, file: IClipboardFileParameters) => {
|
||||
const pasteImage = async (event, file: IClipboardFileParameters) => {
|
||||
const mainWindow = getMainWindow();
|
||||
if (!mainWindow) return;
|
||||
if (!file || !file.name || !file.encodedBuffer) return;
|
||||
const appFolderPrefix = "upscayl";
|
||||
createTempFileFromClipboard(
|
||||
appFolderPrefix,
|
||||
file,
|
||||
(imageFilePath, homeDirectory) => {
|
||||
mainWindow.webContents.send(ELECTRON_COMMANDS.PASTE_IMAGE_SAVE_SUCCESS, [
|
||||
try {
|
||||
const imageFilePath = await createTempFileFromClipboard(file);
|
||||
mainWindow.webContents.send(
|
||||
ELECTRON_COMMANDS.PASTE_IMAGE_SAVE_SUCCESS,
|
||||
imageFilePath,
|
||||
homeDirectory,
|
||||
]);
|
||||
},
|
||||
(error) => {
|
||||
);
|
||||
} catch (error: any) {
|
||||
logit(error.message);
|
||||
mainWindow.webContents.send(
|
||||
ELECTRON_COMMANDS.PASTE_IMAGE_SAVE_ERROR,
|
||||
error.message,
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
export default pasteImage;
|
||||
|
@ -2,7 +2,7 @@
|
||||
import useLogger from "../hooks/use-logger";
|
||||
import { useState, useMemo, useEffect } from "react";
|
||||
import { ELECTRON_COMMANDS } from "@common/electron-commands";
|
||||
import { useAtomValue, useSetAtom } from "jotai";
|
||||
import { useAtom, useAtomValue } from "jotai";
|
||||
import {
|
||||
batchModeAtom,
|
||||
lensSizeAtom,
|
||||
@ -63,7 +63,7 @@ const MainContent = ({
|
||||
const { toast } = useToast();
|
||||
const version = useUpscaylVersion();
|
||||
|
||||
const setOutputPath = useSetAtom(savedOutputPathAtom);
|
||||
const [outputPath, setOutputPath] = useAtom(savedOutputPathAtom);
|
||||
const progress = useAtomValue(progressAtom);
|
||||
const batchMode = useAtomValue(batchModeAtom);
|
||||
|
||||
@ -163,15 +163,17 @@ const MainContent = ({
|
||||
};
|
||||
|
||||
const handlePaste = (e: React.ClipboardEvent<HTMLDivElement>) => {
|
||||
resetImagePaths();
|
||||
e.preventDefault();
|
||||
if (outputPath) {
|
||||
resetImagePaths();
|
||||
if (e.clipboardData.files.length) {
|
||||
const fileObject = e.clipboardData.files[0];
|
||||
const currentDate = new Date(Date.now());
|
||||
const currentTime = `${currentDate.getHours()}-${currentDate.getMinutes()}-${currentDate.getSeconds()}`;
|
||||
const fileName = `${currentTime}-${fileObject.name}`;
|
||||
const fileName = `.temp-${currentTime}-${fileObject.name || "image"}`;
|
||||
const file = {
|
||||
name: fileName,
|
||||
path: outputPath,
|
||||
extension: fileName.split(".").pop() as ImageFormat,
|
||||
size: fileObject.size,
|
||||
type: fileObject.type.split("/")[0],
|
||||
@ -181,8 +183,8 @@ const MainContent = ({
|
||||
logit(
|
||||
"📋 Pasted file: ",
|
||||
JSON.stringify({
|
||||
type: file.type,
|
||||
name: file.name,
|
||||
path: file.path,
|
||||
extension: file.extension,
|
||||
}),
|
||||
);
|
||||
@ -228,39 +230,40 @@ const MainContent = ({
|
||||
description: t("ERRORS.INVALID_IMAGE_ERROR.CLIPBOARD_DESCRIPTION"),
|
||||
});
|
||||
}
|
||||
} else {
|
||||
toast({
|
||||
title: t("ERRORS.NO_OUTPUT_FOLDER_ERROR.TITLE"),
|
||||
description: t("ERRORS.NO_OUTPUT_FOLDER_ERROR.DESCRIPTION"),
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
// Events
|
||||
const handlePasteEvent = (e) => handlePaste(e);
|
||||
window.addEventListener("paste", handlePasteEvent);
|
||||
window.electron.on(
|
||||
ELECTRON_COMMANDS.PASTE_IMAGE_SAVE_SUCCESS,
|
||||
(_: any, output: string[]) => {
|
||||
let [imageFilePath, homeDirectory] = output;
|
||||
const handlePasteImageSaveSuccess = (_: any, imageFilePath: string) => {
|
||||
setImagePath(imageFilePath);
|
||||
var dirname = getDirectoryFromPath(homeDirectory, false);
|
||||
logit("🗂 Setting output path: ", dirname);
|
||||
if (!FEATURE_FLAGS.APP_STORE_BUILD) {
|
||||
if (!rememberOutputFolder) {
|
||||
setOutputPath(dirname);
|
||||
}
|
||||
}
|
||||
validateImagePath(imageFilePath);
|
||||
},
|
||||
);
|
||||
window.electron.on(
|
||||
ELECTRON_COMMANDS.PASTE_IMAGE_SAVE_ERROR,
|
||||
(_: any, error: string) => {
|
||||
};
|
||||
const handlePasteImageSaveError = (_: any, error: string) => {
|
||||
toast({
|
||||
title: t("ERRORS.NO_IMAGE_ERROR.TITLE"),
|
||||
description: error,
|
||||
});
|
||||
},
|
||||
};
|
||||
window.addEventListener("paste", handlePasteEvent);
|
||||
window.electron.on(
|
||||
ELECTRON_COMMANDS.PASTE_IMAGE_SAVE_SUCCESS,
|
||||
handlePasteImageSaveSuccess,
|
||||
);
|
||||
window.electron.on(
|
||||
ELECTRON_COMMANDS.PASTE_IMAGE_SAVE_ERROR,
|
||||
handlePasteImageSaveError,
|
||||
);
|
||||
return () => {
|
||||
window.removeEventListener("paste", handlePasteEvent);
|
||||
};
|
||||
}, [t]);
|
||||
}, [t, outputPath]);
|
||||
|
||||
return (
|
||||
<div
|
||||
|
@ -193,6 +193,10 @@
|
||||
"GENERIC_ERROR": {
|
||||
"TITLE": "Error"
|
||||
},
|
||||
"NO_OUTPUT_FOLDER_ERROR": {
|
||||
"TITLE": "Set Output Folder",
|
||||
"DESCRIPTION": "Please select an output folder first"
|
||||
},
|
||||
"INVALID_IMAGE_ERROR": {
|
||||
"TITLE": "Invalid Image",
|
||||
"DESCRIPTION": "Please select/paste an image with a valid extension like PNG, JPG, JPEG, JFIF or WEBP.",
|
||||
|
@ -193,6 +193,10 @@
|
||||
"GENERIC_ERROR": {
|
||||
"TITLE": "Error"
|
||||
},
|
||||
"NO_OUTPUT_FOLDER_ERROR": {
|
||||
"TITLE": "Establecer carpeta de salida",
|
||||
"DESCRIPTION": "Por favor, selecciona primero una carpeta de salida"
|
||||
},
|
||||
"INVALID_IMAGE_ERROR": {
|
||||
"TITLE": "Imagen inválida",
|
||||
"DESCRIPTION": "Por favor, selecciona/pega una imagen con una extensión válida como PNG, JPG, JPEG, JFIF o WEBP.",
|
||||
|
@ -193,6 +193,10 @@
|
||||
"GENERIC_ERROR": {
|
||||
"TITLE": "Erreur"
|
||||
},
|
||||
"NO_OUTPUT_FOLDER_ERROR": {
|
||||
"TITLE": "Définir le dossier de sortie",
|
||||
"DESCRIPTION": "Veuillez d'abord sélectionner un dossier de sortie"
|
||||
},
|
||||
"INVALID_IMAGE_ERROR": {
|
||||
"TITLE": "Image invalide",
|
||||
"DESCRIPTION": "Veuillez sélectionner/coller une image avec une extension valide comme PNG, JPG, JPEG, JFIF ou WEBP.",
|
||||
|
@ -193,6 +193,10 @@
|
||||
"GENERIC_ERROR": {
|
||||
"TITLE": "エラー"
|
||||
},
|
||||
"NO_OUTPUT_FOLDER_ERROR": {
|
||||
"TITLE": "出力フォルダを設定",
|
||||
"DESCRIPTION": "まず出力フォルダを選択してください"
|
||||
},
|
||||
"INVALID_IMAGE_ERROR": {
|
||||
"TITLE": "無効な画像",
|
||||
"DESCRIPTION": "PNG、JPG、JPEG、JFIF、または WEBP のような有効な拡張子の画像を選択/貼り付けてください。",
|
||||
|
@ -193,6 +193,10 @@
|
||||
"GENERIC_ERROR": {
|
||||
"TITLE": "Ошибка"
|
||||
},
|
||||
"NO_OUTPUT_FOLDER_ERROR": {
|
||||
"TITLE": "Установить папку вывода",
|
||||
"DESCRIPTION": "Пожалуйста, сначала выберите папку вывода"
|
||||
},
|
||||
"INVALID_IMAGE_ERROR": {
|
||||
"TITLE": "Неверное изображение",
|
||||
"DESCRIPTION": "Пожалуйста, выберите/вставьте изображение с допустимым расширением, таким как PNG, JPG, JPEG, JFIF или WEBP.",
|
||||
|
@ -193,6 +193,10 @@
|
||||
"GENERIC_ERROR": {
|
||||
"TITLE": "错误"
|
||||
},
|
||||
"NO_OUTPUT_FOLDER_ERROR": {
|
||||
"TITLE": "选择输出文件夹",
|
||||
"DESCRIPTION": "请先选择一个输出文件夹"
|
||||
},
|
||||
"INVALID_IMAGE_ERROR": {
|
||||
"TITLE": "图片无效",
|
||||
"DESCRIPTION": "请选择/粘貼一个扩展名为 PNG、JPG、JPEG、JFIF 或 WEBP 的有效图片",
|
||||
|
Loading…
Reference in New Issue
Block a user