2013-11-10 01:05:58 +01:00
|
|
|
/**
|
2017-01-11 16:24:33 +01:00
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
2013-11-10 01:05:58 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
**/
|
|
|
|
|
2015-04-14 19:41:48 +02:00
|
|
|
var fs = require('fs-extra');
|
2013-11-10 01:05:58 +01:00
|
|
|
var when = require('when');
|
2014-01-16 06:59:13 +01:00
|
|
|
var nodeFn = require('when/node/function');
|
|
|
|
var keys = require('when/keys');
|
2013-11-10 01:05:58 +01:00
|
|
|
var fspath = require("path");
|
2015-04-14 19:41:48 +02:00
|
|
|
var mkdirp = fs.mkdirs;
|
2014-01-16 06:59:13 +01:00
|
|
|
|
2015-02-03 23:02:26 +01:00
|
|
|
var log = require("../log");
|
|
|
|
|
2014-01-16 06:59:13 +01:00
|
|
|
var promiseDir = nodeFn.lift(mkdirp);
|
2013-11-10 01:05:58 +01:00
|
|
|
|
2015-11-20 23:48:06 +01:00
|
|
|
var initialFlowLoadComplete = false;
|
2013-11-10 01:05:58 +01:00
|
|
|
var settings;
|
|
|
|
var flowsFile;
|
2014-03-31 14:38:35 +02:00
|
|
|
var flowsFullPath;
|
2015-02-04 14:45:45 +01:00
|
|
|
var flowsFileBackup;
|
2013-11-10 01:05:58 +01:00
|
|
|
var credentialsFile;
|
2015-02-04 16:41:18 +01:00
|
|
|
var credentialsFileBackup;
|
2014-05-02 15:35:51 +02:00
|
|
|
var oldCredentialsFile;
|
2015-03-14 00:37:59 +01:00
|
|
|
var sessionsFile;
|
2013-11-10 01:05:58 +01:00
|
|
|
var libDir;
|
|
|
|
var libFlowsDir;
|
2014-08-28 01:35:07 +02:00
|
|
|
var globalSettingsFile;
|
2013-11-10 01:05:58 +01:00
|
|
|
|
|
|
|
function getFileMeta(root,path) {
|
|
|
|
var fn = fspath.join(root,path);
|
|
|
|
var fd = fs.openSync(fn,"r");
|
|
|
|
var size = fs.fstatSync(fd).size;
|
|
|
|
var meta = {};
|
|
|
|
var read = 0;
|
|
|
|
var length = 10;
|
2017-09-17 09:46:14 +02:00
|
|
|
var remaining = Buffer(0);
|
2013-11-10 01:05:58 +01:00
|
|
|
var buffer = Buffer(length);
|
2017-09-17 09:46:14 +02:00
|
|
|
var idx = -1;
|
2013-11-10 01:05:58 +01:00
|
|
|
while(read < size) {
|
|
|
|
read+=fs.readSync(fd,buffer,0,length);
|
2017-09-17 09:46:14 +02:00
|
|
|
var data = Buffer.concat([remaining,buffer]);
|
|
|
|
while((idx = data.indexOf("\n")) != -1){
|
|
|
|
var part = data.slice(0,idx+1);
|
|
|
|
var match = /^\/\/ (\w+): (.*)/.exec(part.toString());
|
2013-11-10 01:05:58 +01:00
|
|
|
if (match) {
|
|
|
|
meta[match[1]] = match[2];
|
|
|
|
} else {
|
|
|
|
read = size;
|
|
|
|
break;
|
|
|
|
}
|
2017-09-17 09:46:14 +02:00
|
|
|
data = data.slice(idx+1);
|
2013-11-10 01:05:58 +01:00
|
|
|
}
|
2017-09-17 09:46:14 +02:00
|
|
|
remaining = data;
|
2013-11-10 01:05:58 +01:00
|
|
|
}
|
|
|
|
fs.closeSync(fd);
|
|
|
|
return meta;
|
|
|
|
}
|
2014-05-02 15:35:51 +02:00
|
|
|
|
2013-11-10 01:05:58 +01:00
|
|
|
function getFileBody(root,path) {
|
2017-09-17 09:46:14 +02:00
|
|
|
var body = Buffer(0);
|
2013-11-10 01:05:58 +01:00
|
|
|
var fn = fspath.join(root,path);
|
|
|
|
var fd = fs.openSync(fn,"r");
|
|
|
|
var size = fs.fstatSync(fd).size;
|
|
|
|
var scanning = true;
|
|
|
|
var read = 0;
|
|
|
|
var length = 50;
|
2017-09-17 09:46:14 +02:00
|
|
|
var remaining = Buffer(0);
|
2013-11-10 01:05:58 +01:00
|
|
|
var buffer = Buffer(length);
|
2017-09-17 09:46:14 +02:00
|
|
|
var idx = -1;
|
2013-11-10 01:05:58 +01:00
|
|
|
while(read < size) {
|
|
|
|
var thisRead = fs.readSync(fd,buffer,0,length);
|
|
|
|
read += thisRead;
|
|
|
|
if (scanning) {
|
2017-09-17 09:46:14 +02:00
|
|
|
var data = Buffer.concat([remaining,buffer.slice(0,thisRead)]);
|
|
|
|
while((idx = data.indexOf("\n")) != -1){
|
|
|
|
var part = data.slice(0,idx+1);
|
|
|
|
if (! /^\/\/ \w+: /.test(part.toString())) {
|
2013-11-10 01:05:58 +01:00
|
|
|
scanning = false;
|
2017-09-17 09:46:14 +02:00
|
|
|
body = Buffer.concat([body,data]);
|
|
|
|
break;
|
2013-11-10 01:05:58 +01:00
|
|
|
}
|
2017-09-17 09:46:14 +02:00
|
|
|
data = data.slice(idx+1);
|
2013-11-10 01:05:58 +01:00
|
|
|
}
|
2017-09-17 09:46:14 +02:00
|
|
|
remaining = data;
|
|
|
|
if (scanning && read >= size) {
|
|
|
|
body = Buffer.concat([body,remaining]);
|
2013-11-10 01:05:58 +01:00
|
|
|
}
|
|
|
|
} else {
|
2017-09-17 09:46:14 +02:00
|
|
|
body = Buffer.concat([body,buffer.slice(0,thisRead)]);
|
2013-11-10 01:05:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fs.closeSync(fd);
|
2017-09-17 09:46:14 +02:00
|
|
|
return body.toString();
|
2013-11-10 01:05:58 +01:00
|
|
|
}
|
|
|
|
|
2015-04-14 19:41:48 +02:00
|
|
|
/**
|
2014-10-31 12:40:10 +01:00
|
|
|
* Write content to a file using UTF8 encoding.
|
|
|
|
* This forces a fsync before completing to ensure
|
|
|
|
* the write hits disk.
|
|
|
|
*/
|
|
|
|
function writeFile(path,content) {
|
|
|
|
return when.promise(function(resolve,reject) {
|
|
|
|
var stream = fs.createWriteStream(path);
|
|
|
|
stream.on('open',function(fd) {
|
2017-11-06 19:19:13 +01:00
|
|
|
stream.write(content,'utf8',function() {
|
|
|
|
fs.fsync(fd,function(err) {
|
|
|
|
if (err) {
|
|
|
|
log.warn(log._("storage.localfilesystem.fsync-fail",{path: path, message: err.toString()}));
|
|
|
|
}
|
|
|
|
stream.end(resolve);
|
|
|
|
});
|
2014-10-31 12:40:10 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
stream.on('error',function(err) {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-05-15 15:21:12 +02:00
|
|
|
function parseJSON(data) {
|
|
|
|
if (data.charCodeAt(0) === 0xFEFF) {
|
|
|
|
data = data.slice(1)
|
|
|
|
}
|
|
|
|
return JSON.parse(data);
|
|
|
|
}
|
2016-07-26 23:23:49 +02:00
|
|
|
|
|
|
|
function readFile(path,backupPath,emptyResponse,type) {
|
|
|
|
return when.promise(function(resolve) {
|
|
|
|
fs.readFile(path,'utf8',function(err,data) {
|
|
|
|
if (!err) {
|
|
|
|
if (data.length === 0) {
|
|
|
|
log.warn(log._("storage.localfilesystem.empty",{type:type}));
|
|
|
|
try {
|
|
|
|
var backupStat = fs.statSync(backupPath);
|
|
|
|
if (backupStat.size === 0) {
|
|
|
|
// Empty flows, empty backup - return empty flow
|
|
|
|
return resolve(emptyResponse);
|
|
|
|
}
|
|
|
|
// Empty flows, restore backup
|
|
|
|
log.warn(log._("storage.localfilesystem.restore",{path:backupPath,type:type}));
|
|
|
|
fs.copy(backupPath,path,function(backupCopyErr) {
|
|
|
|
if (backupCopyErr) {
|
|
|
|
// Restore backup failed
|
|
|
|
log.warn(log._("storage.localfilesystem.restore-fail",{message:backupCopyErr.toString(),type:type}));
|
|
|
|
resolve([]);
|
|
|
|
} else {
|
|
|
|
// Loop back in to load the restored backup
|
|
|
|
resolve(readFile(path,backupPath,emptyResponse,type));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
} catch(backupStatErr) {
|
|
|
|
// Empty flow file, no back-up file
|
|
|
|
return resolve(emptyResponse);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
2017-05-15 15:21:12 +02:00
|
|
|
return resolve(parseJSON(data));
|
2016-07-26 23:23:49 +02:00
|
|
|
} catch(parseErr) {
|
|
|
|
log.warn(log._("storage.localfilesystem.invalid",{type:type}));
|
|
|
|
return resolve(emptyResponse);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (type === 'flow') {
|
|
|
|
log.info(log._("storage.localfilesystem.create",{type:type}));
|
|
|
|
}
|
|
|
|
resolve(emptyResponse);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-11-10 01:05:58 +01:00
|
|
|
var localfilesystem = {
|
|
|
|
init: function(_settings) {
|
|
|
|
settings = _settings;
|
2015-04-14 19:41:48 +02:00
|
|
|
|
2015-02-25 15:23:59 +01:00
|
|
|
var promises = [];
|
2015-04-14 19:41:48 +02:00
|
|
|
|
2015-02-25 15:23:59 +01:00
|
|
|
if (!settings.userDir) {
|
2015-11-16 12:31:55 +01:00
|
|
|
try {
|
|
|
|
fs.statSync(fspath.join(process.env.NODE_RED_HOME,".config.json"));
|
2015-02-25 15:23:59 +01:00
|
|
|
settings.userDir = process.env.NODE_RED_HOME;
|
2015-11-16 12:31:55 +01:00
|
|
|
} catch(err) {
|
2017-07-03 16:22:49 +02:00
|
|
|
try {
|
|
|
|
// Consider compatibility for older versions
|
|
|
|
if (process.env.HOMEPATH) {
|
|
|
|
fs.statSync(fspath.join(process.env.HOMEPATH,".node-red",".config.json"));
|
|
|
|
settings.userDir = fspath.join(process.env.HOMEPATH,".node-red");
|
|
|
|
}
|
|
|
|
} catch(err) {
|
|
|
|
}
|
|
|
|
if (!settings.userDir) {
|
|
|
|
settings.userDir = fspath.join(process.env.HOME || process.env.USERPROFILE || process.env.HOMEPATH || process.env.NODE_RED_HOME,".node-red");
|
|
|
|
if (!settings.readOnly) {
|
|
|
|
promises.push(promiseDir(fspath.join(settings.userDir,"node_modules")));
|
|
|
|
}
|
2015-12-13 23:45:44 +01:00
|
|
|
}
|
2015-02-25 15:23:59 +01:00
|
|
|
}
|
|
|
|
}
|
2015-04-14 19:41:48 +02:00
|
|
|
|
2013-11-10 01:05:58 +01:00
|
|
|
if (settings.flowFile) {
|
|
|
|
flowsFile = settings.flowFile;
|
2015-04-16 20:46:22 +02:00
|
|
|
// handle Unix and Windows "C:\"
|
|
|
|
if ((flowsFile[0] == "/") || (flowsFile[1] == ":")) {
|
2015-02-26 12:30:20 +01:00
|
|
|
// Absolute path
|
|
|
|
flowsFullPath = flowsFile;
|
|
|
|
} else if (flowsFile.substring(0,2) === "./") {
|
|
|
|
// Relative to cwd
|
|
|
|
flowsFullPath = fspath.join(process.cwd(),flowsFile);
|
|
|
|
} else {
|
2015-11-16 12:31:55 +01:00
|
|
|
try {
|
|
|
|
fs.statSync(fspath.join(process.cwd(),flowsFile));
|
2015-02-26 12:30:20 +01:00
|
|
|
// Found in cwd
|
|
|
|
flowsFullPath = fspath.join(process.cwd(),flowsFile);
|
2015-11-16 12:31:55 +01:00
|
|
|
} catch(err) {
|
2015-02-26 12:30:20 +01:00
|
|
|
// Use userDir
|
|
|
|
flowsFullPath = fspath.join(settings.userDir,flowsFile);
|
|
|
|
}
|
|
|
|
}
|
2015-04-14 19:41:48 +02:00
|
|
|
|
2013-11-10 01:05:58 +01:00
|
|
|
} else {
|
|
|
|
flowsFile = 'flows_'+require('os').hostname()+'.json';
|
2015-02-25 15:23:59 +01:00
|
|
|
flowsFullPath = fspath.join(settings.userDir,flowsFile);
|
2013-11-10 01:05:58 +01:00
|
|
|
}
|
2015-02-04 14:45:45 +01:00
|
|
|
var ffExt = fspath.extname(flowsFullPath);
|
|
|
|
var ffName = fspath.basename(flowsFullPath);
|
|
|
|
var ffBase = fspath.basename(flowsFullPath,ffExt);
|
|
|
|
var ffDir = fspath.dirname(flowsFullPath);
|
2015-04-14 19:41:48 +02:00
|
|
|
|
2015-02-25 15:23:59 +01:00
|
|
|
credentialsFile = fspath.join(settings.userDir,ffBase+"_cred"+ffExt);
|
|
|
|
credentialsFileBackup = fspath.join(settings.userDir,"."+ffBase+"_cred"+ffExt+".backup");
|
2015-02-26 12:30:20 +01:00
|
|
|
|
2015-02-25 15:23:59 +01:00
|
|
|
oldCredentialsFile = fspath.join(settings.userDir,"credentials.json");
|
2015-04-14 19:41:48 +02:00
|
|
|
|
2015-02-04 14:45:45 +01:00
|
|
|
flowsFileBackup = fspath.join(ffDir,"."+ffName+".backup");
|
2014-05-02 15:35:51 +02:00
|
|
|
|
2015-03-14 00:37:59 +01:00
|
|
|
sessionsFile = fspath.join(settings.userDir,".sessions.json");
|
|
|
|
|
2015-02-25 15:23:59 +01:00
|
|
|
libDir = fspath.join(settings.userDir,"lib");
|
2013-11-10 01:05:58 +01:00
|
|
|
libFlowsDir = fspath.join(libDir,"flows");
|
2015-04-14 19:41:48 +02:00
|
|
|
|
2015-02-25 15:23:59 +01:00
|
|
|
globalSettingsFile = fspath.join(settings.userDir,".config.json");
|
2015-04-14 19:41:48 +02:00
|
|
|
|
2017-01-27 23:35:17 +01:00
|
|
|
var packageFile = fspath.join(settings.userDir,"package.json");
|
2017-01-28 15:21:22 +01:00
|
|
|
var packagePromise = when.resolve();
|
2015-12-13 23:45:44 +01:00
|
|
|
if (!settings.readOnly) {
|
|
|
|
promises.push(promiseDir(libFlowsDir));
|
2017-01-28 15:21:22 +01:00
|
|
|
packagePromise = function() {
|
|
|
|
try {
|
|
|
|
fs.statSync(packageFile);
|
|
|
|
} catch(err) {
|
|
|
|
var defaultPackage = {
|
|
|
|
"name": "node-red-project",
|
|
|
|
"description": "A Node-RED Project",
|
|
|
|
"version": "0.0.1"
|
|
|
|
};
|
|
|
|
return writeFile(packageFile,JSON.stringify(defaultPackage,"",4));
|
|
|
|
}
|
|
|
|
return true;
|
2017-01-27 23:35:17 +01:00
|
|
|
}
|
2015-12-13 23:45:44 +01:00
|
|
|
}
|
2017-01-28 15:21:22 +01:00
|
|
|
return when.all(promises).then(packagePromise);
|
2013-11-10 01:05:58 +01:00
|
|
|
},
|
2014-05-02 15:35:51 +02:00
|
|
|
|
2013-11-10 01:05:58 +01:00
|
|
|
getFlows: function() {
|
2016-07-26 23:23:49 +02:00
|
|
|
if (!initialFlowLoadComplete) {
|
|
|
|
initialFlowLoadComplete = true;
|
|
|
|
log.info(log._("storage.localfilesystem.user-dir",{path:settings.userDir}));
|
|
|
|
log.info(log._("storage.localfilesystem.flows-file",{path:flowsFullPath}));
|
|
|
|
}
|
|
|
|
return readFile(flowsFullPath,flowsFileBackup,[],'flow');
|
2013-11-10 01:05:58 +01:00
|
|
|
},
|
2014-05-02 15:35:51 +02:00
|
|
|
|
2013-11-10 01:05:58 +01:00
|
|
|
saveFlows: function(flows) {
|
2015-12-13 23:45:44 +01:00
|
|
|
if (settings.readOnly) {
|
|
|
|
return when.resolve();
|
|
|
|
}
|
|
|
|
|
2015-11-16 12:31:55 +01:00
|
|
|
try {
|
2015-02-04 14:45:45 +01:00
|
|
|
fs.renameSync(flowsFullPath,flowsFileBackup);
|
2015-11-16 12:31:55 +01:00
|
|
|
} catch(err) {
|
2014-05-07 01:33:50 +02:00
|
|
|
}
|
2015-04-14 19:41:48 +02:00
|
|
|
|
2014-06-07 23:33:29 +02:00
|
|
|
var flowData;
|
2015-04-14 19:41:48 +02:00
|
|
|
|
2014-06-07 23:33:29 +02:00
|
|
|
if (settings.flowFilePretty) {
|
|
|
|
flowData = JSON.stringify(flows,null,4);
|
|
|
|
} else {
|
|
|
|
flowData = JSON.stringify(flows);
|
|
|
|
}
|
2014-10-31 12:40:10 +01:00
|
|
|
return writeFile(flowsFullPath, flowData);
|
2013-11-10 01:05:58 +01:00
|
|
|
},
|
2014-05-02 15:35:51 +02:00
|
|
|
|
2013-11-10 01:05:58 +01:00
|
|
|
getCredentials: function() {
|
2016-07-26 23:23:49 +02:00
|
|
|
return readFile(credentialsFile,credentialsFileBackup,{},'credentials');
|
2013-11-10 01:05:58 +01:00
|
|
|
},
|
2014-05-02 15:35:51 +02:00
|
|
|
|
2013-11-10 01:05:58 +01:00
|
|
|
saveCredentials: function(credentials) {
|
2015-12-13 23:45:44 +01:00
|
|
|
if (settings.readOnly) {
|
|
|
|
return when.resolve();
|
|
|
|
}
|
|
|
|
|
2015-11-16 12:31:55 +01:00
|
|
|
try {
|
2015-02-04 14:45:45 +01:00
|
|
|
fs.renameSync(credentialsFile,credentialsFileBackup);
|
2015-11-16 12:31:55 +01:00
|
|
|
} catch(err) {
|
2015-02-04 14:45:45 +01:00
|
|
|
}
|
2014-06-07 23:33:29 +02:00
|
|
|
var credentialData;
|
|
|
|
if (settings.flowFilePretty) {
|
|
|
|
credentialData = JSON.stringify(credentials,null,4);
|
|
|
|
} else {
|
|
|
|
credentialData = JSON.stringify(credentials);
|
|
|
|
}
|
2014-10-31 12:40:10 +01:00
|
|
|
return writeFile(credentialsFile, credentialData);
|
2013-11-10 01:05:58 +01:00
|
|
|
},
|
2015-04-14 19:41:48 +02:00
|
|
|
|
2014-08-28 01:35:07 +02:00
|
|
|
getSettings: function() {
|
2015-11-16 12:31:55 +01:00
|
|
|
return when.promise(function(resolve,reject) {
|
|
|
|
fs.readFile(globalSettingsFile,'utf8',function(err,data) {
|
|
|
|
if (!err) {
|
2014-09-23 18:09:23 +02:00
|
|
|
try {
|
2017-05-15 15:21:12 +02:00
|
|
|
return resolve(parseJSON(data));
|
2015-11-16 12:31:55 +01:00
|
|
|
} catch(err2) {
|
2015-05-08 15:21:01 +02:00
|
|
|
log.trace("Corrupted config detected - resetting");
|
2014-09-23 18:09:23 +02:00
|
|
|
}
|
2014-09-22 22:35:30 +02:00
|
|
|
}
|
2015-11-16 12:31:55 +01:00
|
|
|
return resolve({});
|
|
|
|
})
|
|
|
|
})
|
2014-08-28 01:35:07 +02:00
|
|
|
},
|
2017-01-09 00:00:27 +01:00
|
|
|
saveSettings: function(newSettings) {
|
2015-12-13 23:45:44 +01:00
|
|
|
if (settings.readOnly) {
|
|
|
|
return when.resolve();
|
|
|
|
}
|
2017-01-09 00:00:27 +01:00
|
|
|
return writeFile(globalSettingsFile,JSON.stringify(newSettings,null,1));
|
2014-08-28 01:35:07 +02:00
|
|
|
},
|
2015-03-14 00:37:59 +01:00
|
|
|
getSessions: function() {
|
2015-11-16 12:31:55 +01:00
|
|
|
return when.promise(function(resolve,reject) {
|
|
|
|
fs.readFile(sessionsFile,'utf8',function(err,data){
|
|
|
|
if (!err) {
|
2015-03-14 00:37:59 +01:00
|
|
|
try {
|
2017-05-15 15:21:12 +02:00
|
|
|
return resolve(parseJSON(data));
|
2015-11-16 12:31:55 +01:00
|
|
|
} catch(err2) {
|
2015-05-08 15:21:01 +02:00
|
|
|
log.trace("Corrupted sessions file - resetting");
|
2015-03-14 00:37:59 +01:00
|
|
|
}
|
|
|
|
}
|
2015-11-16 12:31:55 +01:00
|
|
|
resolve({});
|
|
|
|
})
|
|
|
|
});
|
2015-03-14 00:37:59 +01:00
|
|
|
},
|
|
|
|
saveSessions: function(sessions) {
|
2015-12-13 23:45:44 +01:00
|
|
|
if (settings.readOnly) {
|
|
|
|
return when.resolve();
|
|
|
|
}
|
2015-03-14 00:37:59 +01:00
|
|
|
return writeFile(sessionsFile,JSON.stringify(sessions));
|
|
|
|
},
|
2015-04-14 19:41:48 +02:00
|
|
|
|
2013-11-10 01:05:58 +01:00
|
|
|
getLibraryEntry: function(type,path) {
|
|
|
|
var root = fspath.join(libDir,type);
|
|
|
|
var rootPath = fspath.join(libDir,type,path);
|
2017-01-09 00:00:27 +01:00
|
|
|
|
2017-01-09 00:01:28 +01:00
|
|
|
// don't create the folder if it does not exist - we are only reading....
|
|
|
|
return nodeFn.call(fs.lstat, rootPath).then(function(stats) {
|
|
|
|
if (stats.isFile()) {
|
|
|
|
return getFileBody(root,path);
|
|
|
|
}
|
|
|
|
if (path.substr(-1) == '/') {
|
|
|
|
path = path.substr(0,path.length-1);
|
|
|
|
}
|
|
|
|
return nodeFn.call(fs.readdir, rootPath).then(function(fns) {
|
|
|
|
var dirs = [];
|
|
|
|
var files = [];
|
|
|
|
fns.sort().filter(function(fn) {
|
|
|
|
var fullPath = fspath.join(path,fn);
|
|
|
|
var absoluteFullPath = fspath.join(root,fullPath);
|
|
|
|
if (fn[0] != ".") {
|
|
|
|
var stats = fs.lstatSync(absoluteFullPath);
|
|
|
|
if (stats.isDirectory()) {
|
|
|
|
dirs.push(fn);
|
|
|
|
} else {
|
|
|
|
var meta = getFileMeta(root,fullPath);
|
|
|
|
meta.fn = fn;
|
|
|
|
files.push(meta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return dirs.concat(files);
|
|
|
|
});
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
// if path is empty, then assume it was a folder, return empty
|
|
|
|
if (path === ""){
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
// if path ends with slash, it was a folder
|
|
|
|
// so return empty
|
|
|
|
if (path.substr(-1) == '/') {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
// else path was specified, but did not exist,
|
|
|
|
// check for path.json as an alternative if flows
|
|
|
|
if (type === "flows" && !/\.json$/.test(path)) {
|
|
|
|
return localfilesystem.getLibraryEntry(type,path+".json")
|
|
|
|
.otherwise(function(e) {
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
});
|
2013-11-10 01:05:58 +01:00
|
|
|
},
|
2014-05-02 15:35:51 +02:00
|
|
|
|
2013-11-10 01:05:58 +01:00
|
|
|
saveLibraryEntry: function(type,path,meta,body) {
|
2015-12-13 23:45:44 +01:00
|
|
|
if (settings.readOnly) {
|
|
|
|
return when.resolve();
|
|
|
|
}
|
2017-07-09 12:58:17 +02:00
|
|
|
if (type === "flows" && !path.endsWith(".json")) {
|
|
|
|
path += ".json";
|
|
|
|
}
|
2014-01-16 06:59:13 +01:00
|
|
|
var fn = fspath.join(libDir, type, path);
|
2013-11-10 01:05:58 +01:00
|
|
|
var headers = "";
|
|
|
|
for (var i in meta) {
|
2014-07-02 00:46:25 +02:00
|
|
|
if (meta.hasOwnProperty(i)) {
|
|
|
|
headers += "// "+i+": "+meta[i]+"\n";
|
|
|
|
}
|
2013-11-10 01:05:58 +01:00
|
|
|
}
|
2016-05-17 23:09:57 +02:00
|
|
|
if (type === "flows" && settings.flowFilePretty) {
|
|
|
|
body = JSON.stringify(JSON.parse(body),null,4);
|
|
|
|
}
|
2014-01-16 06:59:13 +01:00
|
|
|
return promiseDir(fspath.dirname(fn)).then(function () {
|
2014-10-31 12:40:10 +01:00
|
|
|
writeFile(fn,headers+body);
|
2013-11-10 01:05:58 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = localfilesystem;
|