mirror of
https://github.com/upscayl/upscayl.git
synced 2026-04-03 03:00:13 -04:00
Refactor code and remove config vars to fix ASCII path decoding
This commit is contained in:
5
common/decode-path.ts
Normal file
5
common/decode-path.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import path from "path";
|
||||
|
||||
export default function decodePath(filePath: string): string {
|
||||
return path.normalize(decodeURIComponent(filePath));
|
||||
}
|
||||
15
common/get-directory-from-path.ts
Normal file
15
common/get-directory-from-path.ts
Normal 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
8
common/get-file-name.ts
Normal 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
24
common/sanitize-path.ts
Normal 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;
|
||||
}
|
||||
13
common/types/types.d.ts
vendored
13
common/types/types.d.ts
vendored
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user