Skip to content

Commit 7233e6b

Browse files
author
chenyumic
authored
Update functions/imagemagick sample (#580)
* Update index.js * Update package.json * Lint * Update index.test.js
1 parent 3cac032 commit 7233e6b

2 files changed

Lines changed: 33 additions & 13 deletions

File tree

functions/imagemagick/index.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ const exec = require('child_process').exec;
2020
const fs = require('fs');
2121
const path = require('path');
2222
const storage = require('@google-cloud/storage')();
23-
const vision = require('@google-cloud/vision')();
23+
const vision = require('@google-cloud/vision').v1p1beta1;
24+
25+
const client = new vision.ImageAnnotatorClient();
2426
// [END functions_imagemagick_setup]
2527

2628
// [START functions_imagemagick_analyze]
@@ -38,18 +40,22 @@ exports.blurOffensiveImages = (event) => {
3840
}
3941

4042
const file = storage.bucket(object.bucket).file(object.name);
43+
const filePath = `gs://${object.bucket}/${object.name}`;
4144

4245
console.log(`Analyzing ${file.name}.`);
4346

44-
return vision.detectSafeSearch(file)
47+
return client.safeSearchDetection(filePath)
4548
.catch((err) => {
4649
console.error(`Failed to analyze ${file.name}.`, err);
4750
return Promise.reject(err);
4851
})
49-
.then(([safeSearch]) => {
50-
if (safeSearch.adult || safeSearch.violence) {
52+
.then(([result]) => {
53+
const detections = result.safeSearchAnnotation;
54+
55+
if (detections.adult === 'VERY_LIKELY' ||
56+
detections.violence === 'VERY_LIKELY') {
5157
console.log(`The image ${file.name} has been detected as inappropriate.`);
52-
return blurImage(file, safeSearch);
58+
return blurImage(file);
5359
} else {
5460
console.log(`The image ${file.name} has been detected as OK.`);
5561
}

functions/imagemagick/test/index.test.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,21 @@ const proxyquire = require(`proxyquire`).noCallThru();
1919
const sinon = require(`sinon`);
2020
const test = require(`ava`);
2121
const tools = require(`@google-cloud/nodejs-repo-tools`);
22+
const vision = require('@google-cloud/vision').v1p1beta1;
2223

2324
const bucketName = `my-bucket`;
2425
const filename = `image.jpg`;
2526

27+
var VisionStub = sinon.stub(vision, 'ImageAnnotatorClient');
28+
VisionStub.returns({
29+
safeSearchDetection: sinon.stub().returns(Promise.resolve([{
30+
safeSearchAnnotation: {
31+
adult: 'VERY_LIKELY',
32+
violence: 'VERY_LIKELY'
33+
}
34+
}]))
35+
});
36+
2637
function getSample () {
2738
const file = {
2839
getMetadata: sinon.stub().returns(Promise.resolve([{}])),
@@ -39,11 +50,7 @@ function getSample () {
3950
const storageMock = {
4051
bucket: sinon.stub().returns(bucket)
4152
};
42-
const visionMock = {
43-
detectSafeSearch: sinon.stub().returns(Promise.resolve([{ violence: true }]))
44-
};
4553
const StorageMock = sinon.stub().returns(storageMock);
46-
const VisionMock = sinon.stub().returns(visionMock);
4754
const childProcessMock = {
4855
exec: sinon.stub().yields()
4956
};
@@ -53,7 +60,6 @@ function getSample () {
5360

5461
return {
5562
program: proxyquire(`../`, {
56-
'@google-cloud/vision': VisionMock,
5763
'@google-cloud/storage': StorageMock,
5864
'child_process': childProcessMock,
5965
'fs': fsMock
@@ -63,8 +69,7 @@ function getSample () {
6369
childProcess: childProcessMock,
6470
storage: storageMock,
6571
bucket,
66-
file,
67-
vision: visionMock
72+
file
6873
}
6974
};
7075
}
@@ -96,8 +101,17 @@ test.serial(`blurOffensiveImages blurs images`, async (t) => {
96101
});
97102

98103
test.serial(`blurOffensiveImages ignores safe images`, async (t) => {
104+
VisionStub.restore();
105+
VisionStub = sinon.stub(vision, 'ImageAnnotatorClient');
106+
VisionStub.returns({
107+
safeSearchDetection: sinon.stub().returns(Promise.resolve([{
108+
safeSearchAnnotation: {
109+
adult: 'VERY_UNLIKELY',
110+
violence: 'VERY_UNLIKELY'
111+
}
112+
}]))
113+
});
99114
const sample = getSample();
100-
sample.mocks.vision.detectSafeSearch = sinon.stub().returns(Promise.resolve([{}]));
101115
await sample.program.blurOffensiveImages({ data: { bucket: bucketName, name: filename } });
102116
t.is(console.log.callCount, 2);
103117
t.deepEqual(console.log.getCall(0).args, [`Analyzing ${sample.mocks.file.name}.`]);

0 commit comments

Comments
 (0)