2018-09-20 16:03:33 +03:00
|
|
|
const express = require('express');
|
|
|
|
const path = require('path');
|
2018-09-20 21:04:34 +03:00
|
|
|
const config = require('../../../config');
|
|
|
|
const constants = require('../../../lib/constants');
|
|
|
|
const themeUtils = require('../../../services/themes');
|
2015-07-15 19:01:23 +03:00
|
|
|
|
|
|
|
function isBlackListedFileType(file) {
|
2018-09-20 16:03:33 +03:00
|
|
|
const blackListedFileTypes = ['.hbs', '.md', '.json'];
|
|
|
|
const ext = path.extname(file);
|
|
|
|
|
2018-09-10 15:07:57 +03:00
|
|
|
return blackListedFileTypes.includes(ext);
|
2015-07-15 19:01:23 +03:00
|
|
|
}
|
|
|
|
|
2016-06-29 23:44:01 +03:00
|
|
|
function isWhiteListedFile(file) {
|
2018-09-20 16:03:33 +03:00
|
|
|
const whiteListedFiles = ['manifest.json'];
|
|
|
|
const base = path.basename(file);
|
|
|
|
|
2018-09-10 15:07:57 +03:00
|
|
|
return whiteListedFiles.includes(base);
|
2016-06-29 23:44:01 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-09-10 15:07:57 +03:00
|
|
|
const configMaxAge = config.get('caching:theme:maxAge');
|
2017-11-01 16:44:54 +03:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
2018-09-20 16:03:33 +03:00
|
|
|
|
2015-07-15 19:01:23 +03:00
|
|
|
return forwardToExpressStatic(req, res, next);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = staticTheme;
|