Skip to content

Commit

Permalink
Use library in GCF ImageMagick tutorial (#741)
Browse files Browse the repository at this point in the history
* Use library in GCF ImageMagick tutorial

* Fix lint
  • Loading branch information
Ace Nassri authored Sep 25, 2018
1 parent 5e4a52d commit 6189438
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
14 changes: 7 additions & 7 deletions functions/imagemagick/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')();
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion functions/imagemagick/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
14 changes: 9 additions & 5 deletions functions/imagemagick/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,26 @@ 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()
};

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
Expand Down

0 comments on commit 6189438

Please sign in to comment.