]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/plugins.ts
18c6613e25ac798a362785d9d9318111e0c821ae
[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 { PluginManager, RegisteredPlugin } from '../lib/plugins/plugin-manager'
5 import { getPluginValidator, pluginStaticDirectoryValidator, getExternalAuthValidator } from '../middlewares/validators/plugins'
6 import { serveThemeCSSValidator } from '../middlewares/validators/themes'
7 import { HttpStatusCode } from '../../shared/core-utils/miscs/http-error-codes'
8 import { getCompleteLocale, is18nLocale } from '../../shared/core-utils/i18n'
9 import { PluginType } from '../../shared/models/plugins/plugin.type'
10 import { isTestInstance } from '../helpers/core-utils'
11 import { logger } from '@server/helpers/logger'
12
13 const sendFileOptions = {
14 maxAge: '30 days',
15 immutable: !isTestInstance()
16 }
17
18 const pluginsRouter = express.Router()
19
20 pluginsRouter.get('/plugins/global.css',
21 servePluginGlobalCSS
22 )
23
24 pluginsRouter.get('/plugins/translations/:locale.json',
25 getPluginTranslations
26 )
27
28 pluginsRouter.get('/plugins/:pluginName/:pluginVersion/auth/:authName',
29 getPluginValidator(PluginType.PLUGIN),
30 getExternalAuthValidator,
31 handleAuthInPlugin
32 )
33
34 pluginsRouter.get('/plugins/:pluginName/:pluginVersion/static/:staticEndpoint(*)',
35 getPluginValidator(PluginType.PLUGIN),
36 pluginStaticDirectoryValidator,
37 servePluginStaticDirectory
38 )
39
40 pluginsRouter.get('/plugins/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)',
41 getPluginValidator(PluginType.PLUGIN),
42 pluginStaticDirectoryValidator,
43 servePluginClientScripts
44 )
45
46 pluginsRouter.use('/plugins/:pluginName/router',
47 getPluginValidator(PluginType.PLUGIN, false),
48 servePluginCustomRoutes
49 )
50
51 pluginsRouter.use('/plugins/:pluginName/:pluginVersion/router',
52 getPluginValidator(PluginType.PLUGIN),
53 servePluginCustomRoutes
54 )
55
56 pluginsRouter.get('/themes/:pluginName/:pluginVersion/static/:staticEndpoint(*)',
57 getPluginValidator(PluginType.THEME),
58 pluginStaticDirectoryValidator,
59 servePluginStaticDirectory
60 )
61
62 pluginsRouter.get('/themes/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)',
63 getPluginValidator(PluginType.THEME),
64 pluginStaticDirectoryValidator,
65 servePluginClientScripts
66 )
67
68 pluginsRouter.get('/themes/:themeName/:themeVersion/css/:staticEndpoint(*)',
69 serveThemeCSSValidator,
70 serveThemeCSSDirectory
71 )
72
73 // ---------------------------------------------------------------------------
74
75 export {
76 pluginsRouter
77 }
78
79 // ---------------------------------------------------------------------------
80
81 function servePluginGlobalCSS (req: express.Request, res: express.Response) {
82 // Only cache requests that have a ?hash=... query param
83 const globalCSSOptions = req.query.hash
84 ? sendFileOptions
85 : {}
86
87 return res.sendFile(PLUGIN_GLOBAL_CSS_PATH, globalCSSOptions)
88 }
89
90 function getPluginTranslations (req: express.Request, res: express.Response) {
91 const locale = req.params.locale
92
93 if (is18nLocale(locale)) {
94 const completeLocale = getCompleteLocale(locale)
95 const json = PluginManager.Instance.getTranslations(completeLocale)
96
97 return res.json(json)
98 }
99
100 return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
101 }
102
103 function servePluginStaticDirectory (req: express.Request, res: express.Response) {
104 const plugin: RegisteredPlugin = res.locals.registeredPlugin
105 const staticEndpoint = req.params.staticEndpoint
106
107 const [ directory, ...file ] = staticEndpoint.split('/')
108
109 const staticPath = plugin.staticDirs[directory]
110 if (!staticPath) return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
111
112 const filepath = file.join('/')
113 return res.sendFile(join(plugin.path, staticPath, filepath), sendFileOptions)
114 }
115
116 function servePluginCustomRoutes (req: express.Request, res: express.Response, next: express.NextFunction) {
117 const plugin: RegisteredPlugin = res.locals.registeredPlugin
118 const router = PluginManager.Instance.getRouter(plugin.npmName)
119
120 if (!router) return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
121
122 return router(req, res, next)
123 }
124
125 function servePluginClientScripts (req: express.Request, res: express.Response) {
126 const plugin: RegisteredPlugin = res.locals.registeredPlugin
127 const staticEndpoint = req.params.staticEndpoint
128
129 const file = plugin.clientScripts[staticEndpoint]
130 if (!file) return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
131
132 return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions)
133 }
134
135 function serveThemeCSSDirectory (req: express.Request, res: express.Response) {
136 const plugin: RegisteredPlugin = res.locals.registeredPlugin
137 const staticEndpoint = req.params.staticEndpoint
138
139 if (plugin.css.includes(staticEndpoint) === false) {
140 return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
141 }
142
143 return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions)
144 }
145
146 function handleAuthInPlugin (req: express.Request, res: express.Response) {
147 const authOptions = res.locals.externalAuth
148
149 try {
150 logger.debug('Forwarding auth plugin request in %s of plugin %s.', authOptions.authName, res.locals.registeredPlugin.npmName)
151 authOptions.onAuthRequest(req, res)
152 } catch (err) {
153 logger.error('Forward request error in auth %s of plugin %s.', authOptions.authName, res.locals.registeredPlugin.npmName, { err })
154 }
155 }