Skip to content

Commit

Permalink
Fix temp folder delete bug
Browse files Browse the repository at this point in the history
  • Loading branch information
NayamAmarshe committed Nov 26, 2023
1 parent 0052049 commit c77e0f6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
20 changes: 15 additions & 5 deletions electron/commands/batch-upscayl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fs from "fs";
import fs, { rmdir } from "fs";
import { getMainWindow } from "../main-window";
import {
childProcesses,
Expand Down Expand Up @@ -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 });
Expand Down Expand Up @@ -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) => {
Expand All @@ -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();
Expand All @@ -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();
Expand Down
9 changes: 7 additions & 2 deletions electron/utils/convert-and-scale.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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) {
Expand Down

0 comments on commit c77e0f6

Please sign in to comment.