]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import 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' | |
c0e8b12e | 7 | import { HttpStatusCode } from '../../../shared/models/http/http-error-codes' |
9452d4fd | 8 | import { isTestOrDevInstance } from '../../helpers/core-utils' |
b3d9dedc | 9 | import { isIdOrUUIDValid, isUUIDValid, 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 | |
d8755eed C |
31 | const isURLOptions = { |
32 | require_host: true, | |
33 | require_tld: true | |
34 | } | |
35 | ||
36 | // We validate 'localhost', so we don't have the top level domain | |
9452d4fd | 37 | if (isTestOrDevInstance()) { |
d8755eed C |
38 | isURLOptions.require_tld = false |
39 | } | |
40 | ||
41 | const oembedValidator = [ | |
396f6f01 C |
42 | query('url') |
43 | .isURL(isURLOptions), | |
44 | query('maxwidth') | |
45 | .optional() | |
46 | .isInt(), | |
47 | query('maxheight') | |
48 | .optional() | |
49 | .isInt(), | |
50 | query('format') | |
51 | .optional() | |
52 | .isIn([ 'xml', 'json' ]), | |
d8755eed | 53 | |
a2431b7d | 54 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
d8755eed C |
55 | logger.debug('Checking oembed parameters', { parameters: req.query }) |
56 | ||
a2431b7d C |
57 | if (areValidationErrors(req, res)) return |
58 | ||
59 | if (req.query.format !== undefined && req.query.format !== 'json') { | |
76148b27 RK |
60 | return res.fail({ |
61 | status: HttpStatusCode.NOT_IMPLEMENTED_501, | |
62 | message: 'Requested format is not implemented on server.', | |
63 | data: { | |
64 | format: req.query.format | |
65 | } | |
66 | }) | |
a2431b7d C |
67 | } |
68 | ||
67c604ae C |
69 | const url = req.query.url as string |
70 | ||
e93ee838 C |
71 | let urlPath: string |
72 | ||
73 | try { | |
74 | urlPath = new URL(url).pathname | |
75 | } catch (err) { | |
76 | return res.fail({ | |
77 | status: HttpStatusCode.BAD_REQUEST_400, | |
78 | message: err.message, | |
79 | data: { | |
80 | url | |
81 | } | |
82 | }) | |
83 | } | |
84 | ||
a1eda903 C |
85 | const isPlaylist = startPlaylistURLs.some(u => url.startsWith(u)) |
86 | const isVideo = isPlaylist ? false : startVideoURLs.some(u => url.startsWith(u)) | |
6fad8e51 C |
87 | |
88 | const startIsOk = isVideo || isPlaylist | |
89 | ||
e5d9877f | 90 | const parts = urlPath.split('/') |
67c604ae | 91 | |
e5d9877f | 92 | if (startIsOk === false || parts.length === 0) { |
76148b27 RK |
93 | return res.fail({ |
94 | status: HttpStatusCode.BAD_REQUEST_400, | |
95 | message: 'Invalid url.', | |
96 | data: { | |
97 | url | |
98 | } | |
99 | }) | |
a2431b7d | 100 | } |
d8755eed | 101 | |
e5d9877f | 102 | const elementId = toCompleteUUID(parts.pop()) |
6fad8e51 | 103 | if (isIdOrUUIDValid(elementId) === false) { |
76148b27 | 104 | return res.fail({ message: 'Invalid video or playlist id.' }) |
a2431b7d | 105 | } |
d8755eed | 106 | |
6fad8e51 | 107 | if (isVideo) { |
868fce62 | 108 | const video = await loadVideo(elementId, 'all') |
6fad8e51 C |
109 | |
110 | if (!video) { | |
76148b27 RK |
111 | return res.fail({ |
112 | status: HttpStatusCode.NOT_FOUND_404, | |
113 | message: 'Video not found' | |
114 | }) | |
6fad8e51 | 115 | } |
d8755eed | 116 | |
b3d9dedc C |
117 | if ( |
118 | video.privacy === VideoPrivacy.PUBLIC || | |
119 | (video.privacy === VideoPrivacy.UNLISTED && isUUIDValid(elementId) === true) | |
120 | ) { | |
121 | res.locals.videoAll = video | |
122 | return next() | |
6fad8e51 C |
123 | } |
124 | ||
b3d9dedc C |
125 | return res.fail({ |
126 | status: HttpStatusCode.FORBIDDEN_403, | |
127 | message: 'Video is not publicly available' | |
128 | }) | |
6fad8e51 C |
129 | } |
130 | ||
131 | // Is playlist | |
132 | ||
133 | const videoPlaylist = await VideoPlaylistModel.loadWithAccountAndChannelSummary(elementId, undefined) | |
134 | if (!videoPlaylist) { | |
76148b27 RK |
135 | return res.fail({ |
136 | status: HttpStatusCode.NOT_FOUND_404, | |
137 | message: 'Video playlist not found' | |
138 | }) | |
6fad8e51 C |
139 | } |
140 | ||
b3d9dedc C |
141 | if ( |
142 | videoPlaylist.privacy === VideoPlaylistPrivacy.PUBLIC || | |
143 | (videoPlaylist.privacy === VideoPlaylistPrivacy.UNLISTED && isUUIDValid(elementId)) | |
144 | ) { | |
145 | res.locals.videoPlaylistSummary = videoPlaylist | |
146 | return next() | |
6fad8e51 C |
147 | } |
148 | ||
b3d9dedc C |
149 | return res.fail({ |
150 | status: HttpStatusCode.FORBIDDEN_403, | |
151 | message: 'Playlist is not public' | |
152 | }) | |
d8755eed | 153 | } |
6fad8e51 | 154 | |
d8755eed C |
155 | ] |
156 | ||
157 | // --------------------------------------------------------------------------- | |
158 | ||
159 | export { | |
160 | oembedValidator | |
161 | } |