From fb7728077e0f166f86cf8b83f332068418efaa9a Mon Sep 17 00:00:00 2001 From: Nayam Amarshe <25067102+NayamAmarshe@users.noreply.github.com> Date: Mon, 15 Jan 2024 15:34:11 +0530 Subject: [PATCH] Add removeFileExtension utility function --- electron/commands/batch-upscayl.ts | 11 +++++++---- electron/commands/image-upscayl.ts | 19 ++++++++++++------- electron/utils/remove-file-extension.ts | 8 ++++++++ renderer/components/settings-tab/index.tsx | 6 ++++++ .../upscayl-tab/config/LeftPaneImageSteps.tsx | 15 +++++++++++++-- 5 files changed, 46 insertions(+), 13 deletions(-) create mode 100644 electron/utils/remove-file-extension.ts diff --git a/electron/commands/batch-upscayl.ts b/electron/commands/batch-upscayl.ts index 0b28321..f1aa5e4 100644 --- a/electron/commands/batch-upscayl.ts +++ b/electron/commands/batch-upscayl.ts @@ -22,6 +22,7 @@ import DEFAULT_MODELS from "../constants/models"; import { BatchUpscaylPayload } from "../../common/types/types"; import { ImageFormat } from "../utils/types"; import getModelScale from "../../common/check-model-scale"; +import removeFileExtension from "../utils/remove-file-extension"; const batchUpscayl = async (event, payload: BatchUpscaylPayload) => { const mainWindow = getMainWindow(); @@ -129,11 +130,11 @@ const batchUpscayl = async (event, payload: BatchUpscaylPayload) => { const files = fs.readdirSync(inputDir); try { files.forEach(async (file) => { - console.log("Filename: ", file.slice(0, -3)); + console.log("Filename: ", removeFileExtension(file)); await convertAndScale( inputDir + slash + file, - outputDir + slash + file.slice(0, -3) + "png", - outputDir + slash + file.slice(0, -3) + saveImageAs, + `${outputDir}${slash}${removeFileExtension(file)}.png`, + `${outputDir}/${removeFileExtension(file)}.${saveImageAs}`, desiredScale, saveImageAs, onError @@ -141,7 +142,9 @@ const batchUpscayl = async (event, payload: BatchUpscaylPayload) => { // Remove the png file (default) if the saveImageAs is not png if (saveImageAs !== "png") { logit("Removing output PNG"); - fs.unlinkSync(outputDir + slash + file.slice(0, -3) + "png"); + fs.unlinkSync( + `${outputDir}${slash}${removeFileExtension(file)}.png` + ); } }); mainWindow.webContents.send(COMMAND.FOLDER_UPSCAYL_DONE, outputDir); diff --git a/electron/commands/image-upscayl.ts b/electron/commands/image-upscayl.ts index 667b5d1..c8cc7d2 100644 --- a/electron/commands/image-upscayl.ts +++ b/electron/commands/image-upscayl.ts @@ -25,6 +25,7 @@ import { getMainWindow } from "../main-window"; import { ImageUpscaylPayload } from "../../common/types/types"; import { ImageFormat } from "../utils/types"; import getModelScale from "../../common/check-model-scale"; +import removeFileExtension from "../utils/remove-file-extension"; const imageUpscayl = async (event, payload: ImageUpscaylPayload) => { const mainWindow = getMainWindow(); @@ -104,7 +105,7 @@ const imageUpscayl = async (event, payload: ImageUpscaylPayload) => { getSingleImageArguments( inputDir, fullfileName, - outFile.slice(0, -3) + "png", + removeFileExtension(outFile) + ".png", isDefaultModel ? modelsPath : customModelsFolderPath ?? modelsPath, model, initialScale, @@ -165,19 +166,23 @@ const imageUpscayl = async (event, payload: ImageUpscaylPayload) => { try { await convertAndScale( inputDir + slash + fullfileName, - outFile.slice(0, -3) + "png", + removeFileExtension(outFile) + ".png", outFile, desiredScale, saveImageAs, onError ); // Remove the png file (default) if the saveImageAs is not png - fs.access(outFile.slice(0, -3) + "png", fs.constants.F_OK, (err) => { - if (!err && saveImageAs !== "png") { - logit("🗑 Removing png file"); - fs.unlinkSync(outFile.slice(0, -3) + "png"); + fs.access( + removeFileExtension(outFile) + ".png", + fs.constants.F_OK, + (err) => { + if (!err && saveImageAs !== "png") { + logit("🗑 Removing png file"); + fs.unlinkSync(removeFileExtension(outFile) + ".png"); + } } - }); + ); mainWindow.setProgressBar(-1); mainWindow.webContents.send( COMMAND.UPSCAYL_DONE, diff --git a/electron/utils/remove-file-extension.ts b/electron/utils/remove-file-extension.ts new file mode 100644 index 0000000..660450d --- /dev/null +++ b/electron/utils/remove-file-extension.ts @@ -0,0 +1,8 @@ +/** + * Returns the filename without the extension. + * @param filename The filename to remove the extension from. + * @returns The filename without the extension. + */ +export default function removeFileExtension(filename: string): string { + return filename.replace(/\.[^/.]+$/, ""); +} diff --git a/renderer/components/settings-tab/index.tsx b/renderer/components/settings-tab/index.tsx index 9fd0170..266489a 100644 --- a/renderer/components/settings-tab/index.tsx +++ b/renderer/components/settings-tab/index.tsx @@ -175,6 +175,12 @@ function SettingsTab({ target="_blank"> Read Wiki Guide + + Email Developer + {!featureFlags.APP_STORE_BUILD && } diff --git a/renderer/components/upscayl-tab/config/LeftPaneImageSteps.tsx b/renderer/components/upscayl-tab/config/LeftPaneImageSteps.tsx index a060e30..2e700d5 100644 --- a/renderer/components/upscayl-tab/config/LeftPaneImageSteps.tsx +++ b/renderer/components/upscayl-tab/config/LeftPaneImageSteps.tsx @@ -221,10 +221,10 @@ function LeftPaneImageSteps({ Double Upscayl

)} @@ -237,6 +237,17 @@ function LeftPaneImageSteps({ data-tooltip-id="tooltip">
Step 3 + {featureFlags.APP_STORE_BUILD && ( + + )} {!outputPath && featureFlags.APP_STORE_BUILD && (