new staticDirs format, @mrwaffle

v1.18.x
Julian Lam 11 years ago
parent db0ad5b57c
commit 561b42d0f9

@ -46,7 +46,8 @@
"nodebb-theme-cerulean": "~0.0.13", "nodebb-theme-cerulean": "~0.0.13",
"nodebb-theme-lavender": "~0.0.22", "nodebb-theme-lavender": "~0.0.22",
"less": "^1.6.3", "less": "^1.6.3",
"daemon": "~1.1.0" "daemon": "~1.1.0",
"underscore": "^1.6.0"
}, },
"optionalDependencies": { "optionalDependencies": {
"redis": "0.8.3", "redis": "0.8.3",

@ -174,7 +174,7 @@ var fs = require('fs'),
(function(staticDir) { (function(staticDir) {
fs.exists(staticDir, function(exists) { fs.exists(staticDir, function(exists) {
if (exists) { if (exists) {
Plugins.staticDirs[mappedPath] = staticDir; Plugins.staticDirs[path.join(pluginData.id, mappedPath)] = staticDir;
} else { } else {
winston.warn('[plugins/' + pluginData.id + '] Mapped path \'' + mappedPath + ' => ' + staticDir + '\' not found.'); winston.warn('[plugins/' + pluginData.id + '] Mapped path \'' + mappedPath + ' => ' + staticDir + '\' not found.');
} }

@ -4,6 +4,8 @@ var nconf = require('nconf'),
path = require('path'), path = require('path'),
fs = require('fs'), fs = require('fs'),
validator = require('validator'), validator = require('validator'),
_ = require('underscore'),
async = require('async'),
plugins = require('../plugins'), plugins = require('../plugins'),
PluginRoutes = function(app) { PluginRoutes = function(app) {
@ -31,16 +33,34 @@ var nconf = require('nconf'),
// Static Assets // Static Assets
app.get('/plugins/:id/*', function(req, res) { app.get('/plugins/:id/*', function(req, res) {
var relPath = req._parsedUrl.pathname.replace(nconf.get('relative_path') + '/plugins/' + req.params.id, ''); 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) {
var filePath = path.join(plugins.staticDirs[mappedPath], relPath.slice(mappedPath.length));
if (plugins.staticDirs[req.params.id]) { fs.exists(filePath, function(exists) {
var fullPath = path.join(plugins.staticDirs[req.params.id], decodeURIComponent(relPath)); if (exists) {
next(null, filePath);
} else {
next();
}
});
}, function(err, matches) {
// Filter out the nulls
matches = matches.filter(function(a) {
return a;
});
fs.exists(fullPath, function(exists) { if (matches.length) {
if (exists) { res.sendfile(matches[0]);
res.sendfile(fullPath, {
maxAge: app.enabled('cache') ? 5184000000 : 0
});
} else { } else {
res.redirect('/404'); res.redirect('/404');
} }

Loading…
Cancel
Save