forked from psvensson/node-red-contrib-storage.google
-
Notifications
You must be signed in to change notification settings - Fork 1
/
google-storage.js
349 lines (323 loc) · 11.6 KB
/
google-storage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
const {Storage} = require('@google-cloud/storage');
let util = require('util');
let fs = require('fs');
let nodered = require('@node-red/runtime')
let admin = require('firebase-admin')
let settings;
let projectId
let credentials
let appname = 'default';
let storage
let bucketName = 'node-red-storage'
let bucket
let currentFlowRev = {};
let currentSettingsRev = null;
let currentCredRev = null;
let libraryCache = {};
let flowFile = 'flow'
let googleStorage = {
init: (_settings, runtime)=> {
return new Promise((resolve,reject)=>{
console.log('google-storage init adminApi is '+runtime.adminApi)
console.dir(runtime.adminApi.httpAdmin)
console.dir(_settings)
googleStorage.settings = _settings;
googleStorage.runtime = runtime
if(!_settings.googleStorageBucket || !_settings.googleProjectId || !_settings.googleCredentials){
reject('Cannot initialize without Google Cloud settings')
} else {
console.log('---- *** node-red google cloud storage got all needed settings! *** ---')
if(_settings.flowFile){
flowFile = _settings.flowFile.replace('.json','')
}
googleStorage.getBucket().then(()=>{
if(_settings.googleFirebaseReload === true){
googleStorage.setupFirebaseListener()
}
googleStorage.prepopulateFlows(resolve)
})
}
})
},
setupFirebaseListener: ()=>{
let init = !(admin.apps.length === 0)
console.log('setting up firebase listener for flow reloading')
if(!init){
admin.initializeApp({
credential: admin.credential.cert(googleStorage.settings.googleCredentials),
databaseURL: googleStorage.settings.googleDbUrl
});
setTimeout(()=>{
console.log('-------------------------------------- firebase admin SDK initialized. listening on "'+googleStorage.appname+'"')
admin.database().ref(googleStorage.appname).on('value', (flowref)=>{
if(googleStorage.settingFlow !== true){
console.log('firebase listener got update for new flow')
let flows = flowref.val()
console.log(JSON.stringify(flows))
googleStorage.runtime.nodes.loadFlows(true).then(function()
{
console.log('--- flow reloaded from google cloud storage plugin')
})
} else {
googleStorage.settingFlow = false
}
})
}, 5000)
} else {
console.log('google-storage.setupFirebaseListener skipped initialzing firebase-admin since it was already setup, either by us before or by a flow using firebase-admin')
}
},
prepopulateFlows: (resolve)=> {
console.log('google-storage prepopulateFlows called')
googleStorage.getBucket().then((bucket) => {
googleStorage.getFlows().then((existing_flows)=> {
console.log('existing flows is')
console.dir(existing_flows)
if (!existing_flows) {
console.log(">> No default flow found");
let default_flow = [{
"id": "7b9c5129.e9e8",
"type": "tab",
"label": "Flow 1",
"disabled": false,
"info": ""
}]
googleStorage.saveFlows(default_flow).then(()=>{
googleStorage.saveLibraryEntry('flows', 'flow', undefined, default_flow).then(()=>{
resolve()
})
})
} else {
resolve()
}
})
})
},
getBucket: ()=> {
return new Promise(function(resolve,reject) {
console.log('getBucket called')
if (this.bucket) {
console.log('getBucket found bucket')
console.dir(this.bucket)
resolve(this.bucket)
} else {
bucketName = googleStorage.settings.googleStorageBucket;
projectId = googleStorage.settings.googleProjectId
credentials = googleStorage.settings.googleCredentials
//email = googleStorage.settings.googleEmail
appname = googleStorage.settings.googleStorageAppname || 'default';
googleStorage.appname = 'node-red/'+appname
const opts = {
projectId: projectId,
credentials: credentials
}
storage = new Storage(opts);
console.dir(opts)
try {
// Creates the new bucket
console.log('creating bucket with name "'+bucketName+'"')
this.bucket = storage.bucket(bucketName)
resolve(this.bucket)
}
catch (ex) {
console.log('google-storage caught exception!')
console.dir(ex)
}
}
}.bind(this))
},
getFlows: function() {
console.log('------------------------------------------- getFlow called')
return this.getArrayData(flowFile) ;
},
saveFlows: function(flows) {
return new Promise(function(resolve,reject)
{
console.log('------------------------------------------- saveFlow called for')
console.dir(flows)
let flowData
if (googleStorage.settings.flowFilePretty)
{
flowData = JSON.stringify(flows, null, 4);
} else
{
flowData = JSON.stringify(flows);
}
if (googleStorage.settings.googleFirebaseReload === true)
{
googleStorage.settingFlow = true
this.saveData(flowFile, flowData, true).then(() =>
{
console.log('updating flows through firebase')
let dbref = admin.database().ref(googleStorage.appname)
console.log('dbref = '+dbref+' typeof = '+(typeof dbref))
dbref.set(flows)
console.log('setref = '+setref+' typeof = '+(typeof setref))
console.log('updated flows through firebase')
resolve()
})
} else
{
this.saveData(flowFile, flowData, true);
resolve()
}
}.bind(this))
},
getCredentials: function() {
console.log('------------------------------------------- getCredentials')
return this.getData("credential") ;
},
saveCredentials: function(creds) {
console.log('------------------------------------------- saveCredentials')
return this.saveData("credential", creds) ;
},
getSettings: function() {
return new Promise(function(resolve,reject) {
console.log('google-storage.getSettings called')
this.getData("settings").then((ssettings) => {
if (ssettings) {
resolve(ssettings)
}
}, (err)=>{
console.log('**** settings file not found i google cloud storage so returning boot settings.js')
resolve(googleStorage.settings)
})
//resolve(googleStorage.settings)
}.bind(this))
},
saveSettings: function(settings) {
console.log('------------------------------------------- saveSettings')
let props = ['functionGlobalContext', 'userDir', 'storageModule']
var s = {}
for(var p in settings){
if((typeof settings[p] !== 'function') && !props.includes(p)){
console.log(p+' includes = '+(props.includes(p)))
s[p] = settings[p]
}
}
//let s = {nodes: settings.nodes}
return this.saveData("settings", s, ) ;
},
getData: function(entryType) {
console.log('------------------------------------------- getData reading single file from path "'+entryType+'"')
return new Promise(function(resolve,reject) {
this.getBucket().then((bucket)=>{
let fname = googleStorage.appname + '/' + entryType+'.json'
console.log('getData checking if file '+fname+' exists')
let file = bucket.file(fname)
file.exists().then((exists)=>{
console.log('getData file '+entryType+' exists = '+exists[0])
if(exists[0]){
console.log('downloading file '+entryType)
file.download().then((file)=>{
console.log('storage-read.getData got file '+entryType)
console.dir(file)
let rv = JSON.parse(file.toString())
console.log('getData returning: '+rv)
console.dir(rv)
resolve(rv)
})
}else {
console.log('googe-storage getData returning undefined for "'+entryType+'"')
resolve([])
}
})
})
}.bind(this))
},
getArrayData: function(entryType) {
return this.getData(entryType)
},
saveData: function(entryType, dataEntry, bypass) {
console.log('------------------------------------------- google-storage saveData for "'+entryType+'" bypass = '+bypass)
console.dir(dataEntry)
return new Promise(function(resolve,reject) {
if(!dataEntry){
dataEntry = ''
}
console.log('getting bucket..')
this.getBucket().then((bucket) => {
console.log('-----------------------saveData saving file "'+entryType+'"')
console.dir(dataEntry)
console.log('-----------------------saveData')
let f = googleStorage.appname + '/' + entryType + (entryType.indexOf('.js') > -1 ? '' : '.json')
console.log('f = "'+f+'"')
let file = bucket.file(f);
let data = Buffer.from(bypass === true ? dataEntry : JSON.stringify(dataEntry));
file.save(data, function(err) {
if (err) {
console.log('save ERROR: ' + err)
reject(err.toString());
} else {
console.log('saved OK')
resolve();
}
});
})
}.bind(this))
},
saveLibraryEntry: function(type,path,meta,body) {
console.log('------------------------------------------- saveLibrary called for type='+type+', path='+path+', meta='+meta+', body='+body)
let key = "lib/" + type + (path.substr(0) != "/" ? "/" : "") + path;
console.log('saveLibraryEntry for key "'+key+'"')
return this.saveData(key, body)
},
getLibraryEntry: function(type,path) {
return new Promise(function(resolve,reject)
{
let key = "lib/" + type + (path.substr(0) != "/" ? "/" : "") + path;
console.log("------------------------------------------- get library entry: " + type + ":" + path);
if(key.lastIndexOf('.') > key.length-4){
return this.getSingleLibraryFile(key)
} else {
return this.getLibraryDirectoryListing(key)
}
}.bind(this))
},
getSingleLibraryFile: function(key){
return new Promise(function(resolve,reject) {
this.getBucket().then((bucket) => {
let file = bucket.file(key)
file.exists().then((exists) => {
console.log('getLibraryEntry file ' + key + ' exists = ' + exists[0])
if (exists[0]) {
console.log('downloading file ' + key)
file.get().then((res) => {
console.log('storage-read.getLibraryEntry got file info ' + res)
console.dir(res)
let file = res[0]
console.dir(file)
let rv = JSON.parse(file.toString())
console.log('getLibraryEntry returning: ' + rv)
resolve(rv)
})
} else {
console.log('getLibraryEntry returning undefined for "'+key+'"')
resolve([])
}
})
})
}.bind(this))
},
getLibraryDirectoryListing: function(key){
return new Promise(function(resolve,reject) {
this.getBucket().then((bucket) => {
const options = {
prefix: key
};
if(this.delimiter){
options.delimiter = '/'
}
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] !== '/' })
console.log('getLibraryDirectoryListing for path '+key)
console.dir(f)
resolve(f)
})
})
}.bind(this))
}
};
module.exports = googleStorage;