]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/oembed.ts
Implemented configurable minimum signup age
[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'
a1eda903 7import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
da854ddd 8import { isTestInstance } from '../../helpers/core-utils'
3fd3ab2d 9import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc'
da854ddd 10import { logger } from '../../helpers/logger'
6dd9de95 11import { WEBSERVER } from '../../initializers/constants'
6fad8e51 12import { areValidationErrors } from './utils'
d8755eed 13
a1eda903
C
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)
6fad8e51 30
ba5a8d89 31const watchRegex = /([^/]+)$/
d8755eed
C
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
a2431b7d 48 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
d8755eed
C
49 logger.debug('Checking oembed parameters', { parameters: req.query })
50
a2431b7d
C
51 if (areValidationErrors(req, res)) return
52
53 if (req.query.format !== undefined && req.query.format !== 'json') {
2d53be02 54 return res.status(HttpStatusCode.NOT_IMPLEMENTED_501)
6fad8e51 55 .json({ error: 'Requested format is not implemented on server.' })
a2431b7d
C
56 }
57
67c604ae
C
58 const url = req.query.url as string
59
a1eda903
C
60 const isPlaylist = startPlaylistURLs.some(u => url.startsWith(u))
61 const isVideo = isPlaylist ? false : startVideoURLs.some(u => url.startsWith(u))
6fad8e51
C
62
63 const startIsOk = isVideo || isPlaylist
64
65 const matches = watchRegex.exec(url)
67c604ae 66
a2431b7d 67 if (startIsOk === false || matches === null) {
2d53be02 68 return res.status(HttpStatusCode.BAD_REQUEST_400)
6fad8e51 69 .json({ error: 'Invalid url.' })
a2431b7d 70 }
d8755eed 71
6fad8e51
C
72 const elementId = matches[1]
73 if (isIdOrUUIDValid(elementId) === false) {
2d53be02 74 return res.status(HttpStatusCode.BAD_REQUEST_400)
6fad8e51 75 .json({ error: 'Invalid video or playlist id.' })
a2431b7d 76 }
d8755eed 77
6fad8e51
C
78 if (isVideo) {
79 const video = await fetchVideo(elementId, 'all')
80
81 if (!video) {
2d53be02 82 return res.status(HttpStatusCode.NOT_FOUND_404)
6fad8e51
C
83 .json({ error: 'Video not found' })
84 }
d8755eed 85
6fad8e51 86 if (video.privacy !== VideoPrivacy.PUBLIC) {
2d53be02 87 return res.status(HttpStatusCode.FORBIDDEN_403)
6fad8e51
C
88 .json({ error: 'Video is not public' })
89 }
90
91 res.locals.videoAll = video
92 return next()
93 }
94
95 // Is playlist
96
97 const videoPlaylist = await VideoPlaylistModel.loadWithAccountAndChannelSummary(elementId, undefined)
98 if (!videoPlaylist) {
2d53be02 99 return res.status(HttpStatusCode.NOT_FOUND_404)
6fad8e51
C
100 .json({ error: 'Video playlist not found' })
101 }
102
103 if (videoPlaylist.privacy !== VideoPlaylistPrivacy.PUBLIC) {
2d53be02 104 return res.status(HttpStatusCode.FORBIDDEN_403)
6fad8e51
C
105 .json({ error: 'Playlist is not public' })
106 }
107
108 res.locals.videoPlaylistSummary = videoPlaylist
a2431b7d 109 return next()
d8755eed 110 }
6fad8e51 111
d8755eed
C
112]
113
114// ---------------------------------------------------------------------------
115
116export {
117 oembedValidator
118}