diff options
Diffstat (limited to 'server/server/middlewares/validators/themes.ts')
-rw-r--r-- | server/server/middlewares/validators/themes.ts | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/server/server/middlewares/validators/themes.ts b/server/server/middlewares/validators/themes.ts new file mode 100644 index 000000000..ec7ce3b4f --- /dev/null +++ b/server/server/middlewares/validators/themes.ts | |||
@@ -0,0 +1,46 @@ | |||
1 | import express from 'express' | ||
2 | import { param } from 'express-validator' | ||
3 | import { HttpStatusCode } from '@peertube/peertube-models' | ||
4 | import { isSafePath } from '../../helpers/custom-validators/misc.js' | ||
5 | import { isPluginNameValid, isPluginStableOrUnstableVersionValid } from '../../helpers/custom-validators/plugins.js' | ||
6 | import { PluginManager } from '../../lib/plugins/plugin-manager.js' | ||
7 | import { areValidationErrors } from './shared/index.js' | ||
8 | |||
9 | const serveThemeCSSValidator = [ | ||
10 | param('themeName') | ||
11 | .custom(isPluginNameValid), | ||
12 | param('themeVersion') | ||
13 | .custom(isPluginStableOrUnstableVersionValid), | ||
14 | param('staticEndpoint') | ||
15 | .custom(isSafePath), | ||
16 | |||
17 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
18 | if (areValidationErrors(req, res)) return | ||
19 | |||
20 | const theme = PluginManager.Instance.getRegisteredThemeByShortName(req.params.themeName) | ||
21 | |||
22 | if (!theme || theme.version !== req.params.themeVersion) { | ||
23 | return res.fail({ | ||
24 | status: HttpStatusCode.NOT_FOUND_404, | ||
25 | message: 'No theme named ' + req.params.themeName + ' was found with version ' + req.params.themeVersion | ||
26 | }) | ||
27 | } | ||
28 | |||
29 | if (theme.css.includes(req.params.staticEndpoint) === false) { | ||
30 | return res.fail({ | ||
31 | status: HttpStatusCode.NOT_FOUND_404, | ||
32 | message: 'No static endpoint was found for this theme' | ||
33 | }) | ||
34 | } | ||
35 | |||
36 | res.locals.registeredPlugin = theme | ||
37 | |||
38 | return next() | ||
39 | } | ||
40 | ] | ||
41 | |||
42 | // --------------------------------------------------------------------------- | ||
43 | |||
44 | export { | ||
45 | serveThemeCSSValidator | ||
46 | } | ||