]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/controllers/plugins.ts
Refactor middleware helpers
[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'
8import { isTestInstance } from '../helpers/core-utils'
9
10const sendFileOptions = {
11 maxAge: '30 days',
12 immutable: !isTestInstance()
13}
14
15const pluginsRouter = express.Router()
16
17pluginsRouter.get('/plugins/global.css',
18 servePluginGlobalCSS
19)
20
21pluginsRouter.get('/plugins/:pluginName/:pluginVersion/static/:staticEndpoint(*)',
22 servePluginStaticDirectoryValidator(PluginType.PLUGIN),
23 servePluginStaticDirectory
24)
25
26pluginsRouter.get('/plugins/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)',
27 servePluginStaticDirectoryValidator(PluginType.PLUGIN),
28 servePluginClientScripts
29)
30
31pluginsRouter.get('/themes/:pluginName/:pluginVersion/static/:staticEndpoint(*)',
32 servePluginStaticDirectoryValidator(PluginType.THEME),
33 servePluginStaticDirectory
34)
35
36pluginsRouter.get('/themes/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)',
37 servePluginStaticDirectoryValidator(PluginType.THEME),
38 servePluginClientScripts
39)
40
41pluginsRouter.get('/themes/:themeName/:themeVersion/css/:staticEndpoint(*)',
42 serveThemeCSSValidator,
43 serveThemeCSSDirectory
44)
45
46// ---------------------------------------------------------------------------
47
48export {
49 pluginsRouter
50}
51
52// ---------------------------------------------------------------------------
53
54function 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
63function 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
78function 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
90function 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}