Skip to content

πŸ†™ Upscayl - #1 Free and Open Source AI Image Upscaler for Linux, MacOS and Windows.

Fix temp folder delete bug

Nayam Amarshec77e0f6c9254
diff --git a/electron/commands/batch-upscayl.ts b/electron/commands/batch-upscayl.ts
index efce465..8e25419 100644
--- a/electron/commands/batch-upscayl.ts
+++ b/electron/commands/batch-upscayl.ts
@@ -1,4 +1,4 @@
-import fs from "fs";
+import fs, { rmdir } from "fs";
import { getMainWindow } from "../main-window";
import {
childProcesses,
@@ -61,7 +61,6 @@ const batchUpscayl = async (event, payload: BatchUpscaylPayload) => {
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
-
// Create a folder in the output directory to store the original images
if (!fs.existsSync(tempDirectory)) {
fs.mkdirSync(tempDirectory, { recursive: true });
@@ -140,7 +139,7 @@ const batchUpscayl = async (event, payload: BatchUpscaylPayload) => {
mainWindow.webContents.send(COMMAND.FOLDER_UPSCAYL_DONE, outputDir);
return;
}
- // Get number of files in output folder
+
const files = fs.readdirSync(tempDirectory);
try {
files.forEach(async (file) => {
@@ -153,13 +152,25 @@ const batchUpscayl = async (event, payload: BatchUpscaylPayload) => {
saveImageAs,
onError
);
+ });
+ files.forEach(async (file) => {
// Remove the png file (default) if the saveImageAs is not png
if (saveImageAs !== "png") {
fs.unlinkSync(outputDir + slash + file.slice(0, -3) + "png");
}
});
mainWindow.webContents.send(COMMAND.FOLDER_UPSCAYL_DONE, outputDir);
- fs.rmdirSync(tempDirectory, { recursive: true });
+ rmdir(
+ tempDirectory,
+ {
+ recursive: true,
+ },
+ (err) => {
+ if (err) {
+ logit("🚫 Error deleting temp folder", err);
+ }
+ }
+ );
} catch (error) {
logit("❌ Error processing (scaling and converting) the image.", error);
upscayl.kill();
@@ -169,7 +180,6 @@ const batchUpscayl = async (event, payload: BatchUpscaylPayload) => {
"Error processing (scaling and converting) the image. Please report this error on Upscayl GitHub Issues page.\n" +
error
);
- fs.rmdirSync(tempDirectory, { recursive: true });
}
} else {
upscayl.kill();
diff --git a/electron/utils/convert-and-scale.ts b/electron/utils/convert-and-scale.ts
index cf38044..e0aa94d 100644
--- a/electron/utils/convert-and-scale.ts
+++ b/electron/utils/convert-and-scale.ts
@@ -1,5 +1,5 @@
import fs from "fs";
-import sharp, { FormatEnum } from "sharp";
+import sharp, { FormatEnum, Metadata } from "sharp";
import logit from "./logit";
import { getMainWindow } from "../main-window";
import { compression } from "./config-variables";
@@ -17,8 +17,13 @@ const convertAndScale = async (
return;
}
const mainWindow = getMainWindow();
+ let originalImage: Metadata | undefined;
- const originalImage = await sharp(originalImagePath).metadata();
+ try {
+ originalImage = await sharp(originalImagePath).metadata();
+ } catch (error) {
+ logit("❌ Error with original Image: ", error);
+ }
fs.access(originalImagePath, fs.constants.F_OK, (err) => {
if (err) {