oembed.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import * as express from 'express'
  2. import { query } from 'express-validator'
  3. import { join } from 'path'
  4. import { fetchVideo } from '@server/helpers/video'
  5. import { VideoPlaylistModel } from '@server/models/video/video-playlist'
  6. import { VideoPlaylistPrivacy, VideoPrivacy } from '@shared/models'
  7. import { isTestInstance } from '../../helpers/core-utils'
  8. import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc'
  9. import { logger } from '../../helpers/logger'
  10. import { WEBSERVER } from '../../initializers/constants'
  11. import { areValidationErrors } from './utils'
  12. import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
  13. const startVideoPlaylistsURL = WEBSERVER.SCHEME + '://' + join(WEBSERVER.HOST, 'videos', 'watch', 'playlist') + '/'
  14. const startVideosURL = WEBSERVER.SCHEME + '://' + join(WEBSERVER.HOST, 'videos', 'watch') + '/'
  15. const watchRegex = new RegExp('([^/]+)$')
  16. const isURLOptions = {
  17. require_host: true,
  18. require_tld: true
  19. }
  20. // We validate 'localhost', so we don't have the top level domain
  21. if (isTestInstance()) {
  22. isURLOptions.require_tld = false
  23. }
  24. const oembedValidator = [
  25. query('url').isURL(isURLOptions).withMessage('Should have a valid url'),
  26. query('maxwidth').optional().isInt().withMessage('Should have a valid max width'),
  27. query('maxheight').optional().isInt().withMessage('Should have a valid max height'),
  28. query('format').optional().isIn([ 'xml', 'json' ]).withMessage('Should have a valid format'),
  29. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  30. logger.debug('Checking oembed parameters', { parameters: req.query })
  31. if (areValidationErrors(req, res)) return
  32. if (req.query.format !== undefined && req.query.format !== 'json') {
  33. return res.status(HttpStatusCode.NOT_IMPLEMENTED_501)
  34. .json({ error: 'Requested format is not implemented on server.' })
  35. }
  36. const url = req.query.url as string
  37. const isPlaylist = url.startsWith(startVideoPlaylistsURL)
  38. const isVideo = isPlaylist ? false : url.startsWith(startVideosURL)
  39. const startIsOk = isVideo || isPlaylist
  40. const matches = watchRegex.exec(url)
  41. if (startIsOk === false || matches === null) {
  42. return res.status(HttpStatusCode.BAD_REQUEST_400)
  43. .json({ error: 'Invalid url.' })
  44. }
  45. const elementId = matches[1]
  46. if (isIdOrUUIDValid(elementId) === false) {
  47. return res.status(HttpStatusCode.BAD_REQUEST_400)
  48. .json({ error: 'Invalid video or playlist id.' })
  49. }
  50. if (isVideo) {
  51. const video = await fetchVideo(elementId, 'all')
  52. if (!video) {
  53. return res.status(HttpStatusCode.NOT_FOUND_404)
  54. .json({ error: 'Video not found' })
  55. }
  56. if (video.privacy !== VideoPrivacy.PUBLIC) {
  57. return res.status(HttpStatusCode.FORBIDDEN_403)
  58. .json({ error: 'Video is not public' })
  59. }
  60. res.locals.videoAll = video
  61. return next()
  62. }
  63. // Is playlist
  64. const videoPlaylist = await VideoPlaylistModel.loadWithAccountAndChannelSummary(elementId, undefined)
  65. if (!videoPlaylist) {
  66. return res.status(HttpStatusCode.NOT_FOUND_404)
  67. .json({ error: 'Video playlist not found' })
  68. }
  69. if (videoPlaylist.privacy !== VideoPlaylistPrivacy.PUBLIC) {
  70. return res.status(HttpStatusCode.FORBIDDEN_403)
  71. .json({ error: 'Playlist is not public' })
  72. }
  73. res.locals.videoPlaylistSummary = videoPlaylist
  74. return next()
  75. }
  76. ]
  77. // ---------------------------------------------------------------------------
  78. export {
  79. oembedValidator
  80. }