]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/middlewares/validators/oembed.ts
add video upload types, add doc middleware to more routes
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / oembed.ts
... / ...
CommitLineData
1import * as express from 'express'
2import { query } from 'express-validator'
3import { join } from 'path'
4import { loadVideo } from '@server/lib/model-loaders'
5import { VideoPlaylistModel } from '@server/models/video/video-playlist'
6import { VideoPlaylistPrivacy, VideoPrivacy } from '@shared/models'
7import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
8import { isTestInstance } from '../../helpers/core-utils'
9import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc'
10import { logger } from '../../helpers/logger'
11import { WEBSERVER } from '../../initializers/constants'
12import { areValidationErrors } from './shared'
13
14const playlistPaths = [
15 join('videos', 'watch', 'playlist'),
16 join('w', 'p')
17]
18
19const videoPaths = [
20 join('videos', 'watch'),
21 'w'
22]
23
24function buildUrls (paths: string[]) {
25 return paths.map(p => WEBSERVER.SCHEME + '://' + join(WEBSERVER.HOST, p) + '/')
26}
27
28const startPlaylistURLs = buildUrls(playlistPaths)
29const startVideoURLs = buildUrls(videoPaths)
30
31const watchRegex = /([^/]+)$/
32const isURLOptions = {
33 require_host: true,
34 require_tld: true
35}
36
37// We validate 'localhost', so we don't have the top level domain
38if (isTestInstance()) {
39 isURLOptions.require_tld = false
40}
41
42const oembedValidator = [
43 query('url').isURL(isURLOptions).withMessage('Should have a valid url'),
44 query('maxwidth').optional().isInt().withMessage('Should have a valid max width'),
45 query('maxheight').optional().isInt().withMessage('Should have a valid max height'),
46 query('format').optional().isIn([ 'xml', 'json' ]).withMessage('Should have a valid format'),
47
48 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
49 logger.debug('Checking oembed parameters', { parameters: req.query })
50
51 if (areValidationErrors(req, res)) return
52
53 if (req.query.format !== undefined && req.query.format !== 'json') {
54 return res.fail({
55 status: HttpStatusCode.NOT_IMPLEMENTED_501,
56 message: 'Requested format is not implemented on server.',
57 data: {
58 format: req.query.format
59 }
60 })
61 }
62
63 const url = req.query.url as string
64
65 const isPlaylist = startPlaylistURLs.some(u => url.startsWith(u))
66 const isVideo = isPlaylist ? false : startVideoURLs.some(u => url.startsWith(u))
67
68 const startIsOk = isVideo || isPlaylist
69
70 const matches = watchRegex.exec(url)
71
72 if (startIsOk === false || matches === null) {
73 return res.fail({
74 status: HttpStatusCode.BAD_REQUEST_400,
75 message: 'Invalid url.',
76 data: {
77 url
78 }
79 })
80 }
81
82 const elementId = matches[1]
83 if (isIdOrUUIDValid(elementId) === false) {
84 return res.fail({ message: 'Invalid video or playlist id.' })
85 }
86
87 if (isVideo) {
88 const video = await loadVideo(elementId, 'all')
89
90 if (!video) {
91 return res.fail({
92 status: HttpStatusCode.NOT_FOUND_404,
93 message: 'Video not found'
94 })
95 }
96
97 if (video.privacy !== VideoPrivacy.PUBLIC) {
98 return res.fail({
99 status: HttpStatusCode.FORBIDDEN_403,
100 message: 'Video is not public'
101 })
102 }
103
104 res.locals.videoAll = video
105 return next()
106 }
107
108 // Is playlist
109
110 const videoPlaylist = await VideoPlaylistModel.loadWithAccountAndChannelSummary(elementId, undefined)
111 if (!videoPlaylist) {
112 return res.fail({
113 status: HttpStatusCode.NOT_FOUND_404,
114 message: 'Video playlist not found'
115 })
116 }
117
118 if (videoPlaylist.privacy !== VideoPlaylistPrivacy.PUBLIC) {
119 return res.fail({
120 status: HttpStatusCode.FORBIDDEN_403,
121 message: 'Playlist is not public'
122 })
123 }
124
125 res.locals.videoPlaylistSummary = videoPlaylist
126 return next()
127 }
128
129]
130
131// ---------------------------------------------------------------------------
132
133export {
134 oembedValidator
135}