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 import { serveThemeCSSValidator } from '../middlewares/validators/themes'
7 import { PluginType } from '../../shared/models/plugins/plugin.type'
8 import { isTestInstance } from '../helpers/core-utils'
10 const sendFileOptions = {
12 immutable: !isTestInstance()
15 const pluginsRouter = express.Router()
17 pluginsRouter.get('/plugins/global.css',
21 pluginsRouter.get('/plugins/:pluginName/:pluginVersion/static/:staticEndpoint(*)',
22 servePluginStaticDirectoryValidator(PluginType.PLUGIN),
23 servePluginStaticDirectory
26 pluginsRouter.get('/plugins/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)',
27 servePluginStaticDirectoryValidator(PluginType.PLUGIN),
28 servePluginClientScripts
31 pluginsRouter.get('/themes/:pluginName/:pluginVersion/static/:staticEndpoint(*)',
32 servePluginStaticDirectoryValidator(PluginType.THEME),
33 servePluginStaticDirectory
36 pluginsRouter.get('/themes/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)',
37 servePluginStaticDirectoryValidator(PluginType.THEME),
38 servePluginClientScripts
41 pluginsRouter.get('/themes/:themeName/:themeVersion/css/:staticEndpoint(*)',
42 serveThemeCSSValidator,
43 serveThemeCSSDirectory
46 // ---------------------------------------------------------------------------
52 // ---------------------------------------------------------------------------
54 function servePluginGlobalCSS (req: express.Request, res: express.Response) {
55 // Only cache requests that have a ?hash=... query param
56 const globalCSSOptions = req.query.hash
60 return res.sendFile(PLUGIN_GLOBAL_CSS_PATH, globalCSSOptions)
63 function servePluginStaticDirectory (req: express.Request, res: express.Response) {
64 const plugin: RegisteredPlugin = res.locals.registeredPlugin
65 const staticEndpoint = req.params.staticEndpoint
67 const [ directory, ...file ] = staticEndpoint.split('/')
69 const staticPath = plugin.staticDirs[directory]
71 return res.sendStatus(404)
74 const filepath = file.join('/')
75 return res.sendFile(join(plugin.path, staticPath, filepath), sendFileOptions)
78 function servePluginClientScripts (req: express.Request, res: express.Response) {
79 const plugin: RegisteredPlugin = res.locals.registeredPlugin
80 const staticEndpoint = req.params.staticEndpoint
82 const file = plugin.clientScripts[staticEndpoint]
84 return res.sendStatus(404)
87 return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions)
90 function serveThemeCSSDirectory (req: express.Request, res: express.Response) {
91 const plugin: RegisteredPlugin = res.locals.registeredPlugin
92 const staticEndpoint = req.params.staticEndpoint
94 if (plugin.css.includes(staticEndpoint) === false) {
95 return res.sendStatus(404)
98 return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions)