client-html.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { HttpStatusCode } from '@peertube/peertube-models'
  2. import express from 'express'
  3. import { logger } from '../../helpers/logger.js'
  4. import { ACCEPT_HEADERS } from '../../initializers/constants.js'
  5. import { VideoHtml } from './shared/video-html.js'
  6. import { PlaylistHtml } from './shared/playlist-html.js'
  7. import { ActorHtml } from './shared/actor-html.js'
  8. import { PageHtml } from './shared/page-html.js'
  9. class ClientHtml {
  10. static invalidateCache () {
  11. PageHtml.invalidateCache()
  12. }
  13. static getDefaultHTMLPage (req: express.Request, res: express.Response, paramLang?: string) {
  14. return PageHtml.getDefaultHTML(req, res, paramLang)
  15. }
  16. // ---------------------------------------------------------------------------
  17. static getWatchHTMLPage (videoIdArg: string, req: express.Request, res: express.Response) {
  18. return VideoHtml.getWatchVideoHTML(videoIdArg, req, res)
  19. }
  20. static getVideoEmbedHTML (videoIdArg: string) {
  21. return VideoHtml.getEmbedVideoHTML(videoIdArg)
  22. }
  23. // ---------------------------------------------------------------------------
  24. static getWatchPlaylistHTMLPage (videoPlaylistIdArg: string, req: express.Request, res: express.Response) {
  25. return PlaylistHtml.getWatchPlaylistHTML(videoPlaylistIdArg, req, res)
  26. }
  27. static getVideoPlaylistEmbedHTML (playlistIdArg: string) {
  28. return PlaylistHtml.getEmbedPlaylistHTML(playlistIdArg)
  29. }
  30. // ---------------------------------------------------------------------------
  31. static getAccountHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) {
  32. return ActorHtml.getAccountHTMLPage(nameWithHost, req, res)
  33. }
  34. static getVideoChannelHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) {
  35. return ActorHtml.getVideoChannelHTMLPage(nameWithHost, req, res)
  36. }
  37. static getActorHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) {
  38. return ActorHtml.getActorHTMLPage(nameWithHost, req, res)
  39. }
  40. }
  41. function sendHTML (html: string, res: express.Response, localizedHTML: boolean = false) {
  42. res.set('Content-Type', 'text/html; charset=UTF-8')
  43. if (localizedHTML) {
  44. res.set('Vary', 'Accept-Language')
  45. }
  46. return res.send(html)
  47. }
  48. async function serveIndexHTML (req: express.Request, res: express.Response) {
  49. if (req.accepts(ACCEPT_HEADERS) === 'html' || !req.headers.accept) {
  50. try {
  51. await generateHTMLPage(req, res, req.params.language)
  52. return
  53. } catch (err) {
  54. logger.error('Cannot generate HTML page.', { err })
  55. return res.status(HttpStatusCode.INTERNAL_SERVER_ERROR_500).end()
  56. }
  57. }
  58. return res.status(HttpStatusCode.NOT_ACCEPTABLE_406).end()
  59. }
  60. // ---------------------------------------------------------------------------
  61. export {
  62. ClientHtml,
  63. sendHTML,
  64. serveIndexHTML
  65. }
  66. // ---------------------------------------------------------------------------
  67. // Private
  68. // ---------------------------------------------------------------------------
  69. async function generateHTMLPage (req: express.Request, res: express.Response, paramLang?: string) {
  70. const html = await ClientHtml.getDefaultHTMLPage(req, res, paramLang)
  71. return sendHTML(html, res, true)
  72. }