2
1

client.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import * as express from 'express'
  2. import { constants, promises as fs } from 'fs'
  3. import { join } from 'path'
  4. import { CONFIG } from '@server/initializers/config'
  5. import { buildFileLocale, getCompleteLocale, is18nLocale, LOCALE_FILES } from '@shared/core-utils/i18n'
  6. import { HttpStatusCode } from '@shared/core-utils'
  7. import { root } from '../helpers/core-utils'
  8. import { STATIC_MAX_AGE } from '../initializers/constants'
  9. import { ClientHtml, sendHTML, serveIndexHTML } from '../lib/client-html'
  10. import { asyncMiddleware, embedCSP } from '../middlewares'
  11. const clientsRouter = express.Router()
  12. const distPath = join(root(), 'client', 'dist')
  13. const testEmbedPath = join(distPath, 'standalone', 'videos', 'test-embed.html')
  14. // Special route that add OpenGraph and oEmbed tags
  15. // Do not use a template engine for a so little thing
  16. clientsRouter.use('/videos/watch/playlist/:id', asyncMiddleware(generateWatchPlaylistHtmlPage))
  17. clientsRouter.use('/videos/watch/:id', asyncMiddleware(generateWatchHtmlPage))
  18. clientsRouter.use('/accounts/:nameWithHost', asyncMiddleware(generateAccountHtmlPage))
  19. clientsRouter.use('/video-channels/:nameWithHost', asyncMiddleware(generateVideoChannelHtmlPage))
  20. const embedMiddlewares = [
  21. CONFIG.CSP.ENABLED
  22. ? embedCSP
  23. : (req: express.Request, res: express.Response, next: express.NextFunction) => next(),
  24. (req: express.Request, res: express.Response, next: express.NextFunction) => {
  25. res.removeHeader('X-Frame-Options')
  26. // Don't cache HTML file since it's an index to the immutable JS/CSS files
  27. res.setHeader('Cache-Control', 'public, max-age=0')
  28. next()
  29. },
  30. asyncMiddleware(generateEmbedHtmlPage)
  31. ]
  32. clientsRouter.use('/videos/embed', ...embedMiddlewares)
  33. clientsRouter.use('/video-playlists/embed', ...embedMiddlewares)
  34. const testEmbedController = (req: express.Request, res: express.Response) => res.sendFile(testEmbedPath)
  35. clientsRouter.use('/videos/test-embed', testEmbedController)
  36. clientsRouter.use('/video-playlists/test-embed', testEmbedController)
  37. // Static HTML/CSS/JS client files
  38. const staticClientFiles = [
  39. 'ngsw-worker.js',
  40. 'ngsw.json'
  41. ]
  42. for (const staticClientFile of staticClientFiles) {
  43. const path = join(root(), 'client', 'dist', staticClientFile)
  44. clientsRouter.get(`/${staticClientFile}`, (req: express.Request, res: express.Response) => {
  45. res.sendFile(path, { maxAge: STATIC_MAX_AGE.SERVER })
  46. })
  47. }
  48. // Dynamic PWA manifest
  49. clientsRouter.get('/manifest.webmanifest', asyncMiddleware(generateManifest))
  50. // Static client overrides
  51. // Must be consistent with static client overrides redirections in /support/nginx/peertube
  52. const staticClientOverrides = [
  53. 'assets/images/logo.svg',
  54. 'assets/images/favicon.png',
  55. 'assets/images/icons/icon-36x36.png',
  56. 'assets/images/icons/icon-48x48.png',
  57. 'assets/images/icons/icon-72x72.png',
  58. 'assets/images/icons/icon-96x96.png',
  59. 'assets/images/icons/icon-144x144.png',
  60. 'assets/images/icons/icon-192x192.png',
  61. 'assets/images/icons/icon-512x512.png'
  62. ]
  63. for (const staticClientOverride of staticClientOverrides) {
  64. const overridePhysicalPath = join(CONFIG.STORAGE.CLIENT_OVERRIDES_DIR, staticClientOverride)
  65. clientsRouter.use(`/client/${staticClientOverride}`, asyncMiddleware(serveClientOverride(overridePhysicalPath)))
  66. }
  67. clientsRouter.use('/client/locales/:locale/:file.json', serveServerTranslations)
  68. clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE.CLIENT }))
  69. // 404 for static files not found
  70. clientsRouter.use('/client/*', (req: express.Request, res: express.Response) => {
  71. res.sendStatus(HttpStatusCode.NOT_FOUND_404)
  72. })
  73. // Always serve index client page (the client is a single page application, let it handle routing)
  74. // Try to provide the right language index.html
  75. clientsRouter.use('/(:language)?', asyncMiddleware(serveIndexHTML))
  76. // ---------------------------------------------------------------------------
  77. export {
  78. clientsRouter
  79. }
  80. // ---------------------------------------------------------------------------
  81. function serveServerTranslations (req: express.Request, res: express.Response) {
  82. const locale = req.params.locale
  83. const file = req.params.file
  84. if (is18nLocale(locale) && LOCALE_FILES.includes(file)) {
  85. const completeLocale = getCompleteLocale(locale)
  86. const completeFileLocale = buildFileLocale(completeLocale)
  87. const path = join(__dirname, `../../../client/dist/locale/${file}.${completeFileLocale}.json`)
  88. return res.sendFile(path, { maxAge: STATIC_MAX_AGE.SERVER })
  89. }
  90. return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
  91. }
  92. async function generateEmbedHtmlPage (req: express.Request, res: express.Response) {
  93. const html = await ClientHtml.getEmbedHTML()
  94. return sendHTML(html, res)
  95. }
  96. async function generateWatchHtmlPage (req: express.Request, res: express.Response) {
  97. const html = await ClientHtml.getWatchHTMLPage(req.params.id + '', req, res)
  98. return sendHTML(html, res)
  99. }
  100. async function generateWatchPlaylistHtmlPage (req: express.Request, res: express.Response) {
  101. const html = await ClientHtml.getWatchPlaylistHTMLPage(req.params.id + '', req, res)
  102. return sendHTML(html, res)
  103. }
  104. async function generateAccountHtmlPage (req: express.Request, res: express.Response) {
  105. const html = await ClientHtml.getAccountHTMLPage(req.params.nameWithHost, req, res)
  106. return sendHTML(html, res)
  107. }
  108. async function generateVideoChannelHtmlPage (req: express.Request, res: express.Response) {
  109. const html = await ClientHtml.getVideoChannelHTMLPage(req.params.nameWithHost, req, res)
  110. return sendHTML(html, res)
  111. }
  112. async function generateManifest (req: express.Request, res: express.Response) {
  113. const manifestPhysicalPath = join(root(), 'client', 'dist', 'manifest.webmanifest')
  114. const manifestJson = await fs.readFile(manifestPhysicalPath, 'utf8')
  115. const manifest = JSON.parse(manifestJson)
  116. manifest.name = CONFIG.INSTANCE.NAME
  117. manifest.short_name = CONFIG.INSTANCE.NAME
  118. manifest.description = CONFIG.INSTANCE.SHORT_DESCRIPTION
  119. res.json(manifest)
  120. }
  121. function serveClientOverride (path: string) {
  122. return async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  123. try {
  124. await fs.access(path, constants.F_OK)
  125. // Serve override client
  126. res.sendFile(path, { maxAge: STATIC_MAX_AGE.SERVER })
  127. } catch {
  128. // Serve dist client
  129. next()
  130. }
  131. }
  132. }