]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/oembed.ts
Live streaming implementation first step
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / oembed.ts
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
13 const startVideoPlaylistsURL = WEBSERVER.SCHEME + '://' + join(WEBSERVER.HOST, 'videos', 'watch', 'playlist') + '/'
14 const startVideosURL = WEBSERVER.SCHEME + '://' + join(WEBSERVER.HOST, 'videos', 'watch') + '/'
15
16 const watchRegex = new RegExp('([^/]+)$')
17 const isURLOptions = {
18 require_host: true,
19 require_tld: true
20 }
21
22 // We validate 'localhost', so we don't have the top level domain
23 if (isTestInstance()) {
24 isURLOptions.require_tld = false
25 }
26
27 const oembedValidator = [
28 query('url').isURL(isURLOptions).withMessage('Should have a valid url'),
29 query('maxwidth').optional().isInt().withMessage('Should have a valid max width'),
30 query('maxheight').optional().isInt().withMessage('Should have a valid max height'),
31 query('format').optional().isIn([ 'xml', 'json' ]).withMessage('Should have a valid format'),
32
33 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
34 logger.debug('Checking oembed parameters', { parameters: req.query })
35
36 if (areValidationErrors(req, res)) return
37
38 if (req.query.format !== undefined && req.query.format !== 'json') {
39 return res.status(501)
40 .json({ error: 'Requested format is not implemented on server.' })
41 }
42
43 const url = req.query.url as string
44
45 const isPlaylist = url.startsWith(startVideoPlaylistsURL)
46 const isVideo = isPlaylist ? false : url.startsWith(startVideosURL)
47
48 const startIsOk = isVideo || isPlaylist
49
50 const matches = watchRegex.exec(url)
51
52 if (startIsOk === false || matches === null) {
53 return res.status(400)
54 .json({ error: 'Invalid url.' })
55 }
56
57 const elementId = matches[1]
58 if (isIdOrUUIDValid(elementId) === false) {
59 return res.status(400)
60 .json({ error: 'Invalid video or playlist id.' })
61 }
62
63 if (isVideo) {
64 const video = await fetchVideo(elementId, 'all')
65
66 if (!video) {
67 return res.status(404)
68 .json({ error: 'Video not found' })
69 }
70
71 if (video.privacy !== VideoPrivacy.PUBLIC) {
72 return res.status(403)
73 .json({ error: 'Video is not public' })
74 }
75
76 res.locals.videoAll = video
77 return next()
78 }
79
80 // Is playlist
81
82 const videoPlaylist = await VideoPlaylistModel.loadWithAccountAndChannelSummary(elementId, undefined)
83 if (!videoPlaylist) {
84 return res.status(404)
85 .json({ error: 'Video playlist not found' })
86 }
87
88 if (videoPlaylist.privacy !== VideoPlaylistPrivacy.PUBLIC) {
89 return res.status(403)
90 .json({ error: 'Playlist is not public' })
91 }
92
93 res.locals.videoPlaylistSummary = videoPlaylist
94 return next()
95 }
96
97 ]
98
99 // ---------------------------------------------------------------------------
100
101 export {
102 oembedValidator
103 }