2018-09-10 08:07:57 -04:00
|
|
|
const express = require('express'),
|
2017-12-14 14:13:40 +01:00
|
|
|
path = require('path'),
|
|
|
|
config = require('../../config'),
|
|
|
|
constants = require('../../lib/constants'),
|
|
|
|
themeUtils = require('../../services/themes');
|
2015-07-15 18:01:23 +02:00
|
|
|
|
|
|
|
function isBlackListedFileType(file) {
|
2018-09-10 08:07:57 -04:00
|
|
|
const blackListedFileTypes = ['.hbs', '.md', '.json'],
|
2015-07-15 18:01:23 +02:00
|
|
|
ext = path.extname(file);
|
2018-09-10 08:07:57 -04:00
|
|
|
return blackListedFileTypes.includes(ext);
|
2015-07-15 18:01:23 +02:00
|
|
|
}
|
|
|
|
|
2016-06-29 22:44:01 +02:00
|
|
|
function isWhiteListedFile(file) {
|
2018-09-10 08:07:57 -04:00
|
|
|
const whiteListedFiles = ['manifest.json'],
|
2016-06-29 22:44:01 +02:00
|
|
|
base = path.basename(file);
|
2018-09-10 08:07:57 -04:00
|
|
|
return whiteListedFiles.includes(base);
|
2016-06-29 22:44:01 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-09-10 08:07:57 -04:00
|
|
|
const configMaxAge = config.get('caching:theme:maxAge');
|
2017-11-01 13:44:54 +00:00
|
|
|
|
|
|
|
express.static(themeUtils.getActive().path,
|
2017-12-14 14:13:40 +01:00
|
|
|
{maxAge: (configMaxAge || configMaxAge === 0) ? configMaxAge : constants.ONE_YEAR_MS}
|
2017-11-01 13:44:54 +00:00
|
|
|
)(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;
|