refactord, tidied up, added icon for storage and a new node for writing buffers to storage files

This commit is contained in:
Peter Svensson
2019-07-02 20:43:04 +02:00
parent ae90a7c225
commit df5b5c8e82
26 changed files with 120 additions and 16 deletions

View File

@ -106,7 +106,24 @@ If the payload defines an optional bucket property, it will override the default
input: {"payload":{"bucket": "xyzzyz123.appspot.com", "path": "myFile.txt"}}
output: Buffer[] containing the binary file contents
npm publish .output: Buffer object containing the binary file contents. Can easily be converted to a string by calling toString() on the Buffer.
## storage-write
Writes the content of JavaScript Buffer object to a file path in a storage bucket.
input:
{
"payload": {
"bucket": "abc.appspot.com", // optional, is otherwise set as node config
"path": "foo/bar/baz.json", // optional, see above
"contents": <Buffer obj>,
"contentType": "application/json" }, // optional
"metadata": { "very":"interesting"}, // optional
"public": true, // optional
"private": false // optional
}
}
# Auth nodes

View File

@ -21,18 +21,19 @@
"homepage": "https://github.com/psvensson/node-red-contrib-firebase-admin#readme",
"node-red": {
"nodes": {
"rtdb-set": "rtdb-set.js",
"rtdb-push": "rtdb-push.js",
"rtdb-get": "rtdb-get.js",
"rtdb-query": "rtdb-query.js",
"firestore-set": "firestore-set.js",
"firestore-add": "firestore-add.js",
"firestore-get": "firestore-get.js",
"firestore-query": "firestore-query.js",
"rtdb-set": "rtdb/rtdb-set.js",
"rtdb-push": "rtdb/rtdb-push.js",
"rtdb-get": "rtdb/rtdb-get.js",
"rtdb-query": "rtdb/rtdb-query.js",
"firestore-set": "firestore/firestore-set.js",
"firestore-add": "firestore/firestore-add.js",
"firestore-get": "firestore/firestore-get.js",
"firestore-query": "firestore/firestore-query.js",
"firebase-config": "firebase-config.js",
"flow-to-rtdb": "flow-to-rtdb.js",
"rtdb-to-flow": "rtdb-to-flow.js",
"storage-read": "storage-read.js"
"flow-to-rtdb": "rtdb/flow-to-rtdb.js",
"rtdb-to-flow": "rtdb/rtdb-to-flow.js",
"storage-read": "storage/storage-read.js",
"storage-write": "storage/storage-write.js"
}
},
"dependencies": {

View File

@ -11,7 +11,7 @@
},
inputs:1,
outputs:1,
icon: "firebase-admin-icon.png",
icon: "storage.png",
label: function() {
return this.name||"storage-read";
}

View File

@ -17,9 +17,6 @@ module.exports = function(RED) {
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){

View File

@ -0,0 +1,43 @@
<script type="text/javascript">
RED.nodes.registerType('storage-write',{
category: 'firebase-admin',
color: '#a6bbcf',
defaults: {
name: {value:""},
path: {value:""},
bucket: {value:""},
cred: {value: "", type: 'firebase-config'}
},
inputs:1,
outputs:0,
icon: "storage.png",
label: function() {
return this.name||"storage-write";
}
});
</script>
<script type="text/x-red" data-template-name="storage-write">
<div class="form-row">
<label for="node-input-bucket"><i class="icon-tag"></i> Bucket Name</label>
<input type="text" id="node-input-bucket" placeholder="Bucket Name">
</div>
<div class="form-row">
<label for="node-input-path"><i class="icon-tag"></i> Path</label>
<input type="text" id="node-input-path" placeholder="Path">
</div>
<div class="form-row">
<label for="node-input-cred"><i class="icon-tag"></i> Credentials</label>
<input type="text" id="node-input-cred" placeholder="Name">
</div>
</script>
<script type="text/x-red" data-help-name="storage-write">
<p>A node that read files from bucket path in google cloud storage</p>
</script>

46
storage/storage-write.js Normal file
View File

@ -0,0 +1,46 @@
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('configuring storage-write 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
let contents = msg.payload.contents
let options ={
contentType: msg.payload.contentType || 'auto',
metadata: msg.payload.metadata || {},
private: msg.payload.private || true,
public: msg.payload.public || true,
}
console.log('storage-write writing file to bucket "'+bucket+'" path "'+path+'"')
console.dir(msg.payload)
const myBucket = this.storage.bucket(bucket);
const file = myBucket.file(path);
file.save(contents, options, function(err) {
if (!err) {
// File written successfully.
} else {
console.log('cloud storage write error: '+err)
}
});
}
}.bind(this));
}
RED.nodes.registerType("storage-write", FirebaseAdmin);
}