aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators/themes.ts
blob: 080b3e096303fc12c8d2c73e0039b8465063eac2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import express from 'express'
import { param } from 'express-validator'
import { HttpStatusCode } from '../../../shared/models/http/http-error-codes'
import { isSafePath } from '../../helpers/custom-validators/misc'
import { isPluginNameValid, isPluginStableOrUnstableVersionValid } from '../../helpers/custom-validators/plugins'
import { PluginManager } from '../../lib/plugins/plugin-manager'
import { areValidationErrors } from './shared'

const serveThemeCSSValidator = [
  param('themeName')
    .custom(isPluginNameValid),
  param('themeVersion')
    .custom(isPluginStableOrUnstableVersionValid),
  param('staticEndpoint')
    .custom(isSafePath),

  (req: express.Request, res: express.Response, next: express.NextFunction) => {
    if (areValidationErrors(req, res)) return

    const theme = PluginManager.Instance.getRegisteredThemeByShortName(req.params.themeName)

    if (!theme || theme.version !== req.params.themeVersion) {
      return res.fail({
        status: HttpStatusCode.NOT_FOUND_404,
        message: 'No theme named ' + req.params.themeName + ' was found with version ' + req.params.themeVersion
      })
    }

    if (theme.css.includes(req.params.staticEndpoint) === false) {
      return res.fail({
        status: HttpStatusCode.NOT_FOUND_404,
        message: 'No static endpoint was found for this theme'
      })
    }

    res.locals.registeredPlugin = theme

    return next()
  }
]

// ---------------------------------------------------------------------------

export {
  serveThemeCSSValidator
}