Files
NodeBB/src/routes/plugins.js

55 lines
1.4 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'),
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 {
2013-11-21 22:09:40 -05:00
res.send(200, validator.sanitize(returnData).escape());
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) {
var relPath = req._parsedUrl.pathname.replace(nconf.get('relative_path') + '/plugins/' + req.params.id, '');
2013-11-30 13:54:52 -05:00
if (plugins.staticDirs[req.params.id]) {
var fullPath = path.join(plugins.staticDirs[req.params.id], decodeURIComponent(relPath));
2013-11-03 17:15:18 -05:00
fs.exists(fullPath, function(exists) {
if (exists) {
res.sendfile(fullPath, {
maxAge: app.enabled('cache') ? 5184000000 : 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;