ニジカ投稿局 https://tv.nizika.tv
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

97 lines
3.2 KiB

  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. res.set('Cache-Control', 'max-age=0, no-cache, must-revalidate')
  44. if (localizedHTML) {
  45. res.set('Vary', 'Accept-Language')
  46. }
  47. return res.send(html)
  48. }
  49. async function serveIndexHTML (req: express.Request, res: express.Response) {
  50. if (req.accepts(ACCEPT_HEADERS) === 'html' || !req.headers.accept) {
  51. try {
  52. await generateHTMLPage(req, res, req.params.language)
  53. return
  54. } catch (err) {
  55. logger.error('Cannot generate HTML page.', { err })
  56. return res.status(HttpStatusCode.INTERNAL_SERVER_ERROR_500).end()
  57. }
  58. }
  59. return res.status(HttpStatusCode.NOT_ACCEPTABLE_406).end()
  60. }
  61. // ---------------------------------------------------------------------------
  62. export {
  63. ClientHtml,
  64. sendHTML,
  65. serveIndexHTML
  66. }
  67. // ---------------------------------------------------------------------------
  68. // Private
  69. // ---------------------------------------------------------------------------
  70. async function generateHTMLPage (req: express.Request, res: express.Response, paramLang?: string) {
  71. const html = await ClientHtml.getDefaultHTMLPage(req, res, paramLang)
  72. return sendHTML(html, res, true)
  73. }