diff --git a/lib/thumbnailer/index.js b/lib/thumbnailer/index.js index ccac246..8a99cb1 100644 --- a/lib/thumbnailer/index.js +++ b/lib/thumbnailer/index.js @@ -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); + }; };