services.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import * as express from 'express'
  2. import { EMBED_SIZE, PREVIEWS_SIZE, WEBSERVER } from '../initializers/constants'
  3. import { asyncMiddleware, oembedValidator } from '../middlewares'
  4. import { accountNameWithHostGetValidator } from '../middlewares/validators'
  5. const servicesRouter = express.Router()
  6. servicesRouter.use('/oembed',
  7. asyncMiddleware(oembedValidator),
  8. generateOEmbed
  9. )
  10. servicesRouter.use('/redirect/accounts/:accountName',
  11. asyncMiddleware(accountNameWithHostGetValidator),
  12. redirectToAccountUrl
  13. )
  14. // ---------------------------------------------------------------------------
  15. export {
  16. servicesRouter
  17. }
  18. // ---------------------------------------------------------------------------
  19. function generateOEmbed (req: express.Request, res: express.Response) {
  20. const video = res.locals.video
  21. const webserverUrl = WEBSERVER.URL
  22. const maxHeight = parseInt(req.query.maxheight, 10)
  23. const maxWidth = parseInt(req.query.maxwidth, 10)
  24. const embedUrl = webserverUrl + video.getEmbedStaticPath()
  25. let thumbnailUrl = webserverUrl + video.getPreviewStaticPath()
  26. let embedWidth = EMBED_SIZE.width
  27. let embedHeight = EMBED_SIZE.height
  28. if (maxHeight < embedHeight) embedHeight = maxHeight
  29. if (maxWidth < embedWidth) embedWidth = maxWidth
  30. // Our thumbnail is too big for the consumer
  31. if (
  32. (maxHeight !== undefined && maxHeight < PREVIEWS_SIZE.height) ||
  33. (maxWidth !== undefined && maxWidth < PREVIEWS_SIZE.width)
  34. ) {
  35. thumbnailUrl = undefined
  36. }
  37. const html = `<iframe width="${embedWidth}" height="${embedHeight}" sandbox="allow-same-origin allow-scripts" ` +
  38. `src="${embedUrl}" frameborder="0" allowfullscreen></iframe>`
  39. const json: any = {
  40. type: 'video',
  41. version: '1.0',
  42. html,
  43. width: embedWidth,
  44. height: embedHeight,
  45. title: video.name,
  46. author_name: video.VideoChannel.Account.name,
  47. author_url: video.VideoChannel.Account.Actor.url,
  48. provider_name: 'PeerTube',
  49. provider_url: webserverUrl
  50. }
  51. if (thumbnailUrl !== undefined) {
  52. json.thumbnail_url = thumbnailUrl
  53. json.thumbnail_width = PREVIEWS_SIZE.width
  54. json.thumbnail_height = PREVIEWS_SIZE.height
  55. }
  56. return res.json(json)
  57. }
  58. function redirectToAccountUrl (req: express.Request, res: express.Response, next: express.NextFunction) {
  59. return res.redirect(res.locals.account.Actor.url)
  60. }