2015-07-15 19:01:23 +03:00
|
|
|
var _ = require('lodash'),
|
|
|
|
express = require('express'),
|
|
|
|
path = require('path'),
|
|
|
|
config = require('../config'),
|
2017-03-13 23:13:17 +03:00
|
|
|
themeUtils = require('../themes'),
|
2015-07-15 19:01:23 +03:00
|
|
|
utils = require('../utils');
|
|
|
|
|
|
|
|
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()) {
|
2016-01-23 23:34:11 +03:00
|
|
|
next();
|
|
|
|
} else {
|
2017-03-02 20:18:21 +03:00
|
|
|
var configMaxAge = config.get('caching:theme:maxAge');
|
|
|
|
|
2017-03-13 23:13:17 +03:00
|
|
|
express.static(themeUtils.getActive().path,
|
2017-03-02 20:18:21 +03:00
|
|
|
{maxAge: (configMaxAge || configMaxAge === 0) ? configMaxAge : utils.ONE_YEAR_MS}
|
2016-01-23 23:34:11 +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;
|