all files / src/routes/ plugins.js

21.05% Statements 4/19
0% Branches 0/12
0% Functions 0/4
21.05% Lines 4/19
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40                                                                       
"use strict";
 
var _ = require('underscore');
var path = require('path');
 
var plugins = require('../plugins');
 
module.exports = function(app, middleware, controllers) {
	// Static Assets
	app.get('/plugins/:id/*', middleware.addExpiresHeaders, function(req, res, next) {
 
		var relPath = req._parsedUrl.pathname.replace('/plugins/', '');
 
		var matches = _.map(plugins.staticDirs, function(realPath, mappedPath) {
			if (relPath.match(mappedPath)) {
				var pathToFile = path.join(plugins.staticDirs[mappedPath], decodeURIComponent(relPath.slice(mappedPath.length)));
				if (pathToFile.startsWith(plugins.staticDirs[mappedPath])) {
					return pathToFile;
				}
			}
 
			return null;
		}).filter(Boolean);
 
		if (!matches || !matches.length) {
			return next();
		}
 
		res.sendFile(matches[0], {}, function(err) {
			if (err) {
				if (err.code === 'ENOENT') {
					// File doesn't exist, this isn't an error, to send to 404 handler
					return next();
				} else {
					return next(err);
				}
			}
		});
	});
};