]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/controllers/plugins.ts
Add plugin API tests
[github/Chocobozzz/PeerTube.git] / server / controllers / plugins.ts
... / ...
CommitLineData
1import * as express from 'express'
2import { PLUGIN_GLOBAL_CSS_PATH } from '../initializers/constants'
3import { join } from 'path'
4import { RegisteredPlugin } from '../lib/plugins/plugin-manager'
5import { servePluginStaticDirectoryValidator } from '../middlewares/validators/plugins'
6import { serveThemeCSSValidator } from '../middlewares/validators/themes'
7import { PluginType } from '../../shared/models/plugins/plugin.type'
8
9const pluginsRouter = express.Router()
10
11pluginsRouter.get('/plugins/global.css',
12 servePluginGlobalCSS
13)
14
15pluginsRouter.get('/plugins/:pluginName/:pluginVersion/static/:staticEndpoint(*)',
16 servePluginStaticDirectoryValidator(PluginType.PLUGIN),
17 servePluginStaticDirectory
18)
19
20pluginsRouter.get('/plugins/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)',
21 servePluginStaticDirectoryValidator(PluginType.PLUGIN),
22 servePluginClientScripts
23)
24
25pluginsRouter.get('/themes/:pluginName/:pluginVersion/static/:staticEndpoint(*)',
26 servePluginStaticDirectoryValidator(PluginType.THEME),
27 servePluginStaticDirectory
28)
29
30pluginsRouter.get('/themes/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)',
31 servePluginStaticDirectoryValidator(PluginType.THEME),
32 servePluginClientScripts
33)
34
35pluginsRouter.get('/themes/:themeName/:themeVersion/css/:staticEndpoint(*)',
36 serveThemeCSSValidator,
37 serveThemeCSSDirectory
38)
39
40// ---------------------------------------------------------------------------
41
42export {
43 pluginsRouter
44}
45
46// ---------------------------------------------------------------------------
47
48function servePluginGlobalCSS (req: express.Request, res: express.Response) {
49 return res.sendFile(PLUGIN_GLOBAL_CSS_PATH)
50}
51
52function servePluginStaticDirectory (req: express.Request, res: express.Response) {
53 const plugin: RegisteredPlugin = res.locals.registeredPlugin
54 const staticEndpoint = req.params.staticEndpoint
55
56 const [ directory, ...file ] = staticEndpoint.split('/')
57
58 const staticPath = plugin.staticDirs[directory]
59 if (!staticPath) {
60 return res.sendStatus(404)
61 }
62
63 const filepath = file.join('/')
64 return res.sendFile(join(plugin.path, staticPath, filepath))
65}
66
67function servePluginClientScripts (req: express.Request, res: express.Response) {
68 const plugin: RegisteredPlugin = res.locals.registeredPlugin
69 const staticEndpoint = req.params.staticEndpoint
70
71 const file = plugin.clientScripts[staticEndpoint]
72 if (!file) {
73 return res.sendStatus(404)
74 }
75
76 return res.sendFile(join(plugin.path, staticEndpoint))
77}
78
79function serveThemeCSSDirectory (req: express.Request, res: express.Response) {
80 const plugin: RegisteredPlugin = res.locals.registeredPlugin
81 const staticEndpoint = req.params.staticEndpoint
82
83 if (plugin.css.includes(staticEndpoint) === false) {
84 return res.sendStatus(404)
85 }
86
87 return res.sendFile(join(plugin.path, staticEndpoint))
88}