]>
Commit | Line | Data |
---|---|---|
345da516 C |
1 | import * as express from 'express' |
2 | import { PLUGIN_GLOBAL_CSS_PATH } from '../initializers/constants' | |
3 | import { join } from 'path' | |
4 | import { RegisteredPlugin } from '../lib/plugins/plugin-manager' | |
5 | import { servePluginStaticDirectoryValidator } from '../middlewares/validators/plugins' | |
6 | ||
7 | const pluginsRouter = express.Router() | |
8 | ||
9 | pluginsRouter.get('/global.css', | |
10 | express.static(PLUGIN_GLOBAL_CSS_PATH, { fallthrough: false }) | |
11 | ) | |
12 | ||
13 | pluginsRouter.get('/:pluginName/:pluginVersion/statics/:staticEndpoint', | |
14 | servePluginStaticDirectoryValidator, | |
15 | servePluginStaticDirectory | |
16 | ) | |
17 | ||
18 | pluginsRouter.get('/:pluginName/:pluginVersion/client-scripts/:staticEndpoint', | |
19 | servePluginStaticDirectoryValidator, | |
20 | servePluginClientScripts | |
21 | ) | |
22 | ||
23 | // --------------------------------------------------------------------------- | |
24 | ||
25 | export { | |
26 | pluginsRouter | |
27 | } | |
28 | ||
29 | // --------------------------------------------------------------------------- | |
30 | ||
31 | function servePluginStaticDirectory (req: express.Request, res: express.Response) { | |
32 | const plugin: RegisteredPlugin = res.locals.registeredPlugin | |
33 | const staticEndpoint = req.params.staticEndpoint | |
34 | ||
35 | const staticPath = plugin.staticDirs[staticEndpoint] | |
36 | if (!staticPath) { | |
37 | return res.sendStatus(404) | |
38 | } | |
39 | ||
40 | return express.static(join(plugin.path, staticPath), { fallthrough: false }) | |
41 | } | |
42 | ||
43 | function servePluginClientScripts (req: express.Request, res: express.Response) { | |
44 | const plugin: RegisteredPlugin = res.locals.registeredPlugin | |
45 | const staticEndpoint = req.params.staticEndpoint | |
46 | ||
47 | return express.static(join(plugin.path, staticEndpoint), { fallthrough: false }) | |
48 | } |