2018-09-20 16:03:33 +03:00
|
|
|
const path = require('path');
|
2020-05-27 20:47:53 +03:00
|
|
|
const config = require('../../../../shared/config');
|
2020-08-11 14:51:16 +03:00
|
|
|
const constants = require('@tryghost/constants');
|
2021-04-23 15:22:45 +03:00
|
|
|
const themeEngine = require('../../../../frontend/services/theme-engine');
|
2020-05-01 21:29:42 +03:00
|
|
|
const express = require('../../../../shared/express');
|
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) {
|
2021-04-23 15:22:45 +03:00
|
|
|
if (!themeEngine.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
|
|
|
|
2021-04-23 15:22:45 +03:00
|
|
|
express.static(themeEngine.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;
|