2018-03-15 21:52:17 +01:00
|
|
|
/**
|
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
|
|
|
var fs = require('fs-extra');
|
2018-03-23 09:18:56 +01:00
|
|
|
var path = require("path");
|
2018-06-07 13:17:51 +02:00
|
|
|
var util = require("../../util");
|
2018-03-15 21:52:17 +01:00
|
|
|
|
2018-06-07 13:17:51 +02:00
|
|
|
function getStoragePath(storageBaseDir, scope) {
|
2018-05-30 03:24:27 +02:00
|
|
|
if(scope.indexOf(":") === -1){
|
2018-03-23 09:18:56 +01:00
|
|
|
if(scope === "global"){
|
2018-06-08 12:26:07 +02:00
|
|
|
return path.join(storageBaseDir,"global",scope);
|
2018-03-23 09:18:56 +01:00
|
|
|
}else{ // scope:flow
|
2018-06-07 13:17:51 +02:00
|
|
|
return path.join(storageBaseDir,scope,"flow");
|
2018-03-23 09:18:56 +01:00
|
|
|
}
|
|
|
|
}else{ // scope:local
|
|
|
|
var ids = scope.split(":")
|
2018-06-07 13:17:51 +02:00
|
|
|
return path.join(storageBaseDir,ids[1],ids[0]);
|
2018-03-23 09:18:56 +01:00
|
|
|
}
|
2018-03-15 21:52:17 +01:00
|
|
|
}
|
|
|
|
|
2018-06-07 13:17:51 +02:00
|
|
|
function getBasePath(config) {
|
2018-05-24 14:42:40 +02:00
|
|
|
var base = config.base || "contexts";
|
|
|
|
var storageBaseDir;
|
|
|
|
if (!config.dir) {
|
|
|
|
if(config.settings && config.settings.userDir){
|
|
|
|
storageBaseDir = path.join(config.settings.userDir, base);
|
|
|
|
}else{
|
|
|
|
try {
|
|
|
|
fs.statSync(path.join(process.env.NODE_RED_HOME,".config.json"));
|
|
|
|
storageBaseDir = path.join(process.env.NODE_RED_HOME, base);
|
|
|
|
} catch(err) {
|
2018-03-15 21:52:17 +01:00
|
|
|
try {
|
2018-05-24 14:42:40 +02:00
|
|
|
// Consider compatibility for older versions
|
|
|
|
if (process.env.HOMEPATH) {
|
|
|
|
fs.statSync(path.join(process.env.HOMEPATH,".node-red",".config.json"));
|
|
|
|
storageBaseDir = path.join(process.env.HOMEPATH, ".node-red", base);
|
2018-03-23 09:18:56 +01:00
|
|
|
}
|
2018-05-24 14:42:40 +02:00
|
|
|
} catch(err) {
|
|
|
|
}
|
|
|
|
if (!storageBaseDir) {
|
|
|
|
storageBaseDir = path.join(process.env.HOME || process.env.USERPROFILE || process.env.HOMEPATH || process.env.NODE_RED_HOME,".node-red", base);
|
2018-03-15 21:52:17 +01:00
|
|
|
}
|
2018-03-23 09:18:56 +01:00
|
|
|
}
|
2018-03-15 21:52:17 +01:00
|
|
|
}
|
2018-05-24 14:42:40 +02:00
|
|
|
}else{
|
|
|
|
storageBaseDir = path.join(config.dir, base);
|
|
|
|
}
|
|
|
|
return storageBaseDir;
|
|
|
|
}
|
2018-03-15 21:52:17 +01:00
|
|
|
|
2018-06-07 13:17:51 +02:00
|
|
|
function loadFile(storagePath){
|
|
|
|
return fs.pathExists(storagePath).then(function(exists){
|
|
|
|
if(exists === true){
|
|
|
|
return fs.readFile(storagePath, "utf8");
|
|
|
|
}else{
|
2018-06-20 12:50:55 +02:00
|
|
|
return Promise.resolve(undefined);
|
2018-06-07 13:17:51 +02:00
|
|
|
}
|
|
|
|
}).catch(function(err){
|
2018-06-20 12:50:55 +02:00
|
|
|
throw Promise.reject(err);
|
2018-06-07 13:17:51 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-24 14:42:40 +02:00
|
|
|
function LocalFileSystem(config){
|
|
|
|
this.config = config;
|
2018-06-07 13:17:51 +02:00
|
|
|
this.storageBaseDir = getBasePath(this.config);
|
2018-05-24 14:42:40 +02:00
|
|
|
}
|
|
|
|
|
2018-05-30 03:24:27 +02:00
|
|
|
LocalFileSystem.prototype.open = function(){
|
2018-06-20 12:50:55 +02:00
|
|
|
return Promise.resolve();
|
2018-05-30 03:24:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
LocalFileSystem.prototype.close = function(){
|
2018-06-20 12:50:55 +02:00
|
|
|
return Promise.resolve();
|
2018-05-30 03:24:27 +02:00
|
|
|
}
|
|
|
|
|
2018-06-22 10:11:54 +02:00
|
|
|
LocalFileSystem.prototype.get = function(scope, key, callback) {
|
|
|
|
if(typeof callback !== "function"){
|
|
|
|
throw new Error("Callback must be a function");
|
|
|
|
}
|
2018-06-07 13:17:51 +02:00
|
|
|
var storagePath = getStoragePath(this.storageBaseDir ,scope);
|
2018-06-22 10:11:54 +02:00
|
|
|
loadFile(storagePath + ".json").then(function(data){
|
2018-06-07 13:17:51 +02:00
|
|
|
if(data){
|
2018-06-22 10:11:54 +02:00
|
|
|
callback(null, util.getMessageProperty(JSON.parse(data),key));
|
2018-03-23 09:18:56 +01:00
|
|
|
}else{
|
2018-06-22 10:11:54 +02:00
|
|
|
callback(null, undefined);
|
2018-03-15 21:52:17 +01:00
|
|
|
}
|
2018-06-07 13:17:51 +02:00
|
|
|
}).catch(function(err){
|
2018-06-22 10:11:54 +02:00
|
|
|
callback(err);
|
2018-06-07 13:17:51 +02:00
|
|
|
});
|
2018-06-01 04:44:45 +02:00
|
|
|
};
|
|
|
|
|
2018-06-22 10:11:54 +02:00
|
|
|
LocalFileSystem.prototype.set =function(scope, key, value, callback) {
|
2018-06-07 13:17:51 +02:00
|
|
|
var storagePath = getStoragePath(this.storageBaseDir ,scope);
|
2018-06-22 10:11:54 +02:00
|
|
|
loadFile(storagePath + ".json").then(function(data){
|
2018-06-07 13:17:51 +02:00
|
|
|
var obj = data ? JSON.parse(data) : {}
|
|
|
|
util.setMessageProperty(obj,key,value);
|
2018-06-22 10:11:54 +02:00
|
|
|
return fs.outputFile(storagePath + ".json", JSON.stringify(obj, undefined, 4), "utf8");
|
|
|
|
}).then(function(){
|
|
|
|
if(typeof callback === "function"){
|
|
|
|
callback(null);
|
|
|
|
}
|
2018-06-07 13:17:51 +02:00
|
|
|
}).catch(function(err){
|
2018-06-22 10:11:54 +02:00
|
|
|
if(typeof callback === "function"){
|
|
|
|
callback(err);
|
|
|
|
}
|
2018-06-07 13:17:51 +02:00
|
|
|
});
|
2018-06-01 04:44:45 +02:00
|
|
|
};
|
|
|
|
|
2018-06-22 10:11:54 +02:00
|
|
|
LocalFileSystem.prototype.keys = function(scope, callback){
|
|
|
|
if(typeof callback !== "function"){
|
|
|
|
throw new Error("Callback must be a function");
|
|
|
|
}
|
2018-06-07 13:17:51 +02:00
|
|
|
var storagePath = getStoragePath(this.storageBaseDir ,scope);
|
2018-06-22 10:11:54 +02:00
|
|
|
loadFile(storagePath + ".json").then(function(data){
|
2018-06-07 13:17:51 +02:00
|
|
|
if(data){
|
2018-06-22 10:11:54 +02:00
|
|
|
callback(null, Object.keys(JSON.parse(data)));
|
2018-06-07 13:17:51 +02:00
|
|
|
}else{
|
2018-06-22 10:11:54 +02:00
|
|
|
callback(null, []);
|
2018-06-07 13:17:51 +02:00
|
|
|
}
|
|
|
|
}).catch(function(err){
|
2018-06-22 10:11:54 +02:00
|
|
|
callback(err);
|
2018-06-07 13:17:51 +02:00
|
|
|
});
|
2018-06-01 04:44:45 +02:00
|
|
|
};
|
|
|
|
|
2018-05-24 14:42:40 +02:00
|
|
|
LocalFileSystem.prototype.delete = function(scope){
|
2018-06-07 13:17:51 +02:00
|
|
|
var storagePath = getStoragePath(this.storageBaseDir ,scope);
|
2018-06-08 12:26:07 +02:00
|
|
|
return fs.remove(storagePath + ".json");
|
2018-05-24 14:42:40 +02:00
|
|
|
}
|
|
|
|
|
2018-05-30 08:23:34 +02:00
|
|
|
LocalFileSystem.prototype.clean = function(activeNodes){
|
|
|
|
var self = this;
|
|
|
|
return fs.readdir(self.storageBaseDir).then(function(dirs){
|
2018-06-20 12:50:55 +02:00
|
|
|
return Promise.all(dirs.reduce(function(result, item){
|
2018-06-04 06:04:56 +02:00
|
|
|
if(item !== "global" && activeNodes.indexOf(item) === -1){
|
2018-05-30 08:23:34 +02:00
|
|
|
result.push(fs.remove(path.join(self.storageBaseDir,item)));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
},[]));
|
2018-05-31 07:09:36 +02:00
|
|
|
}).catch(function(err){
|
|
|
|
if(err.code == 'ENOENT') {
|
2018-06-20 12:50:55 +02:00
|
|
|
return Promise.resolve();
|
2018-05-31 07:09:36 +02:00
|
|
|
}else{
|
2018-06-20 12:50:55 +02:00
|
|
|
return Promise.reject(err);
|
2018-05-31 07:09:36 +02:00
|
|
|
}
|
2018-05-30 08:23:34 +02:00
|
|
|
});
|
2018-06-08 12:26:07 +02:00
|
|
|
}
|
2018-05-30 08:23:34 +02:00
|
|
|
|
2018-05-24 14:42:40 +02:00
|
|
|
module.exports = function(config){
|
|
|
|
return new LocalFileSystem(config);
|
2018-03-15 21:52:17 +01:00
|
|
|
};
|
|
|
|
|