plugins.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 { servePluginStaticDirectoryValidator } 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. const sendFileOptions = {
  11. maxAge: '30 days',
  12. immutable: !isTestInstance()
  13. }
  14. const pluginsRouter = express.Router()
  15. pluginsRouter.get('/plugins/global.css',
  16. servePluginGlobalCSS
  17. )
  18. pluginsRouter.get('/plugins/translations/:locale.json',
  19. getPluginTranslations
  20. )
  21. pluginsRouter.get('/plugins/:pluginName/:pluginVersion/static/:staticEndpoint(*)',
  22. servePluginStaticDirectoryValidator(PluginType.PLUGIN),
  23. servePluginStaticDirectory
  24. )
  25. pluginsRouter.get('/plugins/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)',
  26. servePluginStaticDirectoryValidator(PluginType.PLUGIN),
  27. servePluginClientScripts
  28. )
  29. pluginsRouter.get('/themes/:pluginName/:pluginVersion/static/:staticEndpoint(*)',
  30. servePluginStaticDirectoryValidator(PluginType.THEME),
  31. servePluginStaticDirectory
  32. )
  33. pluginsRouter.get('/themes/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)',
  34. servePluginStaticDirectoryValidator(PluginType.THEME),
  35. servePluginClientScripts
  36. )
  37. pluginsRouter.get('/themes/:themeName/:themeVersion/css/:staticEndpoint(*)',
  38. serveThemeCSSValidator,
  39. serveThemeCSSDirectory
  40. )
  41. // ---------------------------------------------------------------------------
  42. export {
  43. pluginsRouter
  44. }
  45. // ---------------------------------------------------------------------------
  46. function servePluginGlobalCSS (req: express.Request, res: express.Response) {
  47. // Only cache requests that have a ?hash=... query param
  48. const globalCSSOptions = req.query.hash
  49. ? sendFileOptions
  50. : {}
  51. return res.sendFile(PLUGIN_GLOBAL_CSS_PATH, globalCSSOptions)
  52. }
  53. function getPluginTranslations (req: express.Request, res: express.Response) {
  54. const locale = req.params.locale
  55. if (is18nLocale(locale)) {
  56. const completeLocale = getCompleteLocale(locale)
  57. const json = PluginManager.Instance.getTranslations(completeLocale)
  58. return res.json(json)
  59. }
  60. return res.sendStatus(404)
  61. }
  62. function servePluginStaticDirectory (req: express.Request, res: express.Response) {
  63. const plugin: RegisteredPlugin = res.locals.registeredPlugin
  64. const staticEndpoint = req.params.staticEndpoint
  65. const [ directory, ...file ] = staticEndpoint.split('/')
  66. const staticPath = plugin.staticDirs[directory]
  67. if (!staticPath) {
  68. return res.sendStatus(404)
  69. }
  70. const filepath = file.join('/')
  71. return res.sendFile(join(plugin.path, staticPath, filepath), sendFileOptions)
  72. }
  73. function servePluginClientScripts (req: express.Request, res: express.Response) {
  74. const plugin: RegisteredPlugin = res.locals.registeredPlugin
  75. const staticEndpoint = req.params.staticEndpoint
  76. const file = plugin.clientScripts[staticEndpoint]
  77. if (!file) {
  78. return res.sendStatus(404)
  79. }
  80. return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions)
  81. }
  82. function serveThemeCSSDirectory (req: express.Request, res: express.Response) {
  83. const plugin: RegisteredPlugin = res.locals.registeredPlugin
  84. const staticEndpoint = req.params.staticEndpoint
  85. if (plugin.css.includes(staticEndpoint) === false) {
  86. return res.sendStatus(404)
  87. }
  88. return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions)
  89. }