]>
Commit | Line | Data |
---|---|---|
d8755eed | 1 | import * as express from 'express' |
c8861d5d | 2 | import { query } from 'express-validator' |
d8755eed | 3 | import { join } from 'path' |
868fce62 | 4 | import { loadVideo } from '@server/lib/model-loaders' |
6fad8e51 C |
5 | import { VideoPlaylistModel } from '@server/models/video/video-playlist' |
6 | import { VideoPlaylistPrivacy, VideoPrivacy } from '@shared/models' | |
a1eda903 | 7 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' |
da854ddd | 8 | import { isTestInstance } from '../../helpers/core-utils' |
d4a8e7a6 | 9 | import { isIdOrUUIDValid, toCompleteUUID } from '../../helpers/custom-validators/misc' |
da854ddd | 10 | import { logger } from '../../helpers/logger' |
6dd9de95 | 11 | import { WEBSERVER } from '../../initializers/constants' |
10363c74 | 12 | import { areValidationErrors } from './shared' |
d8755eed | 13 | |
a1eda903 C |
14 | const playlistPaths = [ |
15 | join('videos', 'watch', 'playlist'), | |
16 | join('w', 'p') | |
17 | ] | |
18 | ||
19 | const videoPaths = [ | |
20 | join('videos', 'watch'), | |
21 | 'w' | |
22 | ] | |
23 | ||
24 | function buildUrls (paths: string[]) { | |
25 | return paths.map(p => WEBSERVER.SCHEME + '://' + join(WEBSERVER.HOST, p) + '/') | |
26 | } | |
27 | ||
28 | const startPlaylistURLs = buildUrls(playlistPaths) | |
29 | const startVideoURLs = buildUrls(videoPaths) | |
6fad8e51 | 30 | |
ba5a8d89 | 31 | const watchRegex = /([^/]+)$/ |
d8755eed C |
32 | const isURLOptions = { |
33 | require_host: true, | |
34 | require_tld: true | |
35 | } | |
36 | ||
37 | // We validate 'localhost', so we don't have the top level domain | |
38 | if (isTestInstance()) { | |
39 | isURLOptions.require_tld = false | |
40 | } | |
41 | ||
42 | const 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') { | |
76148b27 RK |
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 | }) | |
a2431b7d C |
61 | } |
62 | ||
67c604ae C |
63 | const url = req.query.url as string |
64 | ||
a1eda903 C |
65 | const isPlaylist = startPlaylistURLs.some(u => url.startsWith(u)) |
66 | const isVideo = isPlaylist ? false : startVideoURLs.some(u => url.startsWith(u)) | |
6fad8e51 C |
67 | |
68 | const startIsOk = isVideo || isPlaylist | |
69 | ||
70 | const matches = watchRegex.exec(url) | |
67c604ae | 71 | |
a2431b7d | 72 | if (startIsOk === false || matches === null) { |
76148b27 RK |
73 | return res.fail({ |
74 | status: HttpStatusCode.BAD_REQUEST_400, | |
75 | message: 'Invalid url.', | |
76 | data: { | |
77 | url | |
78 | } | |
79 | }) | |
a2431b7d | 80 | } |
d8755eed | 81 | |
d4a8e7a6 | 82 | const elementId = toCompleteUUID(matches[1]) |
6fad8e51 | 83 | if (isIdOrUUIDValid(elementId) === false) { |
76148b27 | 84 | return res.fail({ message: 'Invalid video or playlist id.' }) |
a2431b7d | 85 | } |
d8755eed | 86 | |
6fad8e51 | 87 | if (isVideo) { |
868fce62 | 88 | const video = await loadVideo(elementId, 'all') |
6fad8e51 C |
89 | |
90 | if (!video) { | |
76148b27 RK |
91 | return res.fail({ |
92 | status: HttpStatusCode.NOT_FOUND_404, | |
93 | message: 'Video not found' | |
94 | }) | |
6fad8e51 | 95 | } |
d8755eed | 96 | |
6fad8e51 | 97 | if (video.privacy !== VideoPrivacy.PUBLIC) { |
76148b27 RK |
98 | return res.fail({ |
99 | status: HttpStatusCode.FORBIDDEN_403, | |
100 | message: 'Video is not public' | |
101 | }) | |
6fad8e51 C |
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) { | |
76148b27 RK |
112 | return res.fail({ |
113 | status: HttpStatusCode.NOT_FOUND_404, | |
114 | message: 'Video playlist not found' | |
115 | }) | |
6fad8e51 C |
116 | } |
117 | ||
118 | if (videoPlaylist.privacy !== VideoPlaylistPrivacy.PUBLIC) { | |
76148b27 RK |
119 | return res.fail({ |
120 | status: HttpStatusCode.FORBIDDEN_403, | |
121 | message: 'Playlist is not public' | |
122 | }) | |
6fad8e51 C |
123 | } |
124 | ||
125 | res.locals.videoPlaylistSummary = videoPlaylist | |
a2431b7d | 126 | return next() |
d8755eed | 127 | } |
6fad8e51 | 128 | |
d8755eed C |
129 | ] |
130 | ||
131 | // --------------------------------------------------------------------------- | |
132 | ||
133 | export { | |
134 | oembedValidator | |
135 | } |