diff --git a/firebase-config.html b/firebase-config.html index 3a41290..aa66d07 100644 --- a/firebase-config.html +++ b/firebase-config.html @@ -5,6 +5,7 @@ name: {value:"", required:true}, cred: {value:"", required:true}, dburl: {value:"", required:true}, + bucket: {value:"", required: false} }, label: function() { return this.name; @@ -25,4 +26,8 @@ +
+ + +
\ No newline at end of file diff --git a/firebase-config.js b/firebase-config.js index 102d859..48cdc72 100644 --- a/firebase-config.js +++ b/firebase-config.js @@ -1,25 +1,42 @@ const _admin = require('firebase-admin') +const {Storage} = require('@google-cloud/storage'); + + let init = false +let s module.exports = function(RED) { function FirebaseConfigNode(n) { RED.nodes.createNode(this,n); + //------------------------------ this.cred = n.cred this.dburl = n.dburl - this.sturl = n.sturl + this.bucket = n.bucket + //------------------------------ this.admin = _admin + this.storage = s if(!init){ console.log('setting admin....') - init = true this.credentials = JSON.parse(this.cred); - this.dburl = this.dburl - this.sturl = this.sturl + let credobj = _admin.credential.cert(this.credentials) + //console.dir(credobj) + //process.env.GOOGLE_APPLICATION_CREDENTIALS = 'creds.json' + init = true + console.log('*** parsed firebase credentials: '+this.credentials.type+', project-id: '+this.credentials.project_id) _admin.initializeApp({ - credential: _admin.credential.cert(this.credentials), - databaseURL: this.dburl, - storageBucket: this.sturl + credential: credobj, + databaseURL: this.dburl }); + console.log('setting storage....') + + s = new Storage({ + //projectId: 'something-2e584', + //email: 'firebase-adminsdk-d1xiy@something-2e584.iam.gserviceaccount.com', + credentials: this.credentials + }) + + //s = new Storage() } } RED.nodes.registerType("firebase-config", FirebaseConfigNode); diff --git a/package.json b/package.json index eaa3692..2c8a3ef 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-red-contrib-firebase-admin", - "version": "1.0.5", + "version": "1.0.6", "description": "A node-red module that wraps the server-side admin SDK of firebase, firestore, et.c.", "main": "index.js", "scripts": { @@ -31,10 +31,12 @@ "firestore-query": "firestore-query.js", "firebase-config": "firebase-config.js", "flow-to-rtdb": "flow-to-rtdb.js", - "rtdb-to-flow": "rtdb-to-flow.js" + "rtdb-to-flow": "rtdb-to-flow.js", + "storage-read": "storage-read.js" } }, "dependencies": { + "@google-cloud/storage": "^3.0.2", "@node-red/runtime": "^0.20.6", "firebase-admin": "^8.0.0" } diff --git a/storage-read.html b/storage-read.html new file mode 100644 index 0000000..fcc45f8 --- /dev/null +++ b/storage-read.html @@ -0,0 +1,43 @@ + + + + + \ No newline at end of file diff --git a/storage-read.js b/storage-read.js new file mode 100644 index 0000000..1dd2403 --- /dev/null +++ b/storage-read.js @@ -0,0 +1,58 @@ + + +let oldpath + +module.exports = function(RED) { + + function FirebaseAdmin(config) { + RED.nodes.createNode(this, config); + var node = this; + + + if(config.cred){ + let c = RED.nodes.getNode(config.cred) + this.admin = c.admin + this.storage = c.storage + this.bucket = config.bucket || c.bucket + this.path = config.path + } + + console.log('------------------------------- rtdg-get config') + console.dir(config) + + //console.log('configuring storage-read to listen for messages') + node.on('input', function(msg) { + if(msg && msg.payload){ + let path = msg.payload.path || this.path + let bucket = msg.payload.bucket || this.bucket + console.log('storage-read reading from bucket "'+bucket+'" path "'+path+'"') + + this.storage.getBuckets().then((results)=>{ + const [buckets] = results; + + console.log('Buckets:'); + buckets.forEach(bucket => { + console.log(bucket.name); + }); + + + this.storage + .bucket(bucket) + .file(path).download().then((file)=>{ + console.log('storage-read got file') + //console.dir(file) + node.send({payload:file}) + + }) + + + }) + + + } + }.bind(this)); + + + } + RED.nodes.registerType("storage-read", FirebaseAdmin); +} \ No newline at end of file