]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/plugins.ts
Move controller in dedicated functions
[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 import { isTestInstance } from '../helpers/core-utils'
9
10 const sendFileOptions = {
11 maxAge: '30 days',
12 immutable: !isTestInstance()
13 }
14
15 const pluginsRouter = express.Router()
16
17 pluginsRouter.get('/plugins/global.css',
18 servePluginGlobalCSS
19 )
20
21 pluginsRouter.get('/plugins/:pluginName/:pluginVersion/static/:staticEndpoint(*)',
22 servePluginStaticDirectoryValidator(PluginType.PLUGIN),
23 servePluginStaticDirectory
24 )
25
26 pluginsRouter.get('/plugins/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)',
27 servePluginStaticDirectoryValidator(PluginType.PLUGIN),
28 servePluginClientScripts
29 )
30
31 pluginsRouter.get('/themes/:pluginName/:pluginVersion/static/:staticEndpoint(*)',
32 servePluginStaticDirectoryValidator(PluginType.THEME),
33 servePluginStaticDirectory
34 )
35
36 pluginsRouter.get('/themes/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)',
37 servePluginStaticDirectoryValidator(PluginType.THEME),
38 servePluginClientScripts
39 )
40
41 pluginsRouter.get('/themes/:themeName/:themeVersion/css/:staticEndpoint(*)',
42 serveThemeCSSValidator,
43 serveThemeCSSDirectory
44 )
45
46 // ---------------------------------------------------------------------------
47
48 export {
49 pluginsRouter
50 }
51
52 // ---------------------------------------------------------------------------
53
54 function servePluginGlobalCSS (req: express.Request, res: express.Response) {
55 // Only cache requests that have a ?hash=... query param
56 const globalCSSOptions = req.query.hash
57 ? sendFileOptions
58 : {}
59
60 return res.sendFile(PLUGIN_GLOBAL_CSS_PATH, globalCSSOptions)
61 }
62
63 function servePluginStaticDirectory (req: express.Request, res: express.Response) {
64 const plugin: RegisteredPlugin = res.locals.registeredPlugin
65 const staticEndpoint = req.params.staticEndpoint
66
67 const [ directory, ...file ] = staticEndpoint.split('/')
68
69 const staticPath = plugin.staticDirs[directory]
70 if (!staticPath) {
71 return res.sendStatus(404)
72 }
73
74 const filepath = file.join('/')
75 return res.sendFile(join(plugin.path, staticPath, filepath), sendFileOptions)
76 }
77
78 function servePluginClientScripts (req: express.Request, res: express.Response) {
79 const plugin: RegisteredPlugin = res.locals.registeredPlugin
80 const staticEndpoint = req.params.staticEndpoint
81
82 const file = plugin.clientScripts[staticEndpoint]
83 if (!file) {
84 return res.sendStatus(404)
85 }
86
87 return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions)
88 }
89
90 function serveThemeCSSDirectory (req: express.Request, res: express.Response) {
91 const plugin: RegisteredPlugin = res.locals.registeredPlugin
92 const staticEndpoint = req.params.staticEndpoint
93
94 if (plugin.css.includes(staticEndpoint) === false) {
95 return res.sendStatus(404)
96 }
97
98 return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions)
99 }