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