2019-07-05 07:54:32 -04:00
|
|
|
import * as express from 'express'
|
|
|
|
import { PLUGIN_GLOBAL_CSS_PATH } from '../initializers/constants'
|
2019-07-12 05:39:58 -04:00
|
|
|
import { join } from 'path'
|
2019-07-26 08:44:50 -04:00
|
|
|
import { PluginManager, RegisteredPlugin } from '../lib/plugins/plugin-manager'
|
2020-04-10 09:07:54 -04:00
|
|
|
import { getPluginValidator, pluginStaticDirectoryValidator } from '../middlewares/validators/plugins'
|
2019-07-12 05:39:58 -04:00
|
|
|
import { serveThemeCSSValidator } from '../middlewares/validators/themes'
|
|
|
|
import { PluginType } from '../../shared/models/plugins/plugin.type'
|
2019-07-23 03:48:48 -04:00
|
|
|
import { isTestInstance } from '../helpers/core-utils'
|
2019-07-26 08:44:50 -04:00
|
|
|
import { getCompleteLocale, is18nLocale } from '../../shared/models/i18n'
|
2019-07-23 03:48:48 -04:00
|
|
|
|
|
|
|
const sendFileOptions = {
|
|
|
|
maxAge: '30 days',
|
|
|
|
immutable: !isTestInstance()
|
|
|
|
}
|
2019-07-05 07:54:32 -04:00
|
|
|
|
|
|
|
const pluginsRouter = express.Router()
|
|
|
|
|
2019-07-12 05:39:58 -04:00
|
|
|
pluginsRouter.get('/plugins/global.css',
|
2019-07-08 08:02:03 -04:00
|
|
|
servePluginGlobalCSS
|
2019-07-05 07:54:32 -04:00
|
|
|
)
|
|
|
|
|
2019-07-26 08:44:50 -04:00
|
|
|
pluginsRouter.get('/plugins/translations/:locale.json',
|
|
|
|
getPluginTranslations
|
|
|
|
)
|
|
|
|
|
2019-07-12 05:39:58 -04:00
|
|
|
pluginsRouter.get('/plugins/:pluginName/:pluginVersion/static/:staticEndpoint(*)',
|
2020-04-10 09:07:54 -04:00
|
|
|
getPluginValidator(PluginType.PLUGIN),
|
|
|
|
pluginStaticDirectoryValidator,
|
2019-07-05 07:54:32 -04:00
|
|
|
servePluginStaticDirectory
|
|
|
|
)
|
|
|
|
|
2019-07-12 05:39:58 -04:00
|
|
|
pluginsRouter.get('/plugins/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)',
|
2020-04-10 09:07:54 -04:00
|
|
|
getPluginValidator(PluginType.PLUGIN),
|
|
|
|
pluginStaticDirectoryValidator,
|
2019-07-05 07:54:32 -04:00
|
|
|
servePluginClientScripts
|
|
|
|
)
|
|
|
|
|
2020-04-10 09:07:54 -04:00
|
|
|
pluginsRouter.use('/plugins/:pluginName/router',
|
|
|
|
getPluginValidator(PluginType.PLUGIN, false),
|
|
|
|
servePluginCustomRoutes
|
|
|
|
)
|
|
|
|
|
|
|
|
pluginsRouter.use('/plugins/:pluginName/:pluginVersion/router',
|
|
|
|
getPluginValidator(PluginType.PLUGIN),
|
|
|
|
servePluginCustomRoutes
|
|
|
|
)
|
|
|
|
|
2019-07-12 05:39:58 -04:00
|
|
|
pluginsRouter.get('/themes/:pluginName/:pluginVersion/static/:staticEndpoint(*)',
|
2020-04-10 09:07:54 -04:00
|
|
|
getPluginValidator(PluginType.THEME),
|
|
|
|
pluginStaticDirectoryValidator,
|
2019-07-12 05:39:58 -04:00
|
|
|
servePluginStaticDirectory
|
|
|
|
)
|
|
|
|
|
|
|
|
pluginsRouter.get('/themes/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)',
|
2020-04-10 09:07:54 -04:00
|
|
|
getPluginValidator(PluginType.THEME),
|
|
|
|
pluginStaticDirectoryValidator,
|
2019-07-12 05:39:58 -04:00
|
|
|
servePluginClientScripts
|
|
|
|
)
|
|
|
|
|
|
|
|
pluginsRouter.get('/themes/:themeName/:themeVersion/css/:staticEndpoint(*)',
|
|
|
|
serveThemeCSSValidator,
|
|
|
|
serveThemeCSSDirectory
|
|
|
|
)
|
|
|
|
|
2019-07-05 07:54:32 -04:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
pluginsRouter
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2019-07-08 08:02:03 -04:00
|
|
|
function servePluginGlobalCSS (req: express.Request, res: express.Response) {
|
2019-07-23 03:48:48 -04:00
|
|
|
// Only cache requests that have a ?hash=... query param
|
|
|
|
const globalCSSOptions = req.query.hash
|
|
|
|
? sendFileOptions
|
|
|
|
: {}
|
|
|
|
|
|
|
|
return res.sendFile(PLUGIN_GLOBAL_CSS_PATH, globalCSSOptions)
|
2019-07-08 08:02:03 -04:00
|
|
|
}
|
|
|
|
|
2019-07-26 08:44:50 -04:00
|
|
|
function getPluginTranslations (req: express.Request, res: express.Response) {
|
|
|
|
const locale = req.params.locale
|
|
|
|
|
|
|
|
if (is18nLocale(locale)) {
|
|
|
|
const completeLocale = getCompleteLocale(locale)
|
|
|
|
const json = PluginManager.Instance.getTranslations(completeLocale)
|
|
|
|
|
|
|
|
return res.json(json)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.sendStatus(404)
|
|
|
|
}
|
|
|
|
|
2019-07-05 07:54:32 -04:00
|
|
|
function servePluginStaticDirectory (req: express.Request, res: express.Response) {
|
|
|
|
const plugin: RegisteredPlugin = res.locals.registeredPlugin
|
|
|
|
const staticEndpoint = req.params.staticEndpoint
|
|
|
|
|
2019-07-08 08:02:03 -04:00
|
|
|
const [ directory, ...file ] = staticEndpoint.split('/')
|
|
|
|
|
|
|
|
const staticPath = plugin.staticDirs[directory]
|
2020-04-10 09:07:54 -04:00
|
|
|
if (!staticPath) return res.sendStatus(404)
|
2019-07-05 07:54:32 -04:00
|
|
|
|
2019-07-08 08:02:03 -04:00
|
|
|
const filepath = file.join('/')
|
2019-07-23 03:48:48 -04:00
|
|
|
return res.sendFile(join(plugin.path, staticPath, filepath), sendFileOptions)
|
2019-07-05 07:54:32 -04:00
|
|
|
}
|
|
|
|
|
2020-04-10 09:07:54 -04:00
|
|
|
function servePluginCustomRoutes (req: express.Request, res: express.Response, next: express.NextFunction) {
|
|
|
|
const plugin: RegisteredPlugin = res.locals.registeredPlugin
|
|
|
|
const router = PluginManager.Instance.getRouter(plugin.npmName)
|
|
|
|
|
|
|
|
if (!router) return res.sendStatus(404)
|
|
|
|
|
|
|
|
return router(req, res, next)
|
|
|
|
}
|
|
|
|
|
2019-07-05 07:54:32 -04:00
|
|
|
function servePluginClientScripts (req: express.Request, res: express.Response) {
|
|
|
|
const plugin: RegisteredPlugin = res.locals.registeredPlugin
|
|
|
|
const staticEndpoint = req.params.staticEndpoint
|
|
|
|
|
2019-07-08 08:02:03 -04:00
|
|
|
const file = plugin.clientScripts[staticEndpoint]
|
2020-04-10 09:07:54 -04:00
|
|
|
if (!file) return res.sendStatus(404)
|
2019-07-08 08:02:03 -04:00
|
|
|
|
2019-07-23 03:48:48 -04:00
|
|
|
return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions)
|
2019-07-05 07:54:32 -04:00
|
|
|
}
|
2019-07-12 05:39:58 -04:00
|
|
|
|
|
|
|
function serveThemeCSSDirectory (req: express.Request, res: express.Response) {
|
|
|
|
const plugin: RegisteredPlugin = res.locals.registeredPlugin
|
|
|
|
const staticEndpoint = req.params.staticEndpoint
|
|
|
|
|
|
|
|
if (plugin.css.includes(staticEndpoint) === false) {
|
|
|
|
return res.sendStatus(404)
|
|
|
|
}
|
|
|
|
|
2019-07-23 03:48:48 -04:00
|
|
|
return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions)
|
2019-07-12 05:39:58 -04:00
|
|
|
}
|