addes storage-list and some supporting logic in storage-read
This commit is contained in:
49
storage/storage-list.html
Normal file
49
storage/storage-list.html
Normal file
@ -0,0 +1,49 @@
|
||||
<script type="text/javascript">
|
||||
RED.nodes.registerType('storage-list',{
|
||||
|
||||
category: 'firebase-admin',
|
||||
color: '#a6bbcf',
|
||||
defaults: {
|
||||
name: {value:""},
|
||||
path: {value:""},
|
||||
bucket: {value:""},
|
||||
cred: {value: "", type: 'firebase-config'},
|
||||
delimiter: {value: '/'}
|
||||
},
|
||||
inputs:1,
|
||||
outputs:1,
|
||||
icon: "storage.png",
|
||||
label: function() {
|
||||
return this.name||"storage-list";
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
</script>
|
||||
|
||||
<script type="text/x-red" data-template-name="storage-list">
|
||||
<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="Cred">
|
||||
</div>
|
||||
|
||||
<div class="form-row">
|
||||
<label for="node-input-delimiter"><i class="icon-tag"></i> Delimiter</label>
|
||||
<input type="text" id="node-input-delimiter" placeholder="Delimiter">
|
||||
</div>
|
||||
|
||||
</script>
|
||||
|
||||
<script type="text/x-red" data-help-name="storage-list">
|
||||
<p>A node that list files from bucket path in google cloud storage</p>
|
||||
</script>
|
||||
48
storage/storage-list.js
Normal file
48
storage/storage-list.js
Normal file
@ -0,0 +1,48 @@
|
||||
|
||||
|
||||
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
|
||||
}
|
||||
this.delimiter = config.delimiter
|
||||
|
||||
//console.log('configuring storage-list 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-list listing files from bucket "'+bucket+'" path "'+path+'"')
|
||||
const options = {
|
||||
prefix: path
|
||||
};
|
||||
|
||||
if(this.delimiter){
|
||||
options.delimiter = this.delimiter
|
||||
}
|
||||
|
||||
// Lists files in the bucket, filtered by a prefix
|
||||
this.storage.bucket(bucket).getFiles(options).then((files)=>{
|
||||
console.log('got file listing')
|
||||
//console.dir(files[0])
|
||||
let f = files[0].filter((e)=>{ return e.name[e.name.length-1] !== '/' })
|
||||
node.send({payload: {files: f}})
|
||||
})
|
||||
}
|
||||
}.bind(this));
|
||||
|
||||
|
||||
}
|
||||
RED.nodes.registerType("storage-list", FirebaseAdmin);
|
||||
}
|
||||
@ -22,15 +22,40 @@ module.exports = function(RED) {
|
||||
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})
|
||||
console.log('------------------------------ storage-read reading from bucket "'+bucket+'" path "'+path+'"')
|
||||
if(msg.payload.files && msg.payload.files.length > 0){
|
||||
console.log('--reading from files')
|
||||
let count = msg.payload.files.length
|
||||
let rv = {}
|
||||
msg.payload.files.forEach((_file)=>{
|
||||
((file)=>{
|
||||
console.log('reading file '+file.name)
|
||||
this.storage
|
||||
.bucket(bucket)
|
||||
.file(file.name).download().then((content)=>{
|
||||
console.log('storage-read got file '+file.name)
|
||||
rv[file.name] = content
|
||||
if(--count === 0){
|
||||
console.log('all files done, sending..')
|
||||
for(let x in rv){
|
||||
console.log(x)
|
||||
}
|
||||
node.send({payload:rv})
|
||||
}
|
||||
})
|
||||
})(_file)
|
||||
})
|
||||
} else {
|
||||
console.log('reading single file from 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));
|
||||
|
||||
|
||||
@ -26,8 +26,8 @@ module.exports = function(RED) {
|
||||
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)
|
||||
console.log('storage-write writing file to bucket "'+bucket+'" path "'+path+'" contents is of type '+(typeof contents))
|
||||
console.dir(contents)
|
||||
const myBucket = this.storage.bucket(bucket);
|
||||
const file = myBucket.file(path);
|
||||
file.save(contents, options, function(err) {
|
||||
|
||||
Reference in New Issue
Block a user