Advertisement
Guest User

Untitled

a guest
Apr 4th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. exports.blurOffensiveImages = functions.storage.object().onChange(event => {
  2.     const object = event.data;
  3.     console.log(event)
  4.  
  5.     // Exit if this is a move or deletion event.
  6.     if (object.resourceState === 'not_exists') {
  7.       return -1
  8.     }
  9.  
  10.     if (event.data.metadata.isModerateOperation) {
  11.         console.log("Side-effect")
  12.         return -1
  13.     }
  14.  
  15.     // Check the image content using the Cloud Vision API.
  16.     return visionClient.safeSearchDetection(`gs://happyin-8708e.appspot.com/${object.name}`).then(data => {
  17.       const { adult, violence } = data[0].safeSearchAnnotation;
  18.       console.log("Flags: ", adult, violence)
  19.  
  20.       const _flags = ["VERY_UNLIKELY", "UNLIKELY", "POSSIBLE", "LIKELY", "VERY_LIKELY"]
  21.  
  22.       if (_flags.includes(adult) || _flags.includes(violence)) {
  23.          let _moderatedImage = admin.storage().bucket().file('/assets/forbidden-photo.jpg')
  24.          console.log("Copying to: " + object.name)
  25.          return _moderatedImage.copy({
  26.             destination: `gs://happyin-8708e.appspot.com/${object.name}`,
  27.             metadata: {
  28.                 isModerateOperation: true
  29.             }
  30.         })
  31.       }
  32.       else
  33.         return true
  34.     })
  35. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement