2015-07-15 18:01:23 +02:00
|
|
|
var _ = require('lodash'),
|
|
|
|
express = require('express'),
|
|
|
|
path = require('path'),
|
|
|
|
config = require('../config'),
|
2017-03-13 20:13:17 +00:00
|
|
|
themeUtils = require('../themes'),
|
2015-07-15 18:01:23 +02:00
|
|
|
utils = require('../utils');
|
|
|
|
|
|
|
|
function isBlackListedFileType(file) {
|
|
|
|
var blackListedFileTypes = ['.hbs', '.md', '.json'],
|
|
|
|
ext = path.extname(file);
|
2016-06-11 12:23:27 -06:00
|
|
|
return _.includes(blackListedFileTypes, ext);
|
2015-07-15 18:01:23 +02:00
|
|
|
}
|
|
|
|
|
2016-06-29 22:44:01 +02:00
|
|
|
function isWhiteListedFile(file) {
|
|
|
|
var whiteListedFiles = ['manifest.json'],
|
|
|
|
base = path.basename(file);
|
|
|
|
return _.includes(whiteListedFiles, base);
|
|
|
|
}
|
|
|
|
|
2015-07-15 18:01:23 +02:00
|
|
|
function forwardToExpressStatic(req, res, next) {
|
2017-03-13 20:13:17 +00:00
|
|
|
if (!themeUtils.getActive()) {
|
2017-11-01 13:44:54 +00:00
|
|
|
return next();
|
2016-01-23 12:34:11 -08:00
|
|
|
}
|
2017-11-01 13:44:54 +00:00
|
|
|
|
|
|
|
var configMaxAge = config.get('caching:theme:maxAge');
|
|
|
|
|
|
|
|
express.static(themeUtils.getActive().path,
|
|
|
|
{maxAge: (configMaxAge || configMaxAge === 0) ? configMaxAge : utils.ONE_YEAR_MS}
|
|
|
|
)(req, res, next);
|
2015-07-15 18:01:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function staticTheme() {
|
|
|
|
return function blackListStatic(req, res, next) {
|
2016-06-29 22:44:01 +02:00
|
|
|
if (!isWhiteListedFile(req.path) && isBlackListedFileType(req.path)) {
|
2015-07-15 18:01:23 +02:00
|
|
|
return next();
|
|
|
|
}
|
|
|
|
return forwardToExpressStatic(req, res, next);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = staticTheme;
|