From 7233e6b93db8e8f2c77b34ba1ed02a803828d8bb Mon Sep 17 00:00:00 2001 From: chenyumic Date: Tue, 3 Apr 2018 17:30:00 -0700 Subject: [PATCH] Update functions/imagemagick sample (#580) * Update index.js * Update package.json * Lint * Update index.test.js --- functions/imagemagick/index.js | 16 +++++++++---- functions/imagemagick/test/index.test.js | 30 +++++++++++++++++------- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/functions/imagemagick/index.js b/functions/imagemagick/index.js index 6fb561c9dc..751ff6ec21 100644 --- a/functions/imagemagick/index.js +++ b/functions/imagemagick/index.js @@ -20,7 +20,9 @@ const exec = require('child_process').exec; const fs = require('fs'); const path = require('path'); const storage = require('@google-cloud/storage')(); -const vision = require('@google-cloud/vision')(); +const vision = require('@google-cloud/vision').v1p1beta1; + +const client = new vision.ImageAnnotatorClient(); // [END functions_imagemagick_setup] // [START functions_imagemagick_analyze] @@ -38,18 +40,22 @@ exports.blurOffensiveImages = (event) => { } const file = storage.bucket(object.bucket).file(object.name); + const filePath = `gs://${object.bucket}/${object.name}`; console.log(`Analyzing ${file.name}.`); - return vision.detectSafeSearch(file) + return client.safeSearchDetection(filePath) .catch((err) => { console.error(`Failed to analyze ${file.name}.`, err); return Promise.reject(err); }) - .then(([safeSearch]) => { - if (safeSearch.adult || safeSearch.violence) { + .then(([result]) => { + const detections = result.safeSearchAnnotation; + + if (detections.adult === 'VERY_LIKELY' || + detections.violence === 'VERY_LIKELY') { console.log(`The image ${file.name} has been detected as inappropriate.`); - return blurImage(file, safeSearch); + return blurImage(file); } else { console.log(`The image ${file.name} has been detected as OK.`); } diff --git a/functions/imagemagick/test/index.test.js b/functions/imagemagick/test/index.test.js index 63445accc1..0b48819b9f 100644 --- a/functions/imagemagick/test/index.test.js +++ b/functions/imagemagick/test/index.test.js @@ -19,10 +19,21 @@ const proxyquire = require(`proxyquire`).noCallThru(); const sinon = require(`sinon`); const test = require(`ava`); const tools = require(`@google-cloud/nodejs-repo-tools`); +const vision = require('@google-cloud/vision').v1p1beta1; const bucketName = `my-bucket`; const filename = `image.jpg`; +var VisionStub = sinon.stub(vision, 'ImageAnnotatorClient'); +VisionStub.returns({ + safeSearchDetection: sinon.stub().returns(Promise.resolve([{ + safeSearchAnnotation: { + adult: 'VERY_LIKELY', + violence: 'VERY_LIKELY' + } + }])) +}); + function getSample () { const file = { getMetadata: sinon.stub().returns(Promise.resolve([{}])), @@ -39,11 +50,7 @@ function getSample () { const storageMock = { bucket: sinon.stub().returns(bucket) }; - const visionMock = { - detectSafeSearch: sinon.stub().returns(Promise.resolve([{ violence: true }])) - }; const StorageMock = sinon.stub().returns(storageMock); - const VisionMock = sinon.stub().returns(visionMock); const childProcessMock = { exec: sinon.stub().yields() }; @@ -53,7 +60,6 @@ function getSample () { return { program: proxyquire(`../`, { - '@google-cloud/vision': VisionMock, '@google-cloud/storage': StorageMock, 'child_process': childProcessMock, 'fs': fsMock @@ -63,8 +69,7 @@ function getSample () { childProcess: childProcessMock, storage: storageMock, bucket, - file, - vision: visionMock + file } }; } @@ -96,8 +101,17 @@ test.serial(`blurOffensiveImages blurs images`, async (t) => { }); test.serial(`blurOffensiveImages ignores safe images`, async (t) => { + VisionStub.restore(); + VisionStub = sinon.stub(vision, 'ImageAnnotatorClient'); + VisionStub.returns({ + safeSearchDetection: sinon.stub().returns(Promise.resolve([{ + safeSearchAnnotation: { + adult: 'VERY_UNLIKELY', + violence: 'VERY_UNLIKELY' + } + }])) + }); const sample = getSample(); - sample.mocks.vision.detectSafeSearch = sinon.stub().returns(Promise.resolve([{}])); await sample.program.blurOffensiveImages({ data: { bucket: bucketName, name: filename } }); t.is(console.log.callCount, 2); t.deepEqual(console.log.getCall(0).args, [`Analyzing ${sample.mocks.file.name}.`]);