WIP plugins: static files

This commit is contained in:
Chocobozzz 2019-07-08 14:02:03 +02:00 committed by Chocobozzz
parent f023a19c3e
commit 2c0539420d
6 changed files with 124 additions and 15 deletions

View file

@ -1,21 +1,21 @@
import * as express from 'express'
import { PLUGIN_GLOBAL_CSS_PATH } from '../initializers/constants'
import { join } from 'path'
import { basename, join } from 'path'
import { RegisteredPlugin } from '../lib/plugins/plugin-manager'
import { servePluginStaticDirectoryValidator } from '../middlewares/validators/plugins'
const pluginsRouter = express.Router()
pluginsRouter.get('/global.css',
express.static(PLUGIN_GLOBAL_CSS_PATH, { fallthrough: false })
servePluginGlobalCSS
)
pluginsRouter.get('/:pluginName/:pluginVersion/statics/:staticEndpoint',
pluginsRouter.get('/:pluginName/:pluginVersion/static/:staticEndpoint(*)',
servePluginStaticDirectoryValidator,
servePluginStaticDirectory
)
pluginsRouter.get('/:pluginName/:pluginVersion/client-scripts/:staticEndpoint',
pluginsRouter.get('/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)',
servePluginStaticDirectoryValidator,
servePluginClientScripts
)
@ -28,21 +28,33 @@ export {
// ---------------------------------------------------------------------------
function servePluginGlobalCSS (req: express.Request, res: express.Response) {
return res.sendFile(PLUGIN_GLOBAL_CSS_PATH)
}
function servePluginStaticDirectory (req: express.Request, res: express.Response) {
const plugin: RegisteredPlugin = res.locals.registeredPlugin
const staticEndpoint = req.params.staticEndpoint
const staticPath = plugin.staticDirs[staticEndpoint]
const [ directory, ...file ] = staticEndpoint.split('/')
const staticPath = plugin.staticDirs[directory]
if (!staticPath) {
return res.sendStatus(404)
}
return express.static(join(plugin.path, staticPath), { fallthrough: false })
const filepath = file.join('/')
return res.sendFile(join(plugin.path, staticPath, filepath))
}
function servePluginClientScripts (req: express.Request, res: express.Response) {
const plugin: RegisteredPlugin = res.locals.registeredPlugin
const staticEndpoint = req.params.staticEndpoint
return express.static(join(plugin.path, staticEndpoint), { fallthrough: false })
const file = plugin.clientScripts[staticEndpoint]
if (!file) {
return res.sendStatus(404)
}
return res.sendFile(join(plugin.path, staticEndpoint))
}