Skip to content

Commit 40fad7a

Browse files
committed
Fix file not found error
1 parent 11bf779 commit 40fad7a

File tree

2 files changed

+33
-32
lines changed

2 files changed

+33
-32
lines changed

src/utils/processImage.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ export function sliceImage(
3434
cubic,
3535
filename,
3636
);
37-
return;
3837
}
3938
}
4039
});
@@ -48,30 +47,29 @@ export function sliceImage(
4847
let foundImage = false;
4948

5049
// Attempt to read the image with different extensions
51-
supportedFormats.forEach((ext) => {
50+
supportedFormats.forEach(async (ext, index) => {
5251
const fullFilename = filename + ext;
5352
if (!foundImage) {
54-
Jimp.read(fullFilename, (err, image) => {
55-
if (!foundImage && !err) {
56-
foundImage = true;
57-
continueSlicing(
58-
image,
59-
width,
60-
height,
61-
canvasWidth,
62-
canvasHeight,
63-
scale,
64-
cubic,
65-
fullFilename,
66-
);
67-
}
68-
});
53+
try {
54+
const image = await Jimp.read(fullFilename);
55+
foundImage = true;
56+
continueSlicing(
57+
image,
58+
width,
59+
height,
60+
canvasWidth,
61+
canvasHeight,
62+
scale,
63+
cubic,
64+
fullFilename,
65+
);
66+
} catch (err) {}
67+
68+
if (!foundImage && index === supportedFormats.length - 1) {
69+
console.error(`Could not find ${filename}`);
70+
}
6971
}
7072
});
71-
72-
if (foundImage === false) {
73-
throw new Error(`Could not find ${filename}`);
74-
}
7573
}
7674

7775
/**
@@ -87,6 +85,7 @@ function continueSlicing(
8785
cubic: boolean,
8886
inputFilename: string,
8987
): void {
88+
console.time('Done in');
9089
// If height is not specified, use width as height
9190
height = height || width;
9291

@@ -156,6 +155,7 @@ function continueSlicing(
156155
console.log(`Slice saved: ${outputFilename}`);
157156
}
158157
}
158+
console.timeEnd('Done in');
159159
}
160160

161161
// If used as a worker thread, get file name from message

utils/processImage.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ function sliceImage(filename, width, height, canvasWidth, canvasHeight, scale, c
1818
// Continue slicing if image is successfully read
1919
if (image) {
2020
continueSlicing(image, width, height, canvasWidth, canvasHeight, scale, cubic, filename);
21-
return;
2221
}
2322
}
2423
});
@@ -29,26 +28,27 @@ function sliceImage(filename, width, height, canvasWidth, canvasHeight, scale, c
2928
const supportedFormats = ['.png', '.gif', '.jpg', '.jpeg'];
3029
let foundImage = false;
3130
// Attempt to read the image with different extensions
32-
supportedFormats.forEach((ext) => {
31+
supportedFormats.forEach(async (ext, index) => {
3332
const fullFilename = filename + ext;
3433
if (!foundImage) {
35-
Jimp.read(fullFilename, (err, image) => {
36-
if (!foundImage && !err) {
37-
foundImage = true;
38-
continueSlicing(image, width, height, canvasWidth, canvasHeight, scale, cubic, fullFilename);
39-
}
40-
});
34+
try {
35+
const image = await Jimp.read(fullFilename);
36+
foundImage = true;
37+
continueSlicing(image, width, height, canvasWidth, canvasHeight, scale, cubic, fullFilename);
38+
}
39+
catch (err) { }
40+
if (!foundImage && index === supportedFormats.length - 1) {
41+
console.error(`Could not find ${filename}`);
42+
}
4143
}
4244
});
43-
if (foundImage === false) {
44-
throw new Error(`Could not find ${filename}`);
45-
}
4645
}
4746
exports.sliceImage = sliceImage;
4847
/**
4948
* Continue slicing the image into smaller segments
5049
*/
5150
function continueSlicing(image, width, height, canvasWidth, canvasHeight, scale, cubic, inputFilename) {
51+
console.time('Done in');
5252
// If height is not specified, use width as height
5353
height = height || width;
5454
const imageWidth = image.getWidth();
@@ -95,6 +95,7 @@ function continueSlicing(image, width, height, canvasWidth, canvasHeight, scale,
9595
console.log(`Slice saved: ${outputFilename}`);
9696
}
9797
}
98+
console.timeEnd('Done in');
9899
}
99100
// If used as a worker thread, get file name from message
100101
if (!worker_threads_1.isMainThread) {

0 commit comments

Comments
 (0)