Refactor code and remove config vars to fix ASCII path decoding

This commit is contained in:
Nayam Amarshe
2024-04-25 00:59:51 +05:30
parent 618b46f553
commit 5feabea516
15 changed files with 142 additions and 159 deletions

5
common/decode-path.ts Normal file
View File

@@ -0,0 +1,5 @@
import path from "path";
export default function decodePath(filePath: string): string {
return path.normalize(decodeURIComponent(filePath));
}

View File

@@ -0,0 +1,15 @@
export default function getDirectoryFromPath(filePath: string): string {
// Define the path separator based on the operating system
const separator = filePath.includes("/") ? "/" : "\\";
// Split the file path by the path separator
const pathParts = filePath.split(separator);
// Remove the last element to get the directory
pathParts.pop();
// Join the remaining parts back together to form the directory path
const directoryPath = pathParts.join(separator);
return directoryPath || "";
}

8
common/get-file-name.ts Normal file
View File

@@ -0,0 +1,8 @@
export default function getFilenameFromPath(
path: string,
withExtension: boolean = true,
) {
if (!path) return "";
if (withExtension) return path.split("/").slice(-1)[0];
return path.split("/").slice(-1)[0].split(".").slice(0, -1).join(".");
}

24
common/sanitize-path.ts Normal file
View File

@@ -0,0 +1,24 @@
export function sanitizePath(filePath: string) {
// const protocolPrefix = "file://";
// Normalize the file path to use forward slashes (for Windows)
const normalizedFilePath = filePath.replace(/\\/g, "/");
// Split the file path into segments based on forward slashes
const pathSegments = normalizedFilePath.split("/");
// Encode each segment separately using encodeURIComponent
const encodedPathSegments = pathSegments.map((segment) =>
encodeURIComponent(segment),
);
// Join the encoded segments back together with forward slashes
const encodedFilePath = encodedPathSegments.join("/");
// Combine the protocol prefix with the encoded file path to create the final file URL
const fileUrl = encodedFilePath;
console.log("🚀 => fileUrl:", fileUrl);
// Return the final Electron file URL
return fileUrl;
}

View File

@@ -1,10 +1,12 @@
import { ImageFormat } from "@electron/types/types";
export type ImageUpscaylPayload = {
imagePath: string;
outputPath?: string;
outputPath: string;
scale: string;
model: string;
gpuId: string;
saveImageAs: string;
saveImageAs: ImageFormat;
overwrite: boolean;
compression: string;
noImageProcessing: boolean;
@@ -14,11 +16,14 @@ export type ImageUpscaylPayload = {
export type DoubleUpscaylPayload = {
model: string;
/**
* The path to the image to upscale.
*/
imagePath: string;
outputPath: string;
scale: string;
gpuId: string;
saveImageAs: string;
saveImageAs: ImageFormat;
compression: string;
noImageProcessing: boolean;
customWidth: string;
@@ -30,7 +35,7 @@ export type BatchUpscaylPayload = {
outputPath: string;
model: string;
gpuId: string;
saveImageAs: string;
saveImageAs: ImageFormat;
scale: string;
compression: string;
noImageProcessing: boolean;