Skip to content

Commit

Permalink
Make createThumbnailer async
Browse files Browse the repository at this point in the history
  • Loading branch information
polyfloyd committed Jun 30, 2024
1 parent c390b6e commit 23db62b
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions lib/thumbnailer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,35 @@ function createReadStream(filePath) {
}

module.exports = function createThumbnailer(thumbnailFolder, pictureFolder, {width, height, quality}) {
return function getThumbnail(date, filename) {
return async function getThumbnail(date, filename) {
let thumbnailPath = path.join(thumbnailFolder, date, filename);

return Promise.try(() => {
return createReadStream(thumbnailPath);
}).catch({code: "ENOENT"}, (err) => {
/* Thumbnail does not exist yet, we need to create one. */

return Promise.try(() => {
return Promise.try(() => {
return fs.mkdirAsync(path.join(thumbnailFolder, date));
}).catch({code: "EEXIST"}, (err) => {
/* Ignore this type of error, it just means that the destination folder already exists. */
});
}).then(() => {
let resizer = gm(path.join(pictureFolder, date, filename)).noProfile().resize(width, height).quality(quality);
Promise.promisifyAll(resizer); // HACK
return resizer.writeAsync(thumbnailPath);
}).then(() => {
return createReadStream(thumbnailPath);
}).catch((err) => (/Command failed:/.test(err.message)), (err) => {
/* TODO: Chained errors */
throw new ThumbnailError(err.message);
});
});
}
try {
return await createReadStream(thumbnailPath);
} catch (err) {
if (err.code != 'ENOENT') {
throw err
}
}

/* Thumbnail does not exist yet, we need to create one. */

try {
await fs.mkdirAsync(path.join(thumbnailFolder, date));
} catch (err) {
/* Ignore this type of error, it just means that the destination folder already exists. */
if (err.code != 'EEXIST') {
throw err
}
}

let resizer = gm(path.join(pictureFolder, date, filename)).
noProfile().
resize(width, height).
quality(quality);
Promise.promisifyAll(resizer); // HACK
await resizer.writeAsync(thumbnailPath);

return await createReadStream(thumbnailPath);
};
};

0 comments on commit 23db62b

Please sign in to comment.