ニジカ投稿局 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.

services.ts 4.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import express from 'express'
  2. import { escapeHTML, forceNumber } from '@peertube/peertube-core-utils'
  3. import { MChannelSummary } from '@server/types/models/index.js'
  4. import { EMBED_SIZE, PREVIEWS_SIZE, THUMBNAILS_SIZE, WEBSERVER } from '../initializers/constants.js'
  5. import { apiRateLimiter, asyncMiddleware, oembedValidator } from '../middlewares/index.js'
  6. import { accountNameWithHostGetValidator } from '../middlewares/validators/index.js'
  7. const servicesRouter = express.Router()
  8. servicesRouter.use('/oembed',
  9. apiRateLimiter,
  10. asyncMiddleware(oembedValidator),
  11. generateOEmbed
  12. )
  13. servicesRouter.use('/redirect/accounts/:accountName',
  14. apiRateLimiter,
  15. asyncMiddleware(accountNameWithHostGetValidator),
  16. redirectToAccountUrl
  17. )
  18. // ---------------------------------------------------------------------------
  19. export {
  20. servicesRouter
  21. }
  22. // ---------------------------------------------------------------------------
  23. function generateOEmbed (req: express.Request, res: express.Response) {
  24. if (res.locals.videoAll) return generateVideoOEmbed(req, res)
  25. return generatePlaylistOEmbed(req, res)
  26. }
  27. function generatePlaylistOEmbed (req: express.Request, res: express.Response) {
  28. const playlist = res.locals.videoPlaylistSummary
  29. const json = buildOEmbed({
  30. channel: playlist.VideoChannel,
  31. title: playlist.name,
  32. embedPath: playlist.getEmbedStaticPath() + buildPlayerURLQuery(req.query.url),
  33. previewPath: playlist.getThumbnailStaticPath(),
  34. previewSize: THUMBNAILS_SIZE,
  35. req
  36. })
  37. return res.json(json)
  38. }
  39. function generateVideoOEmbed (req: express.Request, res: express.Response) {
  40. const video = res.locals.videoAll
  41. const json = buildOEmbed({
  42. channel: video.VideoChannel,
  43. title: video.name,
  44. embedPath: video.getEmbedStaticPath() + buildPlayerURLQuery(req.query.url),
  45. previewPath: video.getPreviewStaticPath(),
  46. previewSize: PREVIEWS_SIZE,
  47. req
  48. })
  49. return res.json(json)
  50. }
  51. function buildPlayerURLQuery (inputQueryUrl: string) {
  52. const allowedParameters = new Set([
  53. 'start',
  54. 'stop',
  55. 'loop',
  56. 'autoplay',
  57. 'muted',
  58. 'controls',
  59. 'controlBar',
  60. 'title',
  61. 'api',
  62. 'warningTitle',
  63. 'peertubeLink',
  64. 'p2p',
  65. 'subtitle',
  66. 'bigPlayBackgroundColor',
  67. 'mode',
  68. 'foregroundColor'
  69. ])
  70. const params = new URLSearchParams()
  71. new URL(inputQueryUrl).searchParams.forEach((v, k) => {
  72. if (allowedParameters.has(k)) {
  73. params.append(k, v)
  74. }
  75. })
  76. const stringQuery = params.toString()
  77. if (!stringQuery) return ''
  78. return '?' + stringQuery
  79. }
  80. function buildOEmbed (options: {
  81. req: express.Request
  82. title: string
  83. channel: MChannelSummary
  84. previewPath: string | null
  85. embedPath: string
  86. previewSize: {
  87. height: number
  88. width: number
  89. }
  90. }) {
  91. const { req, previewSize, previewPath, title, channel, embedPath } = options
  92. const webserverUrl = WEBSERVER.URL
  93. const maxHeight = forceNumber(req.query.maxheight)
  94. const maxWidth = forceNumber(req.query.maxwidth)
  95. const embedUrl = webserverUrl + embedPath
  96. const embedTitle = escapeHTML(title)
  97. let thumbnailUrl = previewPath
  98. ? webserverUrl + previewPath
  99. : undefined
  100. let embedWidth = EMBED_SIZE.width
  101. if (maxWidth < embedWidth) embedWidth = maxWidth
  102. let embedHeight = EMBED_SIZE.height
  103. if (maxHeight < embedHeight) embedHeight = maxHeight
  104. // Our thumbnail is too big for the consumer
  105. if (
  106. (maxHeight !== undefined && maxHeight < previewSize.height) ||
  107. (maxWidth !== undefined && maxWidth < previewSize.width)
  108. ) {
  109. thumbnailUrl = undefined
  110. }
  111. const html = `<iframe width="${embedWidth}" height="${embedHeight}" sandbox="allow-same-origin allow-scripts allow-popups allow-forms" ` +
  112. `title="${embedTitle}" src="${embedUrl}" frameborder="0" allowfullscreen></iframe>`
  113. const json: any = {
  114. type: 'video',
  115. version: '1.0',
  116. html,
  117. width: embedWidth,
  118. height: embedHeight,
  119. title,
  120. author_name: channel.name,
  121. author_url: channel.Actor.url,
  122. provider_name: 'PeerTube',
  123. provider_url: webserverUrl
  124. }
  125. if (thumbnailUrl !== undefined) {
  126. json.thumbnail_url = thumbnailUrl
  127. json.thumbnail_width = previewSize.width
  128. json.thumbnail_height = previewSize.height
  129. }
  130. return json
  131. }
  132. function redirectToAccountUrl (req: express.Request, res: express.Response, next: express.NextFunction) {
  133. return res.redirect(res.locals.account.Actor.url)
  134. }