From ec43c20f0c21a01b24db52cc9084c7df3798ea92 Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Fri, 14 Sep 2018 16:38:08 -0700 Subject: [PATCH 1/2] Use library in GCF ImageMagick tutorial --- functions/imagemagick/index.js | 14 +++++++------- functions/imagemagick/package.json | 3 ++- functions/imagemagick/test/index.test.js | 14 +++++++++----- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/functions/imagemagick/index.js b/functions/imagemagick/index.js index 751ff6ec21..470cd79a97 100644 --- a/functions/imagemagick/index.js +++ b/functions/imagemagick/index.js @@ -16,7 +16,7 @@ 'use strict'; // [START functions_imagemagick_setup] -const exec = require('child_process').exec; +const gm = require('gm').subClass({imageMagick: true}); const fs = require('fs'); const path = require('path'); const storage = require('@google-cloud/storage')(); @@ -66,20 +66,20 @@ exports.blurOffensiveImages = (event) => { // [START functions_imagemagick_blur] // Blurs the given file using ImageMagick. function blurImage (file) { - const tempLocalFilename = `/tmp/${path.parse(file.name).base}`; + const tempLocalPath = `/tmp/${path.parse(file.name).base}`; // Download file from bucket. - return file.download({ destination: tempLocalFilename }) + return file.download({ destination: tempLocalPath }) .catch((err) => { console.error('Failed to download file.', err); return Promise.reject(err); }) .then(() => { - console.log(`Image ${file.name} has been downloaded to ${tempLocalFilename}.`); + console.log(`Image ${file.name} has been downloaded to ${tempLocalPath}.`); // Blur the image using ImageMagick. return new Promise((resolve, reject) => { - exec(`convert ${tempLocalFilename} -channel RGBA -blur 0x24 ${tempLocalFilename}`, { stdio: 'ignore' }, (err, stdout) => { + gm(tempLocalPath).blur(16).write((tempLocalPath), (err, stdout) => { if (err) { console.error('Failed to blur image.', err); reject(err); @@ -93,7 +93,7 @@ function blurImage (file) { console.log(`Image ${file.name} has been blurred.`); // Upload the Blurred image back into the bucket. - return file.bucket.upload(tempLocalFilename, { destination: file.name }) + return file.bucket.upload(tempLocalPath, { destination: file.name }) .catch((err) => { console.error('Failed to upload blurred image.', err); return Promise.reject(err); @@ -104,7 +104,7 @@ function blurImage (file) { // Delete the temporary file. return new Promise((resolve, reject) => { - fs.unlink(tempLocalFilename, (err) => { + fs.unlink(tempLocalPath, (err) => { if (err) { reject(err); } else { diff --git a/functions/imagemagick/package.json b/functions/imagemagick/package.json index 3730317888..80975e0a24 100644 --- a/functions/imagemagick/package.json +++ b/functions/imagemagick/package.json @@ -18,7 +18,8 @@ }, "dependencies": { "@google-cloud/storage": "1.6.0", - "@google-cloud/vision": "0.16.0" + "@google-cloud/vision": "0.16.0", + "gm": "^1.23.1" }, "devDependencies": { "@google-cloud/nodejs-repo-tools": "2.2.1", diff --git a/functions/imagemagick/test/index.test.js b/functions/imagemagick/test/index.test.js index 0b48819b9f..a88433bf02 100644 --- a/functions/imagemagick/test/index.test.js +++ b/functions/imagemagick/test/index.test.js @@ -51,9 +51,13 @@ function getSample () { bucket: sinon.stub().returns(bucket) }; const StorageMock = sinon.stub().returns(storageMock); - const childProcessMock = { - exec: sinon.stub().yields() - }; + + const gmMock = sinon.stub().returns({ + blur: sinon.stub().returnsThis(), + write: sinon.stub().yields() + }); + gmMock.subClass = sinon.stub().returnsThis() + const fsMock = { unlink: sinon.stub().yields() }; @@ -61,12 +65,12 @@ function getSample () { return { program: proxyquire(`../`, { '@google-cloud/storage': StorageMock, - 'child_process': childProcessMock, + 'gm': gmMock, 'fs': fsMock }), mocks: { fs: fsMock, - childProcess: childProcessMock, + gm: gmMock, storage: storageMock, bucket, file From 5c0fb1b827172fc556b392d499522734ecc7f0fb Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Mon, 17 Sep 2018 14:45:50 -0700 Subject: [PATCH 2/2] Fix lint --- functions/imagemagick/test/index.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/functions/imagemagick/test/index.test.js b/functions/imagemagick/test/index.test.js index a88433bf02..abddf6a663 100644 --- a/functions/imagemagick/test/index.test.js +++ b/functions/imagemagick/test/index.test.js @@ -56,7 +56,7 @@ function getSample () { blur: sinon.stub().returnsThis(), write: sinon.stub().yields() }); - gmMock.subClass = sinon.stub().returnsThis() + gmMock.subClass = sinon.stub().returnsThis(); const fsMock = { unlink: sinon.stub().yields()