]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/plugins.ts
Merge branch 'master' into develop
[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'
345da516 5import { servePluginStaticDirectoryValidator } 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
C
26pluginsRouter.get('/plugins/:pluginName/:pluginVersion/static/:staticEndpoint(*)',
27 servePluginStaticDirectoryValidator(PluginType.PLUGIN),
345da516
C
28 servePluginStaticDirectory
29)
30
b5f919ac
C
31pluginsRouter.get('/plugins/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)',
32 servePluginStaticDirectoryValidator(PluginType.PLUGIN),
345da516
C
33 servePluginClientScripts
34)
35
b5f919ac
C
36pluginsRouter.get('/themes/:pluginName/:pluginVersion/static/:staticEndpoint(*)',
37 servePluginStaticDirectoryValidator(PluginType.THEME),
38 servePluginStaticDirectory
39)
40
41pluginsRouter.get('/themes/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)',
42 servePluginStaticDirectoryValidator(PluginType.THEME),
43 servePluginClientScripts
44)
45
46pluginsRouter.get('/themes/:themeName/:themeVersion/css/:staticEndpoint(*)',
47 serveThemeCSSValidator,
48 serveThemeCSSDirectory
49)
50
345da516
C
51// ---------------------------------------------------------------------------
52
53export {
54 pluginsRouter
55}
56
57// ---------------------------------------------------------------------------
58
2c053942 59function servePluginGlobalCSS (req: express.Request, res: express.Response) {
a8b666e9
C
60 // Only cache requests that have a ?hash=... query param
61 const globalCSSOptions = req.query.hash
62 ? sendFileOptions
63 : {}
64
65 return res.sendFile(PLUGIN_GLOBAL_CSS_PATH, globalCSSOptions)
2c053942
C
66}
67
d75db01f
C
68function getPluginTranslations (req: express.Request, res: express.Response) {
69 const locale = req.params.locale
70
71 if (is18nLocale(locale)) {
72 const completeLocale = getCompleteLocale(locale)
73 const json = PluginManager.Instance.getTranslations(completeLocale)
74
75 return res.json(json)
76 }
77
78 return res.sendStatus(404)
79}
80
345da516
C
81function servePluginStaticDirectory (req: express.Request, res: express.Response) {
82 const plugin: RegisteredPlugin = res.locals.registeredPlugin
83 const staticEndpoint = req.params.staticEndpoint
84
2c053942
C
85 const [ directory, ...file ] = staticEndpoint.split('/')
86
87 const staticPath = plugin.staticDirs[directory]
345da516
C
88 if (!staticPath) {
89 return res.sendStatus(404)
90 }
91
2c053942 92 const filepath = file.join('/')
a8b666e9 93 return res.sendFile(join(plugin.path, staticPath, filepath), sendFileOptions)
345da516
C
94}
95
96function servePluginClientScripts (req: express.Request, res: express.Response) {
97 const plugin: RegisteredPlugin = res.locals.registeredPlugin
98 const staticEndpoint = req.params.staticEndpoint
99
2c053942
C
100 const file = plugin.clientScripts[staticEndpoint]
101 if (!file) {
102 return res.sendStatus(404)
103 }
104
a8b666e9 105 return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions)
345da516 106}
b5f919ac
C
107
108function serveThemeCSSDirectory (req: express.Request, res: express.Response) {
109 const plugin: RegisteredPlugin = res.locals.registeredPlugin
110 const staticEndpoint = req.params.staticEndpoint
111
112 if (plugin.css.includes(staticEndpoint) === false) {
113 return res.sendStatus(404)
114 }
115
a8b666e9 116 return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions)
b5f919ac 117}