]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/themes.ts
Cleanup useless express validator messages
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / themes.ts
1 import express from 'express'
2 import { param } from 'express-validator'
3 import { HttpStatusCode } from '../../../shared/models/http/http-error-codes'
4 import { isSafePath } from '../../helpers/custom-validators/misc'
5 import { isPluginNameValid, isPluginVersionValid } from '../../helpers/custom-validators/plugins'
6 import { logger } from '../../helpers/logger'
7 import { PluginManager } from '../../lib/plugins/plugin-manager'
8 import { areValidationErrors } from './shared'
9
10 const serveThemeCSSValidator = [
11 param('themeName')
12 .custom(isPluginNameValid),
13 param('themeVersion')
14 .custom(isPluginVersionValid),
15 param('staticEndpoint')
16 .custom(isSafePath),
17
18 (req: express.Request, res: express.Response, next: express.NextFunction) => {
19 logger.debug('Checking serveThemeCSS parameters', { parameters: req.params })
20
21 if (areValidationErrors(req, res)) return
22
23 const theme = PluginManager.Instance.getRegisteredThemeByShortName(req.params.themeName)
24
25 if (!theme || theme.version !== req.params.themeVersion) {
26 return res.fail({
27 status: HttpStatusCode.NOT_FOUND_404,
28 message: 'No theme named ' + req.params.themeName + ' was found with version ' + req.params.themeVersion
29 })
30 }
31
32 if (theme.css.includes(req.params.staticEndpoint) === false) {
33 return res.fail({
34 status: HttpStatusCode.NOT_FOUND_404,
35 message: 'No static endpoint was found for this theme'
36 })
37 }
38
39 res.locals.registeredPlugin = theme
40
41 return next()
42 }
43 ]
44
45 // ---------------------------------------------------------------------------
46
47 export {
48 serveThemeCSSValidator
49 }