2
1

client.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import * as express from 'express'
  2. import { join } from 'path'
  3. import { root } from '../helpers/core-utils'
  4. import { ACCEPT_HEADERS, STATIC_MAX_AGE } from '../initializers/constants'
  5. import { asyncMiddleware, embedCSP } from '../middlewares'
  6. import { buildFileLocale, getCompleteLocale, is18nLocale, LOCALE_FILES } from '../../shared/models/i18n/i18n'
  7. import { ClientHtml } from '../lib/client-html'
  8. import { logger } from '../helpers/logger'
  9. const clientsRouter = express.Router()
  10. const distPath = join(root(), 'client', 'dist')
  11. const embedPath = join(distPath, 'standalone', 'videos', 'embed.html')
  12. const testEmbedPath = join(distPath, 'standalone', 'videos', 'test-embed.html')
  13. // Special route that add OpenGraph and oEmbed tags
  14. // Do not use a template engine for a so little thing
  15. clientsRouter.use('/videos/watch/:id', asyncMiddleware(generateWatchHtmlPage))
  16. clientsRouter.use('/accounts/:nameWithHost', asyncMiddleware(generateAccountHtmlPage))
  17. clientsRouter.use('/video-channels/:nameWithHost', asyncMiddleware(generateVideoChannelHtmlPage))
  18. clientsRouter.use(
  19. '/videos/embed',
  20. embedCSP,
  21. (req: express.Request, res: express.Response) => {
  22. res.removeHeader('X-Frame-Options')
  23. res.sendFile(embedPath)
  24. }
  25. )
  26. clientsRouter.use(
  27. '/videos/test-embed',
  28. (req: express.Request, res: express.Response) => res.sendFile(testEmbedPath)
  29. )
  30. // Static HTML/CSS/JS client files
  31. const staticClientFiles = [
  32. 'manifest.webmanifest',
  33. 'ngsw-worker.js',
  34. 'ngsw.json'
  35. ]
  36. for (const staticClientFile of staticClientFiles) {
  37. const path = join(root(), 'client', 'dist', staticClientFile)
  38. clientsRouter.get('/' + staticClientFile, (req: express.Request, res: express.Response) => {
  39. res.sendFile(path, { maxAge: STATIC_MAX_AGE.SERVER })
  40. })
  41. }
  42. clientsRouter.use('/client/locales/:locale/:file.json', serveServerTranslations)
  43. clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE.CLIENT }))
  44. // 404 for static files not found
  45. clientsRouter.use('/client/*', (req: express.Request, res: express.Response) => {
  46. res.sendStatus(404)
  47. })
  48. // Always serve index client page (the client is a single page application, let it handle routing)
  49. // Try to provide the right language index.html
  50. clientsRouter.use('/(:language)?', asyncMiddleware(serveIndexHTML))
  51. // ---------------------------------------------------------------------------
  52. export {
  53. clientsRouter
  54. }
  55. // ---------------------------------------------------------------------------
  56. async function serveServerTranslations (req: express.Request, res: express.Response) {
  57. const locale = req.params.locale
  58. const file = req.params.file
  59. if (is18nLocale(locale) && LOCALE_FILES.indexOf(file) !== -1) {
  60. const completeLocale = getCompleteLocale(locale)
  61. const completeFileLocale = buildFileLocale(completeLocale)
  62. const path = join(__dirname, `../../../client/dist/locale/${file}.${completeFileLocale}.json`)
  63. return res.sendFile(path, { maxAge: STATIC_MAX_AGE.SERVER })
  64. }
  65. return res.sendStatus(404)
  66. }
  67. async function serveIndexHTML (req: express.Request, res: express.Response) {
  68. if (req.accepts(ACCEPT_HEADERS) === 'html') {
  69. try {
  70. await generateHTMLPage(req, res, req.params.language)
  71. return
  72. } catch (err) {
  73. logger.error('Cannot generate HTML page.', err)
  74. }
  75. }
  76. return res.status(404).end()
  77. }
  78. async function generateHTMLPage (req: express.Request, res: express.Response, paramLang?: string) {
  79. const html = await ClientHtml.getDefaultHTMLPage(req, res, paramLang)
  80. return sendHTML(html, res)
  81. }
  82. async function generateWatchHtmlPage (req: express.Request, res: express.Response) {
  83. const html = await ClientHtml.getWatchHTMLPage(req.params.id + '', req, res)
  84. return sendHTML(html, res)
  85. }
  86. async function generateAccountHtmlPage (req: express.Request, res: express.Response) {
  87. const html = await ClientHtml.getAccountHTMLPage(req.params.nameWithHost, req, res)
  88. return sendHTML(html, res)
  89. }
  90. async function generateVideoChannelHtmlPage (req: express.Request, res: express.Response) {
  91. const html = await ClientHtml.getVideoChannelHTMLPage(req.params.nameWithHost, req, res)
  92. return sendHTML(html, res)
  93. }
  94. function sendHTML (html: string, res: express.Response) {
  95. res.set('Content-Type', 'text/html; charset=UTF-8')
  96. return res.send(html)
  97. }