2019-07-05 13:54:32 +02:00
|
|
|
import * as express from 'express'
|
2019-07-25 16:23:44 +02:00
|
|
|
import { param } from 'express-validator'
|
2021-06-03 17:33:44 +02:00
|
|
|
import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
|
|
|
|
import { isSafePath } from '../../helpers/custom-validators/misc'
|
2019-07-05 13:54:32 +02:00
|
|
|
import { isPluginNameValid, isPluginVersionValid } from '../../helpers/custom-validators/plugins'
|
2021-06-03 17:33:44 +02:00
|
|
|
import { logger } from '../../helpers/logger'
|
2019-07-05 13:54:32 +02:00
|
|
|
import { PluginManager } from '../../lib/plugins/plugin-manager'
|
2021-06-03 17:33:44 +02:00
|
|
|
import { areValidationErrors } from './shared'
|
2019-07-05 13:54:32 +02:00
|
|
|
|
|
|
|
const serveThemeCSSValidator = [
|
|
|
|
param('themeName').custom(isPluginNameValid).withMessage('Should have a valid theme name'),
|
|
|
|
param('themeVersion').custom(isPluginVersionValid).withMessage('Should have a valid theme version'),
|
|
|
|
param('staticEndpoint').custom(isSafePath).withMessage('Should have a valid static endpoint'),
|
|
|
|
|
|
|
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
logger.debug('Checking serveThemeCSS parameters', { parameters: req.params })
|
|
|
|
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
|
2020-04-23 11:36:50 +02:00
|
|
|
const theme = PluginManager.Instance.getRegisteredThemeByShortName(req.params.themeName)
|
2019-07-05 13:54:32 +02:00
|
|
|
|
|
|
|
if (!theme || theme.version !== req.params.themeVersion) {
|
2021-06-01 01:36:53 +02:00
|
|
|
return res.fail({
|
|
|
|
status: HttpStatusCode.NOT_FOUND_404,
|
|
|
|
message: 'No theme named ' + req.params.themeName + ' was found with version ' + req.params.themeVersion
|
|
|
|
})
|
2019-07-05 13:54:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (theme.css.includes(req.params.staticEndpoint) === false) {
|
2021-06-01 01:36:53 +02:00
|
|
|
return res.fail({
|
|
|
|
status: HttpStatusCode.NOT_FOUND_404,
|
|
|
|
message: 'No static endpoint was found for this theme'
|
|
|
|
})
|
2019-07-05 13:54:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
res.locals.registeredPlugin = theme
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
serveThemeCSSValidator
|
|
|
|
}
|