import * as functions from "firebase-functions" ; import vision from "@google-cloud/vision" ; import config from "./config" ; const admin = require ( "firebase-admin" ) ; admin . initializeApp ( ) ; const client = new vision . ImageAnnotatorClient ( ) ; const db = admin . firestore ( ) ; exports . extractText = functions . storage . object ( ) . onFinalize ( async ( object ) => { if ( ! object . name ?. toLowerCase ( ) . endsWith ( ".jpg" ) && ! object . name ?. toLowerCase ( ) . endsWith ( ".jpeg" ) && ! object . name ?. toLowerCase ( ) . endsWith ( ".png" ) ) { return ; } const bucket = admin . storage ( ) . bucket ( object . bucket ) ; const imageContents = await bucket . file ( object . name ) . download ( ) ; const imageBase64 = Buffer . from ( imageContents [ 0 ] ) . toString ( "base64" ) ; const request = { image : { content : imageBase64 , } , features : [ { type : "TEXT_DETECTION" , } , ] , } ; const results = await client . annotateImage ( request ) ; const extractedText = results ?. [ 0 ] ?. textAnnotations ?. [ 0 ] ?. description ; await db . collection ( config . collectionPath ) . doc ( object . name ) . create ( { file : "gs://" + object . bucket + "/" + object . name , text : extractedText , } ) ; } ) ;