Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use library in GCF ImageMagick tutorial #741

Merged
merged 2 commits into from
Sep 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the parenthesis around tempLocalPath?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gm is a constructor - this specifies the target file path.

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