]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/oembed.ts
Process remaining segment hashes on live ending
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / oembed.ts
CommitLineData
d8755eed 1import * as express from 'express'
c8861d5d 2import { query } from 'express-validator'
d8755eed 3import { join } from 'path'
6fad8e51
C
4import { fetchVideo } from '@server/helpers/video'
5import { VideoPlaylistModel } from '@server/models/video/video-playlist'
6import { VideoPlaylistPrivacy, VideoPrivacy } from '@shared/models'
da854ddd 7import { isTestInstance } from '../../helpers/core-utils'
3fd3ab2d 8import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc'
da854ddd 9import { logger } from '../../helpers/logger'
6dd9de95 10import { WEBSERVER } from '../../initializers/constants'
6fad8e51 11import { areValidationErrors } from './utils'
d8755eed 12
6fad8e51
C
13const startVideoPlaylistsURL = WEBSERVER.SCHEME + '://' + join(WEBSERVER.HOST, 'videos', 'watch', 'playlist') + '/'
14const startVideosURL = WEBSERVER.SCHEME + '://' + join(WEBSERVER.HOST, 'videos', 'watch') + '/'
15
16const watchRegex = new RegExp('([^/]+)$')
d8755eed
C
17const isURLOptions = {
18 require_host: true,
19 require_tld: true
20}
21
22// We validate 'localhost', so we don't have the top level domain
23if (isTestInstance()) {
24 isURLOptions.require_tld = false
25}
26
27const 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
a2431b7d 33 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
d8755eed
C
34 logger.debug('Checking oembed parameters', { parameters: req.query })
35
a2431b7d
C
36 if (areValidationErrors(req, res)) return
37
38 if (req.query.format !== undefined && req.query.format !== 'json') {
39 return res.status(501)
6fad8e51 40 .json({ error: 'Requested format is not implemented on server.' })
a2431b7d
C
41 }
42
67c604ae
C
43 const url = req.query.url as string
44
6fad8e51
C
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)
67c604ae 51
a2431b7d
C
52 if (startIsOk === false || matches === null) {
53 return res.status(400)
6fad8e51 54 .json({ error: 'Invalid url.' })
a2431b7d 55 }
d8755eed 56
6fad8e51
C
57 const elementId = matches[1]
58 if (isIdOrUUIDValid(elementId) === false) {
a2431b7d 59 return res.status(400)
6fad8e51 60 .json({ error: 'Invalid video or playlist id.' })
a2431b7d 61 }
d8755eed 62
6fad8e51
C
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 }
d8755eed 70
6fad8e51
C
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
a2431b7d 94 return next()
d8755eed 95 }
6fad8e51 96
d8755eed
C
97]
98
99// ---------------------------------------------------------------------------
100
101export {
102 oembedValidator
103}