1616
1717// [START functions_imagemagick_setup]
1818const functions = require ( '@google-cloud/functions-framework' ) ;
19- const gm = require ( 'gm' ) . subClass ( { imageMagick : true } ) ;
19+ const sharp = require ( 'sharp' ) ;
2020const fs = require ( 'fs' ) . promises ;
2121const path = require ( 'path' ) ;
2222const vision = require ( '@google-cloud/vision' ) ;
@@ -34,6 +34,14 @@ functions.cloudEvent('blurOffensiveImages', async cloudEvent => {
3434 // This event represents the triggering Cloud Storage object.
3535 const bucket = cloudEvent . data . bucket ;
3636 const name = cloudEvent . data . name ;
37+
38+ if ( bucket === BLURRED_BUCKET_NAME ) {
39+ console . log (
40+ 'Event triggered by the blurred bucket; skip to avoid recursion'
41+ ) ;
42+ return ;
43+ }
44+
3745 const file = storage . bucket ( bucket ) . file ( name ) ;
3846 const filePath = `gs://${ bucket } /${ name } ` ;
3947
@@ -61,9 +69,10 @@ functions.cloudEvent('blurOffensiveImages', async cloudEvent => {
6169// [END functions_imagemagick_analyze]
6270
6371// [START functions_imagemagick_blur]
64- // Blurs the given file using ImageMagick , and uploads it to another bucket.
72+ // Blurs the given file using sharp , and uploads it to another bucket.
6573const blurImage = async ( file , blurredBucketName ) => {
6674 const tempLocalPath = `/tmp/${ path . parse ( file . name ) . base } ` ;
75+ const tempLocalBlurredPath = `/tmp/blurred-${ path . parse ( file . name ) . base } ` ;
6776
6877 // Download file from bucket.
6978 try {
@@ -74,33 +83,31 @@ const blurImage = async (file, blurredBucketName) => {
7483 throw new Error ( `File download failed: ${ err } ` ) ;
7584 }
7685
77- await new Promise ( ( resolve , reject ) => {
78- gm ( tempLocalPath )
79- . blur ( 0 , 16 )
80- . write ( tempLocalPath , ( err , stdout ) => {
81- if ( err ) {
82- console . error ( 'Failed to blur image.' , err ) ;
83- reject ( err ) ;
84- } else {
85- console . log ( `Blurred image: ${ file . name } ` ) ;
86- resolve ( stdout ) ;
87- }
88- } ) ;
89- } ) ;
86+ try {
87+ await sharp ( tempLocalPath ) . blur ( 16 ) . toFile ( tempLocalBlurredPath ) ;
88+
89+ console . log ( `Blurred image: ${ file . name } ` ) ;
90+ } catch ( err ) {
91+ console . error ( 'Failed to blur image.' , err ) ;
92+ throw err ;
93+ }
9094
9195 // Upload result to a different bucket, to avoid re-triggering this function.
9296 const blurredBucket = storage . bucket ( blurredBucketName ) ;
9397
9498 // Upload the Blurred image back into the bucket.
9599 const gcsPath = `gs://${ blurredBucketName } /${ file . name } ` ;
96100 try {
97- await blurredBucket . upload ( tempLocalPath , { destination : file . name } ) ;
101+ await blurredBucket . upload ( tempLocalBlurredPath , { destination : file . name } ) ;
98102 console . log ( `Uploaded blurred image to: ${ gcsPath } ` ) ;
99103 } catch ( err ) {
100104 throw new Error ( `Unable to upload blurred image to ${ gcsPath } : ${ err } ` ) ;
105+ } finally {
106+ // Delete the temporary file.
107+ await Promise . allSettled ( [
108+ fs . unlink ( tempLocalPath ) ,
109+ fs . unlink ( tempLocalBlurredPath ) ,
110+ ] ) ;
101111 }
102-
103- // Delete the temporary file.
104- return fs . unlink ( tempLocalPath ) ;
105112} ;
106113// [END functions_imagemagick_blur]
0 commit comments