refactord, tidied up, added icon for storage and a new node for writing buffers to storage files
This commit is contained in:
43
storage/storage-read.html
Normal file
43
storage/storage-read.html
Normal file
@ -0,0 +1,43 @@
|
||||
<script type="text/javascript">
|
||||
RED.nodes.registerType('storage-read',{
|
||||
|
||||
category: 'firebase-admin',
|
||||
color: '#a6bbcf',
|
||||
defaults: {
|
||||
name: {value:""},
|
||||
path: {value:""},
|
||||
bucket: {value:""},
|
||||
cred: {value: "", type: 'firebase-config'}
|
||||
},
|
||||
inputs:1,
|
||||
outputs:1,
|
||||
icon: "storage.png",
|
||||
label: function() {
|
||||
return this.name||"storage-read";
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
</script>
|
||||
|
||||
<script type="text/x-red" data-template-name="storage-read">
|
||||
<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-read">
|
||||
<p>A node that read files from bucket path in google cloud storage</p>
|
||||
</script>
|
||||
40
storage/storage-read.js
Normal file
40
storage/storage-read.js
Normal file
@ -0,0 +1,40 @@
|
||||
|
||||
|
||||
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('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
|
||||
.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);
|
||||
}
|
||||
43
storage/storage-write.html
Normal file
43
storage/storage-write.html
Normal 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
46
storage/storage-write.js
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user