Remove event passing for icons/examples from the api layer

This commit is contained in:
Nick O'Leary
2017-02-15 22:54:32 +00:00
parent 702e6d3b51
commit 869fdbcc6a
12 changed files with 291 additions and 149 deletions

View File

@@ -20,6 +20,7 @@ var when = require('when');
var redApp = null;
var storage;
var log;
var redNodes;
var needsPermission = require("./auth").needsPermission;
function createLibrary(type) {
@@ -72,80 +73,22 @@ function createLibrary(type) {
}
}
var exampleRoots = {};
var exampleFlows = {d:{}};
var exampleCount = 0;
function getFlowsFromPath(path) {
return when.promise(function(resolve,reject) {
var result = {};
fs.readdir(path,function(err,files) {
var promises = [];
var validFiles = [];
files.forEach(function(file) {
var fullPath = fspath.join(path,file);
var stats = fs.lstatSync(fullPath);
if (stats.isDirectory()) {
validFiles.push(file);
promises.push(getFlowsFromPath(fullPath));
} else if (/\.json$/.test(file)){
validFiles.push(file);
exampleCount++;
promises.push(when.resolve(file.split(".")[0]))
}
})
var i=0;
when.all(promises).then(function(results) {
results.forEach(function(r) {
if (typeof r === 'string') {
result.f = result.f||[];
result.f.push(r);
} else {
result.d = result.d||{};
result.d[validFiles[i]] = r;
}
i++;
})
resolve(result);
})
});
})
}
function addNodeExamplesDir(module) {
exampleRoots[module.name] = module.path;
getFlowsFromPath(module.path).then(function(result) {
exampleFlows.d[module.name] = result;
});
}
function removeNodeExamplesDir(module) {
delete exampleRoots[module];
delete exampleFlows.d[module];
}
module.exports = {
init: function(app,runtime) {
redApp = app;
log = runtime.log;
storage = runtime.storage;
// TODO: this allows init to be called multiple times without
// registering multiple instances of the listener.
// It isn't.... ideal.
runtime.events.removeListener("node-examples-dir",addNodeExamplesDir);
runtime.events.on("node-examples-dir",addNodeExamplesDir);
runtime.events.removeListener("node-module-uninstalled",removeNodeExamplesDir);
runtime.events.on("node-module-uninstalled",removeNodeExamplesDir);
redNodes = runtime.nodes;
},
register: createLibrary,
getAll: function(req,res) {
storage.getAllFlows().then(function(flows) {
log.audit({event: "library.get.all",type:"flow"},req);
if (exampleCount > 0) {
var examples = redNodes.getNodeExampleFlows();
if (examples) {
flows.d = flows.d||{};
flows.d._examples_ = exampleFlows;
flows.d._examples_ = redNodes.getNodeExampleFlows();
}
res.json(flows);
});
@@ -155,9 +98,9 @@ module.exports = {
var m = /^_examples_\/([^\/]+)\/(.*)$/.exec(req.params[0]);
if (m) {
var module = m[1];
var path = m[2]+".json";
if (exampleRoots[module]) {
var fullPath = fspath.join(exampleRoots[module],path);
var path = m[2];
var fullPath = redNodes.getNodeExampleFlowPath(module,path);
if (fullPath) {
try {
fs.statSync(fullPath);
log.audit({event: "library.get",type:"flow",path:req.params[0]},req);

View File

@@ -16,34 +16,20 @@
var express = require('express');
var fs = require("fs");
var path = require("path");
var Mustache = require("mustache");
var theme = require("./theme");
var Mustache = require("mustache");
var redNodes;
var icon_paths = {
"node-red":[path.resolve(__dirname + '/../../public/icons')]
};
var iconCache = {};
//TODO: create a default icon
var defaultIcon = path.resolve(__dirname + '/../../public/icons/arrow-in.png');
var templateDir = path.resolve(__dirname+"/../../editor/templates");
var editorTemplate;
function nodeIconDir(dir) {
icon_paths[dir.name] = icon_paths[dir.name] || [];
icon_paths[dir.name].push(path.resolve(dir.path));
}
module.exports = {
init: function(runtime) {
redNodes = runtime.nodes;
editorTemplate = fs.readFileSync(path.join(templateDir,"index.mst"),"utf8");
Mustache.parse(editorTemplate);
// TODO: this allows init to be called multiple times without
// registering multiple instances of the listener.
// It isn't.... ideal.
runtime.events.removeListener("node-icon-dir",nodeIconDir);
runtime.events.on("node-icon-dir",nodeIconDir);
},
ensureSlash: function(req,res,next) {
@@ -59,26 +45,8 @@ module.exports = {
icon: function(req,res) {
var icon = req.params.icon;
var module = req.params.module;
var iconName = module+"/"+icon;
if (iconCache[iconName]) {
res.sendFile(iconCache[iconName]); // if not found, express prints this to the console and serves 404
} else {
var paths = icon_paths[module];
if (paths) {
for (var p=0;p<paths.length;p++) {
var iconPath = path.join(paths[p],icon);
try {
fs.statSync(iconPath);
res.sendFile(iconPath);
iconCache[iconName] = iconPath;
return;
} catch(err) {
// iconPath doesn't exist
}
}
}
res.sendFile(defaultIcon);
}
var iconPath = redNodes.getNodeIconPath(module,icon);
res.sendFile(iconPath);
},
editor: function(req,res) {
res.send(Mustache.render(editorTemplate,theme.context()));