]>
Commit | Line | Data |
---|---|---|
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 } from '../middlewares/validators/plugins' | |
6 | import { serveThemeCSSValidator } from '../middlewares/validators/themes' | |
7 | import { PluginType } from '../../shared/models/plugins/plugin.type' | |
8 | import { isTestInstance } from '../helpers/core-utils' | |
9 | import { getCompleteLocale, is18nLocale } from '../../shared/models/i18n' | |
10 | ||
11 | const sendFileOptions = { | |
12 | maxAge: '30 days', | |
13 | immutable: !isTestInstance() | |
14 | } | |
15 | ||
16 | const pluginsRouter = express.Router() | |
17 | ||
18 | pluginsRouter.get('/plugins/global.css', | |
19 | servePluginGlobalCSS | |
20 | ) | |
21 | ||
22 | pluginsRouter.get('/plugins/translations/:locale.json', | |
23 | getPluginTranslations | |
24 | ) | |
25 | ||
26 | pluginsRouter.get('/plugins/:pluginName/:pluginVersion/static/:staticEndpoint(*)', | |
27 | getPluginValidator(PluginType.PLUGIN), | |
28 | pluginStaticDirectoryValidator, | |
29 | servePluginStaticDirectory | |
30 | ) | |
31 | ||
32 | pluginsRouter.get('/plugins/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)', | |
33 | getPluginValidator(PluginType.PLUGIN), | |
34 | pluginStaticDirectoryValidator, | |
35 | servePluginClientScripts | |
36 | ) | |
37 | ||
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 | ||
48 | pluginsRouter.get('/themes/:pluginName/:pluginVersion/static/:staticEndpoint(*)', | |
49 | getPluginValidator(PluginType.THEME), | |
50 | pluginStaticDirectoryValidator, | |
51 | servePluginStaticDirectory | |
52 | ) | |
53 | ||
54 | pluginsRouter.get('/themes/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)', | |
55 | getPluginValidator(PluginType.THEME), | |
56 | pluginStaticDirectoryValidator, | |
57 | servePluginClientScripts | |
58 | ) | |
59 | ||
60 | pluginsRouter.get('/themes/:themeName/:themeVersion/css/:staticEndpoint(*)', | |
61 | serveThemeCSSValidator, | |
62 | serveThemeCSSDirectory | |
63 | ) | |
64 | ||
65 | // --------------------------------------------------------------------------- | |
66 | ||
67 | export { | |
68 | pluginsRouter | |
69 | } | |
70 | ||
71 | // --------------------------------------------------------------------------- | |
72 | ||
73 | function servePluginGlobalCSS (req: express.Request, res: express.Response) { | |
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) | |
80 | } | |
81 | ||
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 | ||
95 | function servePluginStaticDirectory (req: express.Request, res: express.Response) { | |
96 | const plugin: RegisteredPlugin = res.locals.registeredPlugin | |
97 | const staticEndpoint = req.params.staticEndpoint | |
98 | ||
99 | const [ directory, ...file ] = staticEndpoint.split('/') | |
100 | ||
101 | const staticPath = plugin.staticDirs[directory] | |
102 | if (!staticPath) return res.sendStatus(404) | |
103 | ||
104 | const filepath = file.join('/') | |
105 | return res.sendFile(join(plugin.path, staticPath, filepath), sendFileOptions) | |
106 | } | |
107 | ||
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 | ||
117 | function servePluginClientScripts (req: express.Request, res: express.Response) { | |
118 | const plugin: RegisteredPlugin = res.locals.registeredPlugin | |
119 | const staticEndpoint = req.params.staticEndpoint | |
120 | ||
121 | const file = plugin.clientScripts[staticEndpoint] | |
122 | if (!file) return res.sendStatus(404) | |
123 | ||
124 | return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions) | |
125 | } | |
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 | ||
135 | return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions) | |
136 | } |