]>
Commit | Line | Data |
---|---|---|
d8755eed | 1 | import * as express from 'express' |
a2431b7d | 2 | import { query } from 'express-validator/check' |
d8755eed | 3 | import { join } from 'path' |
3fd3ab2d C |
4 | import { isTestInstance, logger } from '../../helpers' |
5 | import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc' | |
6 | import { isVideoExist } from '../../helpers/custom-validators/videos' | |
d8755eed | 7 | import { CONFIG } from '../../initializers' |
a2431b7d | 8 | import { areValidationErrors } from './utils' |
d8755eed C |
9 | |
10 | const urlShouldStartWith = CONFIG.WEBSERVER.SCHEME + '://' + join(CONFIG.WEBSERVER.HOST, 'videos', 'watch') + '/' | |
11 | const videoWatchRegex = new RegExp('([^/]+)$') | |
12 | const isURLOptions = { | |
13 | require_host: true, | |
14 | require_tld: true | |
15 | } | |
16 | ||
17 | // We validate 'localhost', so we don't have the top level domain | |
18 | if (isTestInstance()) { | |
19 | isURLOptions.require_tld = false | |
20 | } | |
21 | ||
22 | const oembedValidator = [ | |
23 | query('url').isURL(isURLOptions).withMessage('Should have a valid url'), | |
24 | query('maxwidth').optional().isInt().withMessage('Should have a valid max width'), | |
25 | query('maxheight').optional().isInt().withMessage('Should have a valid max height'), | |
26 | query('format').optional().isIn([ 'xml', 'json' ]).withMessage('Should have a valid format'), | |
27 | ||
a2431b7d | 28 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
d8755eed C |
29 | logger.debug('Checking oembed parameters', { parameters: req.query }) |
30 | ||
a2431b7d C |
31 | if (areValidationErrors(req, res)) return |
32 | ||
33 | if (req.query.format !== undefined && req.query.format !== 'json') { | |
34 | return res.status(501) | |
35 | .json({ error: 'Requested format is not implemented on server.' }) | |
36 | .end() | |
37 | } | |
38 | ||
39 | const startIsOk = req.query.url.startsWith(urlShouldStartWith) | |
40 | const matches = videoWatchRegex.exec(req.query.url) | |
41 | if (startIsOk === false || matches === null) { | |
42 | return res.status(400) | |
43 | .json({ error: 'Invalid url.' }) | |
44 | .end() | |
45 | } | |
d8755eed | 46 | |
a2431b7d C |
47 | const videoId = matches[1] |
48 | if (isIdOrUUIDValid(videoId) === false) { | |
49 | return res.status(400) | |
50 | .json({ error: 'Invalid video id.' }) | |
51 | .end() | |
52 | } | |
d8755eed | 53 | |
a2431b7d | 54 | if (!await isVideoExist(videoId, res)) return |
d8755eed | 55 | |
a2431b7d | 56 | return next() |
d8755eed C |
57 | } |
58 | ] | |
59 | ||
60 | // --------------------------------------------------------------------------- | |
61 | ||
62 | export { | |
63 | oembedValidator | |
64 | } |