From a0b94bba4e32e9af2e2cb12a45e0fe02183bf219 Mon Sep 17 00:00:00 2001 From: j433866 Date: Wed, 20 Feb 2019 11:26:39 +0000 Subject: [PATCH] Change run() functions to be async --- src/core/operations/DitherImage.mjs | 23 ++++----------- src/core/operations/ResizeImage.mjs | 45 +++++++++-------------------- src/core/operations/RotateImage.mjs | 24 ++++----------- 3 files changed, 24 insertions(+), 68 deletions(-) diff --git a/src/core/operations/DitherImage.mjs b/src/core/operations/DitherImage.mjs index a3fd4974..aff95a3c 100644 --- a/src/core/operations/DitherImage.mjs +++ b/src/core/operations/DitherImage.mjs @@ -36,27 +36,14 @@ class DitherImage extends Operation { * @param {Object[]} args * @returns {byteArray} */ - run(input, args) { + async run(input, args) { const type = Magic.magicFileType(input); if (type && type.mime.indexOf("image") === 0){ - return new Promise((resolve, reject) => { - jimp.read(Buffer.from(input)) - .then(image => { - image - .dither565() - .getBuffer(jimp.AUTO, (error, result) => { - if (error){ - reject(new OperationError("Error getting the new image buffer")); - } else { - resolve([...result]); - } - }); - }) - .catch(err => { - reject(new OperationError("Error applying a dither effect to the image.")); - }); - }); + const image = await jimp.read(Buffer.from(input)); + image.dither565(); + const imageBuffer = await image.getBufferAsync(jimp.AUTO); + return [...imageBuffer]; } else { throw new OperationError("Invalid file type."); } diff --git a/src/core/operations/ResizeImage.mjs b/src/core/operations/ResizeImage.mjs index 3e177c16..115d8c65 100644 --- a/src/core/operations/ResizeImage.mjs +++ b/src/core/operations/ResizeImage.mjs @@ -57,7 +57,7 @@ class ResizeImage extends Operation { * @param {Object[]} args * @returns {byteArray} */ - run(input, args) { + async run(input, args) { let width = args[0], height = args[1]; const unit = args[2], @@ -67,37 +67,20 @@ class ResizeImage extends Operation { if (!type || type.mime.indexOf("image") !== 0){ throw new OperationError("Invalid file type."); } + const image = await jimp.read(Buffer.from(input)); - return new Promise((resolve, reject) => { - jimp.read(Buffer.from(input)) - .then(image => { - if (unit === "Percent") { - width = image.getWidth() * (width / 100); - height = image.getHeight() * (height / 100); - } - if (aspect) { - image - .scaleToFit(width, height) - .getBuffer(jimp.AUTO, (error, result) => { - if (error){ - reject(new OperationError("Error scaling the image.")); - } else { - resolve([...result]); - } - }); - } else { - image - .resize(width, height) - .getBuffer(jimp.AUTO, (error, result) => { - if (error){ - reject(new OperationError("Error scaling the image.")); - } else { - resolve([...result]); - } - }); - } - }); - }); + if (unit === "Percent") { + width = image.getWidth() * (width / 100); + height = image.getHeight() * (height / 100); + } + if (aspect) { + image.scaleToFit(width, height); + } else { + image.resize(width, height); + } + + const imageBuffer = await image.getBufferAsync(jimp.AUTO); + return [...imageBuffer]; } /** diff --git a/src/core/operations/RotateImage.mjs b/src/core/operations/RotateImage.mjs index 7f01b034..1bab6c98 100644 --- a/src/core/operations/RotateImage.mjs +++ b/src/core/operations/RotateImage.mjs @@ -42,28 +42,15 @@ class RotateImage extends Operation { * @param {Object[]} args * @returns {byteArray} */ - run(input, args) { + async run(input, args) { const [degrees] = args; const type = Magic.magicFileType(input); if (type && type.mime.indexOf("image") === 0){ - return new Promise((resolve, reject) => { - jimp.read(Buffer.from(input)) - .then(image => { - image - .rotate(degrees) - .getBuffer(jimp.AUTO, (error, result) => { - if (error){ - reject(new OperationError("Error getting the new image buffer")); - } else { - resolve([...result]); - } - }); - }) - .catch(err => { - reject(new OperationError("Error reading the input image.")); - }); - }); + const image = await jimp.read(Buffer.from(input)); + image.rotate(degrees); + const imageBuffer = await image.getBufferAsync(jimp.AUTO); + return [...imageBuffer]; } else { throw new OperationError("Invalid file type."); } @@ -71,7 +58,6 @@ class RotateImage extends Operation { /** * Displays the rotated image using HTML for web apps - * * @param {byteArray} data * @returns {html} */