0

I,m using moderate images solution trigger from google. I taked this solution from here. I ask some to upgrade for me this solution & here is code:

'use strict'
const gm = require('gm').subClass({imageMagick: true})
const functions = require('firebase-functions')
const admin = require('firebase-admin')
admin.initializeApp()
const Vision = require('@google-cloud/vision')
const vision = new Vision.ImageAnnotatorClient()
const spawn = require('child-process-promise').spawn
const path = require('path')
const fs = require('fs')
const { Storage } = require('@google-cloud/storage')

const gcs = new Storage({
  projectId: xxxxxxxxxxx,
})

exports.blurOffensiveImages = functions.storage
  .object()
  .onFinalize(async (object) => {
    const file = gcs.bucket(object.bucket).file(object.name)
    const filePath = `gs://${object.bucket}/${object.name}`

    console.log(`Analyzing ${file.name}.`)

    try {
      const [result] = await vision.safeSearchDetection(filePath)
      const detections = result.safeSearchAnnotation || {}

      if (
        detections.adult === 'VERY_LIKELY' ||
        detections.violence === 'VERY_LIKELY'
      ) {
        console.log(`Detected ${file.name} as inappropriate.`)
        await blurImage(file, object.bucket, object.metadata)
        console.log('Deleted local file', file)
        return null
      } else {
        console.log(`Detected ${file.name} as OK.`)
      }
    } catch (err) {
      console.error(`Failed to analyze ${file.name}.`, err)
      throw err
    }
  })

async function blurImage(file, bucketName, metadata) {
  const tempLocalPath = `/tmp/${path.parse(file.name).base}`
  const bucket = gcs.bucket(bucketName)

  await file.download({ destination: tempLocalPath })

  console.log('The file has been downloaded to', tempLocalPath)
  // Blur the image using ImageMagick.
await new Promise((resolve, reject) => {
    gm(tempLocalPath)
      .blur(0, 20)
      .write(tempLocalPath, (err, stdout) => {
        if (err) {
          console.error('Failed to blur image.', err);
          reject(err);
        } else {
          console.log(`Blurred image: ${file.name}`);
          resolve(stdout);
        }
      });
  });

  console.log('Blurred image created at', tempLocalPath)

  await bucket.upload(tempLocalPath, {
    destination: file.name,
    metadata: { metadata: metadata },
  })

  console.log('Blurred image uploaded to Storage at', file)
  return fs.unlink(tempLocalPath, (e) => { if (e) {console.log(e)}})
}
End it's worked perfect, with one bad issue. Sometimes when user sending list of photos i have "application/octet-stream" file type, but it should be "image/jpg" all media files at my project should be image/jpg. one user's publication with error in image data type It's looks like this trigger stuck when it executing. I made delay in uploading images in my project, but it's doesn't helps me. I tested - when i delete this trigger - all uploading photos is well & no issues at all. Help me fix it. P.S. want to say also, after uploading - image should have all data like original. (Destination, name etc.)
  • Can you please elaborate on your trigger getting stuck? Is there debug output or error messages? – MrTech Oct 05 '20 at 20:34
  • no at all((( Console clear( no errors at all( it's good trigger but maybe only for one image, & a i don't know at all what is resason & how can it be fixed for multiple image uploading – Pasha Fateev Oct 06 '20 at 12:23
  • If you want to upload multiple images, I have found a link here (https://stackoverflow.com/questions/41673499/how-to-upload-multiple-files-to-firebase). I think you can use the code here for your purpose. – MrTech Oct 07 '20 at 18:43

0 Answers0