mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-03 23:00:14 -05:00
53d14fd8e3
- Added a wrapper around express.Router to our shared/express util - Also export static and _express - Use this shared util everywhre, meaning express is only used directly in this one file - ATM this file is mostly an experiment / debug helper, it might be removed again later - The aim is to have a minimal framework wrapping express that allows us to: - reduce our usage of express() in favour of Router() - unify some of our duplicated logic - fix some structural issues e.g. Sentry - make it easier to understand the codebase
43 lines
1.2 KiB
JavaScript
43 lines
1.2 KiB
JavaScript
const path = require('path');
|
|
const config = require('../../../config');
|
|
const constants = require('../../../lib/constants');
|
|
const themeUtils = require('../../../../frontend/services/themes');
|
|
const express = require('../../../../shared/express');
|
|
|
|
function isBlackListedFileType(file) {
|
|
const blackListedFileTypes = ['.hbs', '.md', '.json'];
|
|
const ext = path.extname(file);
|
|
|
|
return blackListedFileTypes.includes(ext);
|
|
}
|
|
|
|
function isWhiteListedFile(file) {
|
|
const whiteListedFiles = ['manifest.json'];
|
|
const base = path.basename(file);
|
|
|
|
return whiteListedFiles.includes(base);
|
|
}
|
|
|
|
function forwardToExpressStatic(req, res, next) {
|
|
if (!themeUtils.getActive()) {
|
|
return next();
|
|
}
|
|
|
|
const configMaxAge = config.get('caching:theme:maxAge');
|
|
|
|
express.static(themeUtils.getActive().path,
|
|
{maxAge: (configMaxAge || configMaxAge === 0) ? configMaxAge : constants.ONE_YEAR_MS}
|
|
)(req, res, next);
|
|
}
|
|
|
|
function staticTheme() {
|
|
return function blackListStatic(req, res, next) {
|
|
if (!isWhiteListedFile(req.path) && isBlackListedFileType(req.path)) {
|
|
return next();
|
|
}
|
|
|
|
return forwardToExpressStatic(req, res, next);
|
|
};
|
|
}
|
|
|
|
module.exports = staticTheme;
|