]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/plugins.ts
672299ee137060c98fcc6c1e98ce67e37dddaef3
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / plugins.ts
1 import * as express from 'express'
2 import { param } from 'express-validator/check'
3 import { logger } from '../../helpers/logger'
4 import { areValidationErrors } from './utils'
5 import { isPluginNameValid, isPluginVersionValid } from '../../helpers/custom-validators/plugins'
6 import { PluginManager } from '../../lib/plugins/plugin-manager'
7 import { isSafePath } from '../../helpers/custom-validators/misc'
8
9 const servePluginStaticDirectoryValidator = [
10 param('pluginName').custom(isPluginNameValid).withMessage('Should have a valid plugin name'),
11 param('pluginVersion').custom(isPluginVersionValid).withMessage('Should have a valid plugin version'),
12 param('staticEndpoint').custom(isSafePath).withMessage('Should have a valid static endpoint'),
13
14 (req: express.Request, res: express.Response, next: express.NextFunction) => {
15 logger.debug('Checking servePluginStaticDirectory parameters', { parameters: req.params })
16
17 if (areValidationErrors(req, res)) return
18
19 const plugin = PluginManager.Instance.getRegisteredPlugin(req.params.pluginName)
20
21 if (!plugin || plugin.version !== req.params.pluginVersion) {
22 return res.sendStatus(404)
23 }
24
25 res.locals.registeredPlugin = plugin
26
27 return next()
28 }
29 ]
30
31 // ---------------------------------------------------------------------------
32
33 export {
34 servePluginStaticDirectoryValidator
35 }