1
0
mirror of https://github.com/upscayl/upscayl.git synced 2024-11-12 01:40:53 +01:00

Merge pull request #285 from upscayl/localStorage-fix

Local storage fix
This commit is contained in:
Suvojit Ghosh 2023-04-28 22:21:21 +05:30 committed by GitHub
commit 9ec6afc54e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 118 additions and 97 deletions

View File

@ -32,6 +32,17 @@ import commands from "./commands";
log.initialize({ preload: true });
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
}
// Path variables for file and folder selection
let imagePath: string | undefined = undefined;
let folderPath: string | undefined = undefined;
let customModelsFolderPath: string | undefined = undefined;
let outputFolderPath: string | undefined = undefined;
let saveOutputFolder = false;
// Prepare the renderer once the app is ready
let mainWindow: BrowserWindow;
app.on("ready", async () => {
@ -80,44 +91,53 @@ app.on("ready", async () => {
autoUpdater.checkForUpdates();
}
// // SAVE LAST IMAGE PATH TO LOCAL STORAGE
// mainWindow.webContents
// .executeJavaScript('localStorage.getItem("lastImagePath");', true)
// .then((lastImagePath: string | null) => {
// if (lastImagePath && lastImagePath.length > 0) {
// imagePath = lastImagePath;
// }
// });
// GET LAST IMAGE PATH TO LOCAL STORAGE
mainWindow.webContents
.executeJavaScript('localStorage.getItem("lastImagePath");', true)
.then((lastImagePath: string | null) => {
if (lastImagePath && lastImagePath.length > 0) {
imagePath = lastImagePath;
}
});
// // SAVE LAST FOLDER PATH TO LOCAL STORAGE
// mainWindow.webContents
// .executeJavaScript('localStorage.getItem("lastFolderPath");', true)
// .then((lastFolderPath: string | null) => {
// if (lastFolderPath && lastFolderPath.length > 0) {
// folderPath = lastFolderPath;
// }
// });
// GET LAST FOLDER PATH TO LOCAL STORAGE
mainWindow.webContents
.executeJavaScript('localStorage.getItem("lastFolderPath");', true)
.then((lastFolderPath: string | null) => {
if (lastFolderPath && lastFolderPath.length > 0) {
folderPath = lastFolderPath;
}
});
// // SAVE LAST CUSTOM MODELS FOLDER PATH TO LOCAL STORAGE
// mainWindow.webContents
// .executeJavaScript(
// 'localStorage.getItem("lastCustomModelsFolderPath");',
// true
// )
// .then((lastCustomModelsFolderPath: string | null) => {
// if (lastCustomModelsFolderPath && lastCustomModelsFolderPath.length > 0) {
// customModelsFolderPath = lastCustomModelsFolderPath;
// }
// });
// GET LAST CUSTOM MODELS FOLDER PATH TO LOCAL STORAGE
mainWindow.webContents
.executeJavaScript(
'localStorage.getItem("lastCustomModelsFolderPath");',
true
)
.then((lastCustomModelsFolderPath: string | null) => {
if (lastCustomModelsFolderPath && lastCustomModelsFolderPath.length > 0) {
customModelsFolderPath = lastCustomModelsFolderPath;
}
});
// // SAVE LAST CUSTOM MODELS FOLDER PATH TO LOCAL STORAGE
// mainWindow.webContents
// .executeJavaScript('localStorage.getItem("lastOutputFolderPath");', true)
// .then((lastOutputFolderPath: string | null) => {
// if (lastOutputFolderPath && lastOutputFolderPath.length > 0) {
// outputFolderPath = lastOutputFolderPath;
// }
// });
// GET LAST CUSTOM MODELS FOLDER PATH TO LOCAL STORAGE
mainWindow.webContents
.executeJavaScript('localStorage.getItem("lastOutputFolderPath");', true)
.then((lastOutputFolderPath: string | null) => {
if (lastOutputFolderPath && lastOutputFolderPath.length > 0) {
outputFolderPath = lastOutputFolderPath;
}
});
// GET LAST SAVE OUTPUT FOLDER (BOOLEAN) TO LOCAL STORAGE
mainWindow.webContents
.executeJavaScript('localStorage.getItem("rememberOutputFolder");', true)
.then((lastSaveOutputFolder: boolean | null) => {
if (lastSaveOutputFolder !== null) {
saveOutputFolder = lastSaveOutputFolder;
}
});
});
// Quit the app once all windows are closed
@ -130,12 +150,6 @@ const logit = (...args: any) => {
mainWindow.webContents.send(commands.LOG, args.join(" "));
};
// Path variables for file and folder selection
let imagePath: string | undefined = undefined;
let folderPath: string | undefined = undefined;
let customModelsFolderPath: string | undefined = undefined;
let outputFolderPath: string | undefined = undefined;
// Default models
const defaultModels = [
"realesrgan-x4plus",
@ -159,16 +173,7 @@ ipcMain.handle(commands.SELECT_FILE, async () => {
return null;
} else {
logit("Selected File Path: ", filePaths[0]);
imagePath = filePaths[0];
// mainWindow.webContents
// .executeJavaScript(
// `localStorage.setItem("lastImagePath", "${imagePath}");`,
// true
// )
// .then(() => {
// logit(`Saved Last Image Path (${imagePath}) to Local Storage`);
// });
let isValid = false;
// READ SELECTED FILES
@ -215,14 +220,6 @@ ipcMain.handle(commands.SELECT_FOLDER, async (event, message) => {
} else {
logit("Selected Folder Path: ", folderPaths[0]);
folderPath = folderPaths[0];
// mainWindow.webContents
// .executeJavaScript(
// `localStorage.setItem("lastImagePath", "${folderPath}");`,
// true
// )
// .then(() => {
// logit(`Saved Last Image Path (${folderPath}) to Local Storage`);
// });
return folderPaths[0];
}
});
@ -322,6 +319,10 @@ ipcMain.on(commands.DOUBLE_UPSCAYL, async (event, payload) => {
const model = payload.model as string;
let inputDir = (payload.imagePath.match(/(.*)[\/\\]/)[1] || "") as string;
let outputDir = payload.outputPath as string;
if (saveOutputFolder === true && outputFolderPath) {
outputDir = outputFolderPath;
}
const gpuId = payload.gpuId as string;
const saveImageAs = payload.saveImageAs as string;
const scale = payload.scale as string;
@ -442,7 +443,11 @@ ipcMain.on(commands.UPSCAYL, async (event, payload) => {
const gpuId = payload.gpuId as string;
const saveImageAs = payload.saveImageAs as string;
let inputDir = (payload.imagePath.match(/(.*)[\/\\]/)[1] || "") as string;
let outputDir = payload.outputPath as string;
let outputDir = folderPath || (payload.outputPath as string);
if (saveOutputFolder === true && outputFolderPath) {
outputDir = outputFolderPath;
}
const isDefaultModel = defaultModels.includes(model);
@ -539,6 +544,10 @@ ipcMain.on(commands.FOLDER_UPSCAYL, async (event, payload) => {
// GET THE OUTPUT DIRECTORY
let outputDir = payload.outputPath;
if (saveOutputFolder === true && outputFolderPath) {
outputDir = outputFolderPath;
}
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}

View File

@ -10,7 +10,6 @@ interface IProps {
selectImageHandler: () => Promise<void>;
selectFolderHandler: () => Promise<void>;
handleModelChange: (e: any) => void;
handleDrop: (e: any) => void;
outputHandler: () => Promise<void>;
upscaylHandler: () => Promise<void>;
batchMode: boolean;
@ -19,13 +18,8 @@ interface IProps {
outputPath: string;
doubleUpscayl: boolean;
setDoubleUpscayl: React.Dispatch<React.SetStateAction<boolean>>;
model: string;
setModel: React.Dispatch<React.SetStateAction<string>>;
isVideo: boolean;
setIsVideo: React.Dispatch<React.SetStateAction<boolean>>;
saveImageAs: string;
setSaveImageAs: React.Dispatch<React.SetStateAction<string>>;
gpuId: string;
setGpuId: React.Dispatch<React.SetStateAction<string>>;
dimensions: {
width: number | null;
@ -38,7 +32,6 @@ function LeftPaneImageSteps({
selectImageHandler,
selectFolderHandler,
handleModelChange,
handleDrop,
outputHandler,
upscaylHandler,
batchMode,
@ -47,13 +40,8 @@ function LeftPaneImageSteps({
outputPath,
doubleUpscayl,
setDoubleUpscayl,
model,
setModel,
isVideo,
setIsVideo,
gpuId,
setGpuId,
saveImageAs,
setSaveImageAs,
dimensions,
}: IProps) {
@ -176,7 +164,7 @@ function LeftPaneImageSteps({
type="checkbox"
className="toggle"
defaultChecked={batchMode}
onClick={handleBatchMode}></input>
onClick={() => setBatchMode((oldValue) => !oldValue)}></input>
<p
className="mr-1 inline-block cursor-help text-sm"
data-tip="This will let you upscale all files in a folder at once">

View File

@ -2,17 +2,11 @@ import React, { useEffect, useState } from "react";
import { themeChange } from "theme-change";
import commands from "../../electron/commands";
import { useAtom } from "jotai";
import {
customModelsPathAtom,
rememberOutputFolderAtom,
scaleAtom,
} from "../atoms/userSettingsAtom";
import { customModelsPathAtom, scaleAtom } from "../atoms/userSettingsAtom";
import { modelsListAtom } from "../atoms/modelsListAtom";
interface IProps {
batchMode: boolean;
setBatchMode: React.Dispatch<React.SetStateAction<boolean>>;
imagePath: string;
setModel: React.Dispatch<React.SetStateAction<string>>;
saveImageAs: string;
setSaveImageAs: React.Dispatch<React.SetStateAction<string>>;
@ -23,8 +17,6 @@ interface IProps {
function SettingsTab({
batchMode,
setBatchMode,
imagePath,
setModel,
gpuId,
setGpuId,
@ -45,12 +37,11 @@ function SettingsTab({
const [customModelsPath, setCustomModelsPath] = useAtom(customModelsPathAtom);
const [modelOptions, setModelOptions] = useAtom(modelsListAtom);
const [rememberOutputFolder, setRememberOutputFolder] = useAtom(
rememberOutputFolderAtom
);
const [scale, setScale] = useAtom(scaleAtom);
const [rememberOutputFolder, setRememberOutputFolder] = useState(false);
useEffect(() => {
themeChange(false);
@ -79,6 +70,22 @@ function SettingsTab({
const currentlySavedGpuId = localStorage.getItem("gpuId");
setGpuId(currentlySavedGpuId);
}
if (!localStorage.getItem("rememberOutputFolder")) {
localStorage.setItem("rememberOutputFolder", "false");
} else {
const currentlySavedRememberOutputFolder = localStorage.getItem(
"rememberOutputFolder"
);
console.log(
"🚀 => file: SettingsTab.tsx:80 => currentlySavedRememberOutputFolder:",
currentlySavedRememberOutputFolder
);
setRememberOutputFolder(
currentlySavedRememberOutputFolder === "true" ? true : false
);
}
}, []);
useEffect(() => {
@ -158,9 +165,19 @@ function SettingsTab({
<input
type="checkbox"
className="toggle-primary toggle"
defaultChecked={rememberOutputFolder}
onChange={() => {
setRememberOutputFolder((oldValue) => !oldValue);
checked={rememberOutputFolder}
onClick={() => {
setRememberOutputFolder((oldValue) => {
if (oldValue === true) {
localStorage.removeItem("lastOutputFolderPath");
}
return !oldValue;
});
localStorage.setItem(
"rememberOutputFolder",
JSON.stringify(!rememberOutputFolder)
);
}}
/>
</div>
@ -268,7 +285,7 @@ function SettingsTab({
<div className="relative flex flex-col gap-2">
<button
className="btn-primary btn-xs btn absolute top-10 right-2 z-10"
className="btn-primary btn-xs btn absolute right-2 top-10 z-10"
onClick={copyOnClickHandler}>
{isCopied ? <span>Copied 📋</span> : <span>Copy 📋</span>}
</button>

View File

@ -199,6 +199,18 @@ const Home = () => {
}
}, []);
useEffect(() => {
const rememberOutputFolder = localStorage.getItem("rememberOutputFolder");
const lastOutputFolderPath = localStorage.getItem("lastOutputFolderPath");
if (rememberOutputFolder === "true") {
setOutputPath(lastOutputFolderPath);
} else {
setOutputPath("");
localStorage.removeItem("lastOutputFolderPath");
}
}, []);
useEffect(() => {
setProgress("");
}, [batchMode]);
@ -366,7 +378,6 @@ const Home = () => {
}
var dirname = filePath.match(/(.*)[\/\\]/)[1] || "";
console.log("🚀 => handleDrop => dirname", dirname);
setOutputPath(dirname);
}
};
@ -395,6 +406,10 @@ const Home = () => {
var path = await window.electron.invoke(commands.SELECT_FOLDER);
if (path !== null) {
setOutputPath(path);
const rememberOutputFolder = localStorage.getItem("rememberOutputFolder");
if (rememberOutputFolder) {
localStorage.setItem("lastOutputFolderPath", path);
}
} else {
console.log("Getting output path from input file");
}
@ -513,7 +528,6 @@ const Home = () => {
selectImageHandler={selectImageHandler}
selectFolderHandler={selectFolderHandler}
handleModelChange={handleModelChange}
handleDrop={handleDrop}
outputHandler={outputHandler}
upscaylHandler={upscaylHandler}
batchMode={batchMode}
@ -522,13 +536,8 @@ const Home = () => {
outputPath={outputPath}
doubleUpscayl={doubleUpscayl}
setDoubleUpscayl={setDoubleUpscayl}
model={model}
setModel={setModel}
isVideo={isVideo}
setIsVideo={setIsVideo}
gpuId={gpuId}
setGpuId={setGpuId}
saveImageAs={saveImageAs}
setSaveImageAs={setSaveImageAs}
dimensions={dimensions}
/>
@ -537,8 +546,6 @@ const Home = () => {
{selectedTab === 1 && (
<SettingsTab
batchMode={batchMode}
setBatchMode={setBatchMode}
imagePath={imagePath}
setModel={setModel}
gpuId={gpuId}
setGpuId={setGpuId}