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