]>
Commit | Line | Data |
---|---|---|
d8755eed | 1 | import * as express from 'express' |
c8861d5d | 2 | import { query } from 'express-validator' |
d8755eed | 3 | import { join } from 'path' |
6fad8e51 C |
4 | import { fetchVideo } from '@server/helpers/video' |
5 | import { VideoPlaylistModel } from '@server/models/video/video-playlist' | |
6 | import { VideoPlaylistPrivacy, VideoPrivacy } from '@shared/models' | |
da854ddd | 7 | import { isTestInstance } from '../../helpers/core-utils' |
3fd3ab2d | 8 | import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc' |
da854ddd | 9 | import { logger } from '../../helpers/logger' |
6dd9de95 | 10 | import { WEBSERVER } from '../../initializers/constants' |
6fad8e51 | 11 | import { areValidationErrors } from './utils' |
2d53be02 | 12 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' |
d8755eed | 13 | |
6fad8e51 C |
14 | const startVideoPlaylistsURL = WEBSERVER.SCHEME + '://' + join(WEBSERVER.HOST, 'videos', 'watch', 'playlist') + '/' |
15 | const startVideosURL = WEBSERVER.SCHEME + '://' + join(WEBSERVER.HOST, 'videos', 'watch') + '/' | |
16 | ||
ba5a8d89 | 17 | const watchRegex = /([^/]+)$/ |
d8755eed C |
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 | ||
a2431b7d | 34 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
d8755eed C |
35 | logger.debug('Checking oembed parameters', { parameters: req.query }) |
36 | ||
a2431b7d C |
37 | if (areValidationErrors(req, res)) return |
38 | ||
39 | if (req.query.format !== undefined && req.query.format !== 'json') { | |
2d53be02 | 40 | return res.status(HttpStatusCode.NOT_IMPLEMENTED_501) |
6fad8e51 | 41 | .json({ error: 'Requested format is not implemented on server.' }) |
a2431b7d C |
42 | } |
43 | ||
67c604ae C |
44 | const url = req.query.url as string |
45 | ||
6fad8e51 C |
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) | |
67c604ae | 52 | |
a2431b7d | 53 | if (startIsOk === false || matches === null) { |
2d53be02 | 54 | return res.status(HttpStatusCode.BAD_REQUEST_400) |
6fad8e51 | 55 | .json({ error: 'Invalid url.' }) |
a2431b7d | 56 | } |
d8755eed | 57 | |
6fad8e51 C |
58 | const elementId = matches[1] |
59 | if (isIdOrUUIDValid(elementId) === false) { | |
2d53be02 | 60 | return res.status(HttpStatusCode.BAD_REQUEST_400) |
6fad8e51 | 61 | .json({ error: 'Invalid video or playlist id.' }) |
a2431b7d | 62 | } |
d8755eed | 63 | |
6fad8e51 C |
64 | if (isVideo) { |
65 | const video = await fetchVideo(elementId, 'all') | |
66 | ||
67 | if (!video) { | |
2d53be02 | 68 | return res.status(HttpStatusCode.NOT_FOUND_404) |
6fad8e51 C |
69 | .json({ error: 'Video not found' }) |
70 | } | |
d8755eed | 71 | |
6fad8e51 | 72 | if (video.privacy !== VideoPrivacy.PUBLIC) { |
2d53be02 | 73 | return res.status(HttpStatusCode.FORBIDDEN_403) |
6fad8e51 C |
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) { | |
2d53be02 | 85 | return res.status(HttpStatusCode.NOT_FOUND_404) |
6fad8e51 C |
86 | .json({ error: 'Video playlist not found' }) |
87 | } | |
88 | ||
89 | if (videoPlaylist.privacy !== VideoPlaylistPrivacy.PUBLIC) { | |
2d53be02 | 90 | return res.status(HttpStatusCode.FORBIDDEN_403) |
6fad8e51 C |
91 | .json({ error: 'Playlist is not public' }) |
92 | } | |
93 | ||
94 | res.locals.videoPlaylistSummary = videoPlaylist | |
a2431b7d | 95 | return next() |
d8755eed | 96 | } |
6fad8e51 | 97 | |
d8755eed C |
98 | ] |
99 | ||
100 | // --------------------------------------------------------------------------- | |
101 | ||
102 | export { | |
103 | oembedValidator | |
104 | } |