]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/plugins.ts
WIP plugins: update plugin
[github/Chocobozzz/PeerTube.git] / server / controllers / plugins.ts
1 import * as express from 'express'
2 import { PLUGIN_GLOBAL_CSS_PATH } from '../initializers/constants'
3 import { join } from 'path'
4 import { RegisteredPlugin } from '../lib/plugins/plugin-manager'
5 import { servePluginStaticDirectoryValidator } from '../middlewares/validators/plugins'
6 import { serveThemeCSSValidator } from '../middlewares/validators/themes'
7 import { PluginType } from '../../shared/models/plugins/plugin.type'
8
9 const pluginsRouter = express.Router()
10
11 pluginsRouter.get('/plugins/global.css',
12 servePluginGlobalCSS
13 )
14
15 pluginsRouter.get('/plugins/:pluginName/:pluginVersion/static/:staticEndpoint(*)',
16 servePluginStaticDirectoryValidator(PluginType.PLUGIN),
17 servePluginStaticDirectory
18 )
19
20 pluginsRouter.get('/plugins/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)',
21 servePluginStaticDirectoryValidator(PluginType.PLUGIN),
22 servePluginClientScripts
23 )
24
25 pluginsRouter.get('/themes/:pluginName/:pluginVersion/static/:staticEndpoint(*)',
26 servePluginStaticDirectoryValidator(PluginType.THEME),
27 servePluginStaticDirectory
28 )
29
30 pluginsRouter.get('/themes/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)',
31 servePluginStaticDirectoryValidator(PluginType.THEME),
32 servePluginClientScripts
33 )
34
35 pluginsRouter.get('/themes/:themeName/:themeVersion/css/:staticEndpoint(*)',
36 serveThemeCSSValidator,
37 serveThemeCSSDirectory
38 )
39
40 // ---------------------------------------------------------------------------
41
42 export {
43 pluginsRouter
44 }
45
46 // ---------------------------------------------------------------------------
47
48 function servePluginGlobalCSS (req: express.Request, res: express.Response) {
49 return res.sendFile(PLUGIN_GLOBAL_CSS_PATH)
50 }
51
52 function 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
67 function 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
79 function 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 }