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 { HttpStatusCode } from '../../shared/core-utils/miscs/http-error-codes'
8 import { getCompleteLocale, is18nLocale } from '../../shared/core-utils/i18n'
9 import { PluginType } from '../../shared/models/plugins/plugin.type'
10 import { isTestInstance } from '../helpers/core-utils'
11 import { logger } from '@server/helpers/logger'
13 const sendFileOptions = {
15 immutable: !isTestInstance()
18 const pluginsRouter = express.Router()
20 pluginsRouter.get('/plugins/global.css',
24 pluginsRouter.get('/plugins/translations/:locale.json',
28 pluginsRouter.get('/plugins/:pluginName/:pluginVersion/auth/:authName',
29 getPluginValidator(PluginType.PLUGIN),
30 getExternalAuthValidator,
34 pluginsRouter.get('/plugins/:pluginName/:pluginVersion/static/:staticEndpoint(*)',
35 getPluginValidator(PluginType.PLUGIN),
36 pluginStaticDirectoryValidator,
37 servePluginStaticDirectory
40 pluginsRouter.get('/plugins/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)',
41 getPluginValidator(PluginType.PLUGIN),
42 pluginStaticDirectoryValidator,
43 servePluginClientScripts
46 pluginsRouter.use('/plugins/:pluginName/router',
47 getPluginValidator(PluginType.PLUGIN, false),
48 servePluginCustomRoutes
51 pluginsRouter.use('/plugins/:pluginName/:pluginVersion/router',
52 getPluginValidator(PluginType.PLUGIN),
53 servePluginCustomRoutes
56 pluginsRouter.get('/themes/:pluginName/:pluginVersion/static/:staticEndpoint(*)',
57 getPluginValidator(PluginType.THEME),
58 pluginStaticDirectoryValidator,
59 servePluginStaticDirectory
62 pluginsRouter.get('/themes/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)',
63 getPluginValidator(PluginType.THEME),
64 pluginStaticDirectoryValidator,
65 servePluginClientScripts
68 pluginsRouter.get('/themes/:themeName/:themeVersion/css/:staticEndpoint(*)',
69 serveThemeCSSValidator,
70 serveThemeCSSDirectory
73 // ---------------------------------------------------------------------------
79 // ---------------------------------------------------------------------------
81 function servePluginGlobalCSS (req: express.Request, res: express.Response) {
82 // Only cache requests that have a ?hash=... query param
83 const globalCSSOptions = req.query.hash
87 return res.sendFile(PLUGIN_GLOBAL_CSS_PATH, globalCSSOptions)
90 function getPluginTranslations (req: express.Request, res: express.Response) {
91 const locale = req.params.locale
93 if (is18nLocale(locale)) {
94 const completeLocale = getCompleteLocale(locale)
95 const json = PluginManager.Instance.getTranslations(completeLocale)
100 return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
103 function servePluginStaticDirectory (req: express.Request, res: express.Response) {
104 const plugin: RegisteredPlugin = res.locals.registeredPlugin
105 const staticEndpoint = req.params.staticEndpoint
107 const [ directory, ...file ] = staticEndpoint.split('/')
109 const staticPath = plugin.staticDirs[directory]
110 if (!staticPath) return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
112 const filepath = file.join('/')
113 return res.sendFile(join(plugin.path, staticPath, filepath), sendFileOptions)
116 function servePluginCustomRoutes (req: express.Request, res: express.Response, next: express.NextFunction) {
117 const plugin: RegisteredPlugin = res.locals.registeredPlugin
118 const router = PluginManager.Instance.getRouter(plugin.npmName)
120 if (!router) return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
122 return router(req, res, next)
125 function servePluginClientScripts (req: express.Request, res: express.Response) {
126 const plugin: RegisteredPlugin = res.locals.registeredPlugin
127 const staticEndpoint = req.params.staticEndpoint
129 const file = plugin.clientScripts[staticEndpoint]
130 if (!file) return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
132 return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions)
135 function serveThemeCSSDirectory (req: express.Request, res: express.Response) {
136 const plugin: RegisteredPlugin = res.locals.registeredPlugin
137 const staticEndpoint = req.params.staticEndpoint
139 if (plugin.css.includes(staticEndpoint) === false) {
140 return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
143 return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions)
146 function handleAuthInPlugin (req: express.Request, res: express.Response) {
147 const authOptions = res.locals.externalAuth
150 logger.debug('Forwarding auth plugin request in %s of plugin %s.', authOptions.authName, res.locals.registeredPlugin.npmName)
151 authOptions.onAuthRequest(req, res)
153 logger.error('Forward request error in auth %s of plugin %s.', authOptions.authName, res.locals.registeredPlugin.npmName, { err })