]>
Commit | Line | Data |
---|---|---|
345da516 C |
1 | import * as express from 'express' |
2 | import { PLUGIN_GLOBAL_CSS_PATH } from '../initializers/constants' | |
b5f919ac | 3 | import { join } from 'path' |
d75db01f | 4 | import { PluginManager, RegisteredPlugin } from '../lib/plugins/plugin-manager' |
5e2b2e27 | 5 | import { getPluginValidator, pluginStaticDirectoryValidator } from '../middlewares/validators/plugins' |
b5f919ac C |
6 | import { serveThemeCSSValidator } from '../middlewares/validators/themes' |
7 | import { PluginType } from '../../shared/models/plugins/plugin.type' | |
a8b666e9 | 8 | import { isTestInstance } from '../helpers/core-utils' |
d75db01f | 9 | import { getCompleteLocale, is18nLocale } from '../../shared/models/i18n' |
a8b666e9 C |
10 | |
11 | const sendFileOptions = { | |
12 | maxAge: '30 days', | |
13 | immutable: !isTestInstance() | |
14 | } | |
345da516 C |
15 | |
16 | const pluginsRouter = express.Router() | |
17 | ||
b5f919ac | 18 | pluginsRouter.get('/plugins/global.css', |
2c053942 | 19 | servePluginGlobalCSS |
345da516 C |
20 | ) |
21 | ||
d75db01f C |
22 | pluginsRouter.get('/plugins/translations/:locale.json', |
23 | getPluginTranslations | |
24 | ) | |
25 | ||
b5f919ac | 26 | pluginsRouter.get('/plugins/:pluginName/:pluginVersion/static/:staticEndpoint(*)', |
5e2b2e27 C |
27 | getPluginValidator(PluginType.PLUGIN), |
28 | pluginStaticDirectoryValidator, | |
345da516 C |
29 | servePluginStaticDirectory |
30 | ) | |
31 | ||
b5f919ac | 32 | pluginsRouter.get('/plugins/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)', |
5e2b2e27 C |
33 | getPluginValidator(PluginType.PLUGIN), |
34 | pluginStaticDirectoryValidator, | |
345da516 C |
35 | servePluginClientScripts |
36 | ) | |
37 | ||
5e2b2e27 C |
38 | pluginsRouter.use('/plugins/:pluginName/router', |
39 | getPluginValidator(PluginType.PLUGIN, false), | |
40 | servePluginCustomRoutes | |
41 | ) | |
42 | ||
43 | pluginsRouter.use('/plugins/:pluginName/:pluginVersion/router', | |
44 | getPluginValidator(PluginType.PLUGIN), | |
45 | servePluginCustomRoutes | |
46 | ) | |
47 | ||
b5f919ac | 48 | pluginsRouter.get('/themes/:pluginName/:pluginVersion/static/:staticEndpoint(*)', |
5e2b2e27 C |
49 | getPluginValidator(PluginType.THEME), |
50 | pluginStaticDirectoryValidator, | |
b5f919ac C |
51 | servePluginStaticDirectory |
52 | ) | |
53 | ||
54 | pluginsRouter.get('/themes/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)', | |
5e2b2e27 C |
55 | getPluginValidator(PluginType.THEME), |
56 | pluginStaticDirectoryValidator, | |
b5f919ac C |
57 | servePluginClientScripts |
58 | ) | |
59 | ||
60 | pluginsRouter.get('/themes/:themeName/:themeVersion/css/:staticEndpoint(*)', | |
61 | serveThemeCSSValidator, | |
62 | serveThemeCSSDirectory | |
63 | ) | |
64 | ||
345da516 C |
65 | // --------------------------------------------------------------------------- |
66 | ||
67 | export { | |
68 | pluginsRouter | |
69 | } | |
70 | ||
71 | // --------------------------------------------------------------------------- | |
72 | ||
2c053942 | 73 | function 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 |
82 | function 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 |
95 | function 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 |
108 | function 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 |
117 | function 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 | |
127 | function 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 | } |