2019-07-05 06:54:32 -05:00
|
|
|
import * as express from 'express'
|
|
|
|
import { PLUGIN_GLOBAL_CSS_PATH } from '../initializers/constants'
|
2019-07-08 07:02:03 -05:00
|
|
|
import { basename, join } from 'path'
|
2019-07-05 06:54:32 -05:00
|
|
|
import { RegisteredPlugin } from '../lib/plugins/plugin-manager'
|
|
|
|
import { servePluginStaticDirectoryValidator } from '../middlewares/validators/plugins'
|
|
|
|
|
|
|
|
const pluginsRouter = express.Router()
|
|
|
|
|
|
|
|
pluginsRouter.get('/global.css',
|
2019-07-08 07:02:03 -05:00
|
|
|
servePluginGlobalCSS
|
2019-07-05 06:54:32 -05:00
|
|
|
)
|
|
|
|
|
2019-07-08 07:02:03 -05:00
|
|
|
pluginsRouter.get('/:pluginName/:pluginVersion/static/:staticEndpoint(*)',
|
2019-07-05 06:54:32 -05:00
|
|
|
servePluginStaticDirectoryValidator,
|
|
|
|
servePluginStaticDirectory
|
|
|
|
)
|
|
|
|
|
2019-07-08 07:02:03 -05:00
|
|
|
pluginsRouter.get('/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)',
|
2019-07-05 06:54:32 -05:00
|
|
|
servePluginStaticDirectoryValidator,
|
|
|
|
servePluginClientScripts
|
|
|
|
)
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
pluginsRouter
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2019-07-08 07:02:03 -05:00
|
|
|
function servePluginGlobalCSS (req: express.Request, res: express.Response) {
|
|
|
|
return res.sendFile(PLUGIN_GLOBAL_CSS_PATH)
|
|
|
|
}
|
|
|
|
|
2019-07-05 06:54:32 -05:00
|
|
|
function servePluginStaticDirectory (req: express.Request, res: express.Response) {
|
|
|
|
const plugin: RegisteredPlugin = res.locals.registeredPlugin
|
|
|
|
const staticEndpoint = req.params.staticEndpoint
|
|
|
|
|
2019-07-08 07:02:03 -05:00
|
|
|
const [ directory, ...file ] = staticEndpoint.split('/')
|
|
|
|
|
|
|
|
const staticPath = plugin.staticDirs[directory]
|
2019-07-05 06:54:32 -05:00
|
|
|
if (!staticPath) {
|
|
|
|
return res.sendStatus(404)
|
|
|
|
}
|
|
|
|
|
2019-07-08 07:02:03 -05:00
|
|
|
const filepath = file.join('/')
|
|
|
|
return res.sendFile(join(plugin.path, staticPath, filepath))
|
2019-07-05 06:54:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function servePluginClientScripts (req: express.Request, res: express.Response) {
|
|
|
|
const plugin: RegisteredPlugin = res.locals.registeredPlugin
|
|
|
|
const staticEndpoint = req.params.staticEndpoint
|
|
|
|
|
2019-07-08 07:02:03 -05:00
|
|
|
const file = plugin.clientScripts[staticEndpoint]
|
|
|
|
if (!file) {
|
|
|
|
return res.sendStatus(404)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.sendFile(join(plugin.path, staticEndpoint))
|
2019-07-05 06:54:32 -05:00
|
|
|
}
|