2013-11-21 12:28:10 -05:00
|
|
|
"use strict";
|
|
|
|
|
|
2013-11-03 17:15:18 -05:00
|
|
|
var nconf = require('nconf'),
|
|
|
|
|
path = require('path'),
|
|
|
|
|
fs = require('fs'),
|
2013-11-21 22:09:40 -05:00
|
|
|
validator = require('validator'),
|
2014-02-28 16:21:02 -05:00
|
|
|
_ = require('underscore'),
|
|
|
|
|
async = require('async'),
|
2013-11-30 13:35:42 -05:00
|
|
|
plugins = require('../plugins'),
|
2013-11-03 17:15:18 -05:00
|
|
|
|
|
|
|
|
PluginRoutes = function(app) {
|
2014-03-03 12:37:57 -05:00
|
|
|
/**
|
|
|
|
|
* GET/PUT /plugins/fireHook to be deprecated after 0.4.x
|
|
|
|
|
*
|
|
|
|
|
*/
|
2013-11-21 12:28:10 -05:00
|
|
|
app.get('/plugins/fireHook', function(req, res) {
|
|
|
|
|
// GET = filter
|
2013-12-01 20:11:27 -05:00
|
|
|
plugins.fireHook('filter:' + req.query.hook, req.query.args, function(err, returnData) {
|
2013-11-21 12:28:10 -05:00
|
|
|
if (typeof returnData === 'object') {
|
|
|
|
|
res.json(200, returnData);
|
|
|
|
|
} else {
|
2014-02-19 17:23:25 -05:00
|
|
|
res.send(200, validator.escape(returnData));
|
2013-11-21 12:28:10 -05:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.put('/plugins/fireHook', function(req, res) {
|
|
|
|
|
// PUT = action
|
2014-01-29 12:28:21 -05:00
|
|
|
var hook = 'action:' + req.body.hook;
|
|
|
|
|
if (plugins.hasListeners(hook)) {
|
|
|
|
|
// Hook executes
|
|
|
|
|
plugins.fireHook(hook, req.body.args);
|
|
|
|
|
}
|
2014-01-29 12:58:52 -05:00
|
|
|
|
|
|
|
|
res.send(200);
|
2013-11-21 12:28:10 -05:00
|
|
|
});
|
|
|
|
|
|
2013-11-03 17:15:18 -05:00
|
|
|
// Static Assets
|
|
|
|
|
app.get('/plugins/:id/*', function(req, res) {
|
2014-02-28 16:21:02 -05:00
|
|
|
var relPath = req._parsedUrl.pathname.replace(nconf.get('relative_path') + '/plugins/', ''),
|
|
|
|
|
matches = _.map(plugins.staticDirs, function(realPath, mappedPath) {
|
|
|
|
|
if (relPath.match(mappedPath)) {
|
|
|
|
|
return mappedPath;
|
|
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}).filter(function(a) { return a; });
|
|
|
|
|
|
|
|
|
|
if (matches) {
|
|
|
|
|
async.map(matches, function(mappedPath, next) {
|
2014-02-28 23:38:04 -05:00
|
|
|
var filePath = path.join(plugins.staticDirs[mappedPath], decodeURIComponent(relPath.slice(mappedPath.length)));
|
2014-01-06 23:44:32 -05:00
|
|
|
|
2014-02-28 16:21:02 -05:00
|
|
|
fs.exists(filePath, function(exists) {
|
|
|
|
|
if (exists) {
|
|
|
|
|
next(null, filePath);
|
|
|
|
|
} else {
|
|
|
|
|
next();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}, function(err, matches) {
|
|
|
|
|
// Filter out the nulls
|
|
|
|
|
matches = matches.filter(function(a) {
|
|
|
|
|
return a;
|
|
|
|
|
});
|
2014-01-06 23:44:32 -05:00
|
|
|
|
2014-02-28 16:21:02 -05:00
|
|
|
if (matches.length) {
|
|
|
|
|
res.sendfile(matches[0]);
|
2013-11-03 17:15:18 -05:00
|
|
|
} else {
|
|
|
|
|
res.redirect('/404');
|
|
|
|
|
}
|
2013-11-21 12:28:10 -05:00
|
|
|
});
|
2013-11-03 17:15:18 -05:00
|
|
|
} else {
|
|
|
|
|
res.redirect('/404');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2013-12-01 20:11:27 -05:00
|
|
|
module.exports = PluginRoutes;
|