1515'use strict' ;
1616
1717// [START functions_imagemagick_setup]
18- const gm = require ( 'gm' ) . subClass ( { imageMagick : true } ) ;
18+ const sharp = require ( 'sharp' ) ;
1919const fs = require ( 'fs' ) . promises ;
2020const path = require ( 'path' ) ;
2121const vision = require ( '@google-cloud/vision' ) ;
@@ -32,6 +32,12 @@ const {BLURRED_BUCKET_NAME} = process.env;
3232exports . blurOffensiveImages = async event => {
3333 // This event represents the triggering Cloud Storage object.
3434 const object = event ;
35+ if ( object . bucket === BLURRED_BUCKET_NAME ) {
36+ console . log (
37+ 'Event triggered by the blurred bucket; skip to avoid recursion'
38+ ) ;
39+ return ;
40+ }
3541
3642 const file = storage . bucket ( object . bucket ) . file ( object . name ) ;
3743 const filePath = `gs://${ object . bucket } /${ object . name } ` ;
@@ -60,9 +66,10 @@ exports.blurOffensiveImages = async event => {
6066// [END functions_imagemagick_analyze]
6167
6268// [START functions_imagemagick_blur]
63- // Blurs the given file using ImageMagick , and uploads it to another bucket.
69+ // Blurs the given file using sharp , and uploads it to another bucket.
6470const blurImage = async ( file , blurredBucketName ) => {
6571 const tempLocalPath = `/tmp/${ path . parse ( file . name ) . base } ` ;
72+ const tempLocalBlurredPath = `/tmp/blurred-${ path . parse ( file . name ) . base } ` ;
6673
6774 // Download file from bucket.
6875 try {
@@ -72,34 +79,31 @@ const blurImage = async (file, blurredBucketName) => {
7279 } catch ( err ) {
7380 throw new Error ( `File download failed: ${ err } ` ) ;
7481 }
82+ try {
83+ await sharp ( tempLocalPath ) . blur ( 16 ) . toFile ( tempLocalBlurredPath ) ;
7584
76- await new Promise ( ( resolve , reject ) => {
77- gm ( tempLocalPath )
78- . blur ( 0 , 16 )
79- . write ( tempLocalPath , ( err , stdout ) => {
80- if ( err ) {
81- console . error ( 'Failed to blur image.' , err ) ;
82- reject ( err ) ;
83- } else {
84- console . log ( `Blurred image: ${ file . name } ` ) ;
85- resolve ( stdout ) ;
86- }
87- } ) ;
88- } ) ;
85+ console . log ( `Blurred image: ${ file . name } ` ) ;
86+ } catch ( err ) {
87+ console . error ( 'Failed to blur image.' , err ) ;
88+ throw err ;
89+ }
8990
9091 // Upload result to a different bucket, to avoid re-triggering this function.
9192 const blurredBucket = storage . bucket ( blurredBucketName ) ;
9293
9394 // Upload the Blurred image back into the bucket.
9495 const gcsPath = `gs://${ blurredBucketName } /${ file . name } ` ;
9596 try {
96- await blurredBucket . upload ( tempLocalPath , { destination : file . name } ) ;
97+ await blurredBucket . upload ( tempLocalBlurredPath , { destination : file . name } ) ;
9798 console . log ( `Uploaded blurred image to: ${ gcsPath } ` ) ;
9899 } catch ( err ) {
99100 throw new Error ( `Unable to upload blurred image to ${ gcsPath } : ${ err } ` ) ;
101+ } finally {
102+ // Delete the temporary file.
103+ await Promise . allSettled ( [
104+ fs . unlink ( tempLocalPath ) ,
105+ fs . unlink ( tempLocalBlurredPath ) ,
106+ ] ) ;
100107 }
101-
102- // Delete the temporary file.
103- return fs . unlink ( tempLocalPath ) ;
104108} ;
105109// [END functions_imagemagick_blur]
0 commit comments