1 import * as express from 'express'
2 import { PLUGIN_GLOBAL_CSS_PATH } from '../initializers/constants'
3 import { join } from 'path'
4 import { PluginManager, RegisteredPlugin } from '../lib/plugins/plugin-manager'
5 import { getPluginValidator, pluginStaticDirectoryValidator, getExternalAuthValidator } 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'
9 import { getCompleteLocale, is18nLocale } from '../../shared/models/i18n'
10 import { logger } from '@server/helpers/logger'
12 const sendFileOptions = {
14 immutable: !isTestInstance()
17 const pluginsRouter = express.Router()
19 pluginsRouter.get('/plugins/global.css',
23 pluginsRouter.get('/plugins/translations/:locale.json',
27 pluginsRouter.get('/plugins/:pluginName/:pluginVersion/auth/:authName',
28 getPluginValidator(PluginType.PLUGIN),
29 getExternalAuthValidator,
33 pluginsRouter.get('/plugins/:pluginName/:pluginVersion/static/:staticEndpoint(*)',
34 getPluginValidator(PluginType.PLUGIN),
35 pluginStaticDirectoryValidator,
36 servePluginStaticDirectory
39 pluginsRouter.get('/plugins/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)',
40 getPluginValidator(PluginType.PLUGIN),
41 pluginStaticDirectoryValidator,
42 servePluginClientScripts
45 pluginsRouter.use('/plugins/:pluginName/router',
46 getPluginValidator(PluginType.PLUGIN, false),
47 servePluginCustomRoutes
50 pluginsRouter.use('/plugins/:pluginName/:pluginVersion/router',
51 getPluginValidator(PluginType.PLUGIN),
52 servePluginCustomRoutes
55 pluginsRouter.get('/themes/:pluginName/:pluginVersion/static/:staticEndpoint(*)',
56 getPluginValidator(PluginType.THEME),
57 pluginStaticDirectoryValidator,
58 servePluginStaticDirectory
61 pluginsRouter.get('/themes/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)',
62 getPluginValidator(PluginType.THEME),
63 pluginStaticDirectoryValidator,
64 servePluginClientScripts
67 pluginsRouter.get('/themes/:themeName/:themeVersion/css/:staticEndpoint(*)',
68 serveThemeCSSValidator,
69 serveThemeCSSDirectory
72 // ---------------------------------------------------------------------------
78 // ---------------------------------------------------------------------------
80 function servePluginGlobalCSS (req: express.Request, res: express.Response) {
81 // Only cache requests that have a ?hash=... query param
82 const globalCSSOptions = req.query.hash
86 return res.sendFile(PLUGIN_GLOBAL_CSS_PATH, globalCSSOptions)
89 function getPluginTranslations (req: express.Request, res: express.Response) {
90 const locale = req.params.locale
92 if (is18nLocale(locale)) {
93 const completeLocale = getCompleteLocale(locale)
94 const json = PluginManager.Instance.getTranslations(completeLocale)
99 return res.sendStatus(404)
102 function servePluginStaticDirectory (req: express.Request, res: express.Response) {
103 const plugin: RegisteredPlugin = res.locals.registeredPlugin
104 const staticEndpoint = req.params.staticEndpoint
106 const [ directory, ...file ] = staticEndpoint.split('/')
108 const staticPath = plugin.staticDirs[directory]
109 if (!staticPath) return res.sendStatus(404)
111 const filepath = file.join('/')
112 return res.sendFile(join(plugin.path, staticPath, filepath), sendFileOptions)
115 function servePluginCustomRoutes (req: express.Request, res: express.Response, next: express.NextFunction) {
116 const plugin: RegisteredPlugin = res.locals.registeredPlugin
117 const router = PluginManager.Instance.getRouter(plugin.npmName)
119 if (!router) return res.sendStatus(404)
121 return router(req, res, next)
124 function servePluginClientScripts (req: express.Request, res: express.Response) {
125 const plugin: RegisteredPlugin = res.locals.registeredPlugin
126 const staticEndpoint = req.params.staticEndpoint
128 const file = plugin.clientScripts[staticEndpoint]
129 if (!file) return res.sendStatus(404)
131 return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions)
134 function serveThemeCSSDirectory (req: express.Request, res: express.Response) {
135 const plugin: RegisteredPlugin = res.locals.registeredPlugin
136 const staticEndpoint = req.params.staticEndpoint
138 if (plugin.css.includes(staticEndpoint) === false) {
139 return res.sendStatus(404)
142 return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions)
145 function handleAuthInPlugin (req: express.Request, res: express.Response) {
146 const authOptions = res.locals.externalAuth
149 logger.debug('Forwarding auth plugin request in %s of plugin %s.', authOptions.authName, res.locals.registeredPlugin.npmName)
150 authOptions.onAuthRequest(req, res)
152 logger.error('Forward request error in auth %s of plugin %s.', authOptions.authName, res.locals.registeredPlugin.npmName, { err })