1
0
mirror of https://github.com/upscayl/upscayl.git synced 2024-12-15 00:51:16 +01:00
upscayl/electron/commands/select-file.ts

83 lines
2.1 KiB
TypeScript
Raw Normal View History

2023-11-10 12:41:35 +01:00
import { MessageBoxOptions, app, dialog } from "electron";
2023-09-10 19:42:18 +02:00
import { getMainWindow } from "../main-window";
import { imagePath, setImagePath } from "../utils/config-variables";
2023-09-10 11:14:04 +02:00
import logit from "../utils/logit";
2023-11-10 12:41:35 +01:00
import settings from "electron-settings";
2023-11-23 06:39:46 +01:00
import { featureFlags } from "../../common/feature-flags";
2023-09-10 11:14:04 +02:00
const selectFile = async () => {
2023-09-10 19:54:08 +02:00
const mainWindow = getMainWindow();
2023-11-02 16:03:21 +01:00
const { canceled, filePaths, bookmarks } = await dialog.showOpenDialog({
2023-11-10 12:41:35 +01:00
properties: ["openFile"],
2023-09-10 11:14:04 +02:00
title: "Select Image",
2023-09-10 19:42:18 +02:00
defaultPath: imagePath,
2023-11-02 16:03:21 +01:00
securityScopedBookmarks: true,
2023-11-10 12:41:35 +01:00
message: "Select Image to Upscale",
filters: [
{
name: "Images",
extensions: [
"png",
"jpg",
"jpeg",
"webp",
"PNG",
"JPG",
"JPEG",
"WEBP",
],
},
],
2023-09-10 11:14:04 +02:00
});
2023-11-23 06:39:46 +01:00
if (featureFlags.APP_STORE_BUILD && bookmarks && bookmarks.length > 0) {
2023-11-10 12:41:35 +01:00
console.log("🚨 Setting Bookmark: ", bookmarks);
2023-11-02 16:03:21 +01:00
settings.set("file-bookmarks", bookmarks[0]);
}
2023-09-10 11:14:04 +02:00
if (canceled) {
logit("🚫 File Operation Cancelled");
return null;
} else {
setImagePath(filePaths[0]);
let isValid = false;
// READ SELECTED FILES
filePaths.forEach((file) => {
// log.log("Files in Folder: ", file);
if (
file.endsWith(".png") ||
file.endsWith(".jpg") ||
file.endsWith(".jpeg") ||
file.endsWith(".webp") ||
file.endsWith(".JPG") ||
file.endsWith(".PNG") ||
file.endsWith(".JPEG") ||
file.endsWith(".WEBP")
) {
isValid = true;
}
});
if (!isValid) {
logit("❌ Invalid File Detected");
const options: MessageBoxOptions = {
type: "error",
title: "Invalid File",
message:
"The selected file is not a valid image. Make sure you select a '.png', '.jpg', or '.webp' file.",
};
2023-09-10 19:42:18 +02:00
if (!mainWindow) return null;
2023-09-10 11:14:04 +02:00
dialog.showMessageBoxSync(mainWindow, options);
return null;
}
logit("📄 Selected File Path: ", filePaths[0]);
// CREATE input AND upscaled FOLDER
return filePaths[0];
}
};
export default selectFile;