]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/plugins.ts
Check auth plugin result
[github/Chocobozzz/PeerTube.git] / server / controllers / plugins.ts
CommitLineData
345da516
C
1import * as express from 'express'
2import { PLUGIN_GLOBAL_CSS_PATH } from '../initializers/constants'
b5f919ac 3import { join } from 'path'
d75db01f 4import { PluginManager, RegisteredPlugin } from '../lib/plugins/plugin-manager'
5e2b2e27 5import { getPluginValidator, pluginStaticDirectoryValidator } from '../middlewares/validators/plugins'
b5f919ac
C
6import { serveThemeCSSValidator } from '../middlewares/validators/themes'
7import { PluginType } from '../../shared/models/plugins/plugin.type'
a8b666e9 8import { isTestInstance } from '../helpers/core-utils'
d75db01f 9import { getCompleteLocale, is18nLocale } from '../../shared/models/i18n'
a8b666e9
C
10
11const sendFileOptions = {
12 maxAge: '30 days',
13 immutable: !isTestInstance()
14}
345da516
C
15
16const pluginsRouter = express.Router()
17
b5f919ac 18pluginsRouter.get('/plugins/global.css',
2c053942 19 servePluginGlobalCSS
345da516
C
20)
21
d75db01f
C
22pluginsRouter.get('/plugins/translations/:locale.json',
23 getPluginTranslations
24)
25
b5f919ac 26pluginsRouter.get('/plugins/:pluginName/:pluginVersion/static/:staticEndpoint(*)',
5e2b2e27
C
27 getPluginValidator(PluginType.PLUGIN),
28 pluginStaticDirectoryValidator,
345da516
C
29 servePluginStaticDirectory
30)
31
b5f919ac 32pluginsRouter.get('/plugins/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)',
5e2b2e27
C
33 getPluginValidator(PluginType.PLUGIN),
34 pluginStaticDirectoryValidator,
345da516
C
35 servePluginClientScripts
36)
37
5e2b2e27
C
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
b5f919ac 48pluginsRouter.get('/themes/:pluginName/:pluginVersion/static/:staticEndpoint(*)',
5e2b2e27
C
49 getPluginValidator(PluginType.THEME),
50 pluginStaticDirectoryValidator,
b5f919ac
C
51 servePluginStaticDirectory
52)
53
54pluginsRouter.get('/themes/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)',
5e2b2e27
C
55 getPluginValidator(PluginType.THEME),
56 pluginStaticDirectoryValidator,
b5f919ac
C
57 servePluginClientScripts
58)
59
60pluginsRouter.get('/themes/:themeName/:themeVersion/css/:staticEndpoint(*)',
61 serveThemeCSSValidator,
62 serveThemeCSSDirectory
63)
64
345da516
C
65// ---------------------------------------------------------------------------
66
67export {
68 pluginsRouter
69}
70
71// ---------------------------------------------------------------------------
72
2c053942 73function servePluginGlobalCSS (req: express.Request, res: express.Response) {
a8b666e9
C
74 // Only cache requests that have a ?hash=... query param
75 const globalCSSOptions = req.query.hash
76 ? sendFileOptions
77 : {}
78
79 return res.sendFile(PLUGIN_GLOBAL_CSS_PATH, globalCSSOptions)
2c053942
C
80}
81
d75db01f
C
82function getPluginTranslations (req: express.Request, res: express.Response) {
83 const locale = req.params.locale
84
85 if (is18nLocale(locale)) {
86 const completeLocale = getCompleteLocale(locale)
87 const json = PluginManager.Instance.getTranslations(completeLocale)
88
89 return res.json(json)
90 }
91
92 return res.sendStatus(404)
93}
94
345da516
C
95function servePluginStaticDirectory (req: express.Request, res: express.Response) {
96 const plugin: RegisteredPlugin = res.locals.registeredPlugin
97 const staticEndpoint = req.params.staticEndpoint
98
2c053942
C
99 const [ directory, ...file ] = staticEndpoint.split('/')
100
101 const staticPath = plugin.staticDirs[directory]
5e2b2e27 102 if (!staticPath) return res.sendStatus(404)
345da516 103
2c053942 104 const filepath = file.join('/')
a8b666e9 105 return res.sendFile(join(plugin.path, staticPath, filepath), sendFileOptions)
345da516
C
106}
107
5e2b2e27
C
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
345da516
C
117function servePluginClientScripts (req: express.Request, res: express.Response) {
118 const plugin: RegisteredPlugin = res.locals.registeredPlugin
119 const staticEndpoint = req.params.staticEndpoint
120
2c053942 121 const file = plugin.clientScripts[staticEndpoint]
5e2b2e27 122 if (!file) return res.sendStatus(404)
2c053942 123
a8b666e9 124 return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions)
345da516 125}
b5f919ac
C
126
127function serveThemeCSSDirectory (req: express.Request, res: express.Response) {
128 const plugin: RegisteredPlugin = res.locals.registeredPlugin
129 const staticEndpoint = req.params.staticEndpoint
130
131 if (plugin.css.includes(staticEndpoint) === false) {
132 return res.sendStatus(404)
133 }
134
a8b666e9 135 return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions)
b5f919ac 136}