2015-07-15 19:01:23 +03:00
|
|
|
var _ = require('lodash'),
|
|
|
|
express = require('express'),
|
|
|
|
path = require('path'),
|
2017-12-06 19:37:54 +03:00
|
|
|
config = require('../../config'),
|
|
|
|
themeUtils = require('../../themes'),
|
|
|
|
utils = require('../../utils');
|
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,
|
|
|
|
{maxAge: (configMaxAge || configMaxAge === 0) ? configMaxAge : utils.ONE_YEAR_MS}
|
|
|
|
)(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;
|