Files
NodeBB/src/routes/plugins.js

75 lines
1.8 KiB
JavaScript
Raw Normal View History

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) {
2013-11-21 12:28:10 -05:00
app.get('/plugins/fireHook', function(req, res) {
// GET = filter
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
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-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-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');
}
});
};
module.exports = PluginRoutes;