@@ -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,7 +66,7 @@ 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 } ` ;
6672 const tempLocalBlurredPath = `/tmp/blurred-${ path . parse ( file . name ) . base } ` ;
@@ -92,10 +98,12 @@ const blurImage = async (file, blurredBucketName) => {
9298 console . log ( `Uploaded blurred image to: ${ gcsPath } ` ) ;
9399 } catch ( err ) {
94100 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+ ] ) ;
95107 }
96-
97- // Delete the temporary file.
98- await fs . unlink ( tempLocalPath ) ;
99- return await fs . unlink ( tempLocalBlurredPath ) ;
100108} ;
101109// [END functions_imagemagick_blur]
0 commit comments