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