aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2020-04-10 15:07:54 +0200
committerChocobozzz <me@florianbigard.com>2020-04-10 15:23:25 +0200
commit5e2b2e2775421cd98286d6e2f75cf38aae7a212c (patch)
treed92e32824d83cecbe5e90206738f393b47e55754 /server/controllers
parent9afa0901f11c321e071c42ba3c814a3af4843c55 (diff)
downloadPeerTube-5e2b2e2775421cd98286d6e2f75cf38aae7a212c.tar.gz
PeerTube-5e2b2e2775421cd98286d6e2f75cf38aae7a212c.tar.zst
PeerTube-5e2b2e2775421cd98286d6e2f75cf38aae7a212c.zip
Add ability for plugins to add custom routes
Diffstat (limited to 'server/controllers')
-rw-r--r--server/controllers/plugins.ts41
1 files changed, 30 insertions, 11 deletions
diff --git a/server/controllers/plugins.ts b/server/controllers/plugins.ts
index 1caee9a29..1fc49b646 100644
--- a/server/controllers/plugins.ts
+++ b/server/controllers/plugins.ts
@@ -2,7 +2,7 @@ import * as express from 'express'
2import { PLUGIN_GLOBAL_CSS_PATH } from '../initializers/constants' 2import { PLUGIN_GLOBAL_CSS_PATH } from '../initializers/constants'
3import { join } from 'path' 3import { join } from 'path'
4import { PluginManager, RegisteredPlugin } from '../lib/plugins/plugin-manager' 4import { PluginManager, RegisteredPlugin } from '../lib/plugins/plugin-manager'
5import { servePluginStaticDirectoryValidator } from '../middlewares/validators/plugins' 5import { getPluginValidator, pluginStaticDirectoryValidator } from '../middlewares/validators/plugins'
6import { serveThemeCSSValidator } from '../middlewares/validators/themes' 6import { serveThemeCSSValidator } from '../middlewares/validators/themes'
7import { PluginType } from '../../shared/models/plugins/plugin.type' 7import { PluginType } from '../../shared/models/plugins/plugin.type'
8import { isTestInstance } from '../helpers/core-utils' 8import { isTestInstance } from '../helpers/core-utils'
@@ -24,22 +24,36 @@ pluginsRouter.get('/plugins/translations/:locale.json',
24) 24)
25 25
26pluginsRouter.get('/plugins/:pluginName/:pluginVersion/static/:staticEndpoint(*)', 26pluginsRouter.get('/plugins/:pluginName/:pluginVersion/static/:staticEndpoint(*)',
27 servePluginStaticDirectoryValidator(PluginType.PLUGIN), 27 getPluginValidator(PluginType.PLUGIN),
28 pluginStaticDirectoryValidator,
28 servePluginStaticDirectory 29 servePluginStaticDirectory
29) 30)
30 31
31pluginsRouter.get('/plugins/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)', 32pluginsRouter.get('/plugins/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)',
32 servePluginStaticDirectoryValidator(PluginType.PLUGIN), 33 getPluginValidator(PluginType.PLUGIN),
34 pluginStaticDirectoryValidator,
33 servePluginClientScripts 35 servePluginClientScripts
34) 36)
35 37
38pluginsRouter.use('/plugins/:pluginName/router',
39 getPluginValidator(PluginType.PLUGIN, false),
40 servePluginCustomRoutes
41)
42
43pluginsRouter.use('/plugins/:pluginName/:pluginVersion/router',
44 getPluginValidator(PluginType.PLUGIN),
45 servePluginCustomRoutes
46)
47
36pluginsRouter.get('/themes/:pluginName/:pluginVersion/static/:staticEndpoint(*)', 48pluginsRouter.get('/themes/:pluginName/:pluginVersion/static/:staticEndpoint(*)',
37 servePluginStaticDirectoryValidator(PluginType.THEME), 49 getPluginValidator(PluginType.THEME),
50 pluginStaticDirectoryValidator,
38 servePluginStaticDirectory 51 servePluginStaticDirectory
39) 52)
40 53
41pluginsRouter.get('/themes/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)', 54pluginsRouter.get('/themes/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)',
42 servePluginStaticDirectoryValidator(PluginType.THEME), 55 getPluginValidator(PluginType.THEME),
56 pluginStaticDirectoryValidator,
43 servePluginClientScripts 57 servePluginClientScripts
44) 58)
45 59
@@ -85,22 +99,27 @@ function servePluginStaticDirectory (req: express.Request, res: express.Response
85 const [ directory, ...file ] = staticEndpoint.split('/') 99 const [ directory, ...file ] = staticEndpoint.split('/')
86 100
87 const staticPath = plugin.staticDirs[directory] 101 const staticPath = plugin.staticDirs[directory]
88 if (!staticPath) { 102 if (!staticPath) return res.sendStatus(404)
89 return res.sendStatus(404)
90 }
91 103
92 const filepath = file.join('/') 104 const filepath = file.join('/')
93 return res.sendFile(join(plugin.path, staticPath, filepath), sendFileOptions) 105 return res.sendFile(join(plugin.path, staticPath, filepath), sendFileOptions)
94} 106}
95 107
108function servePluginCustomRoutes (req: express.Request, res: express.Response, next: express.NextFunction) {
109 const plugin: RegisteredPlugin = res.locals.registeredPlugin
110 const router = PluginManager.Instance.getRouter(plugin.npmName)
111
112 if (!router) return res.sendStatus(404)
113
114 return router(req, res, next)
115}
116
96function servePluginClientScripts (req: express.Request, res: express.Response) { 117function servePluginClientScripts (req: express.Request, res: express.Response) {
97 const plugin: RegisteredPlugin = res.locals.registeredPlugin 118 const plugin: RegisteredPlugin = res.locals.registeredPlugin
98 const staticEndpoint = req.params.staticEndpoint 119 const staticEndpoint = req.params.staticEndpoint
99 120
100 const file = plugin.clientScripts[staticEndpoint] 121 const file = plugin.clientScripts[staticEndpoint]
101 if (!file) { 122 if (!file) return res.sendStatus(404)
102 return res.sendStatus(404)
103 }
104 123
105 return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions) 124 return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions)
106} 125}