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