]>
Commit | Line | Data |
---|---|---|
1 | import express from 'express' | |
2 | import { query } from 'express-validator' | |
3 | import { join } from 'path' | |
4 | import { loadVideo } from '@server/lib/model-loaders' | |
5 | import { VideoPlaylistModel } from '@server/models/video/video-playlist' | |
6 | import { VideoPlaylistPrivacy, VideoPrivacy } from '@shared/models' | |
7 | import { HttpStatusCode } from '../../../shared/models/http/http-error-codes' | |
8 | import { isTestInstance } from '../../helpers/core-utils' | |
9 | import { isIdOrUUIDValid, toCompleteUUID } from '../../helpers/custom-validators/misc' | |
10 | import { logger } from '../../helpers/logger' | |
11 | import { WEBSERVER } from '../../initializers/constants' | |
12 | import { areValidationErrors } from './shared' | |
13 | ||
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) | |
30 | ||
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 | |
37 | if (isTestInstance()) { | |
38 | isURLOptions.require_tld = false | |
39 | } | |
40 | ||
41 | const oembedValidator = [ | |
42 | query('url').isURL(isURLOptions).withMessage('Should have a valid url'), | |
43 | query('maxwidth').optional().isInt().withMessage('Should have a valid max width'), | |
44 | query('maxheight').optional().isInt().withMessage('Should have a valid max height'), | |
45 | query('format').optional().isIn([ 'xml', 'json' ]).withMessage('Should have a valid format'), | |
46 | ||
47 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
48 | logger.debug('Checking oembed parameters', { parameters: req.query }) | |
49 | ||
50 | if (areValidationErrors(req, res)) return | |
51 | ||
52 | if (req.query.format !== undefined && req.query.format !== 'json') { | |
53 | return res.fail({ | |
54 | status: HttpStatusCode.NOT_IMPLEMENTED_501, | |
55 | message: 'Requested format is not implemented on server.', | |
56 | data: { | |
57 | format: req.query.format | |
58 | } | |
59 | }) | |
60 | } | |
61 | ||
62 | const url = req.query.url as string | |
63 | ||
64 | let urlPath: string | |
65 | ||
66 | try { | |
67 | urlPath = new URL(url).pathname | |
68 | } catch (err) { | |
69 | return res.fail({ | |
70 | status: HttpStatusCode.BAD_REQUEST_400, | |
71 | message: err.message, | |
72 | data: { | |
73 | url | |
74 | } | |
75 | }) | |
76 | } | |
77 | ||
78 | const isPlaylist = startPlaylistURLs.some(u => url.startsWith(u)) | |
79 | const isVideo = isPlaylist ? false : startVideoURLs.some(u => url.startsWith(u)) | |
80 | ||
81 | const startIsOk = isVideo || isPlaylist | |
82 | ||
83 | const parts = urlPath.split('/') | |
84 | ||
85 | if (startIsOk === false || parts.length === 0) { | |
86 | return res.fail({ | |
87 | status: HttpStatusCode.BAD_REQUEST_400, | |
88 | message: 'Invalid url.', | |
89 | data: { | |
90 | url | |
91 | } | |
92 | }) | |
93 | } | |
94 | ||
95 | const elementId = toCompleteUUID(parts.pop()) | |
96 | if (isIdOrUUIDValid(elementId) === false) { | |
97 | return res.fail({ message: 'Invalid video or playlist id.' }) | |
98 | } | |
99 | ||
100 | if (isVideo) { | |
101 | const video = await loadVideo(elementId, 'all') | |
102 | ||
103 | if (!video) { | |
104 | return res.fail({ | |
105 | status: HttpStatusCode.NOT_FOUND_404, | |
106 | message: 'Video not found' | |
107 | }) | |
108 | } | |
109 | ||
110 | if (video.privacy !== VideoPrivacy.PUBLIC) { | |
111 | return res.fail({ | |
112 | status: HttpStatusCode.FORBIDDEN_403, | |
113 | message: 'Video is not public' | |
114 | }) | |
115 | } | |
116 | ||
117 | res.locals.videoAll = video | |
118 | return next() | |
119 | } | |
120 | ||
121 | // Is playlist | |
122 | ||
123 | const videoPlaylist = await VideoPlaylistModel.loadWithAccountAndChannelSummary(elementId, undefined) | |
124 | if (!videoPlaylist) { | |
125 | return res.fail({ | |
126 | status: HttpStatusCode.NOT_FOUND_404, | |
127 | message: 'Video playlist not found' | |
128 | }) | |
129 | } | |
130 | ||
131 | if (videoPlaylist.privacy !== VideoPlaylistPrivacy.PUBLIC) { | |
132 | return res.fail({ | |
133 | status: HttpStatusCode.FORBIDDEN_403, | |
134 | message: 'Playlist is not public' | |
135 | }) | |
136 | } | |
137 | ||
138 | res.locals.videoPlaylistSummary = videoPlaylist | |
139 | return next() | |
140 | } | |
141 | ||
142 | ] | |
143 | ||
144 | // --------------------------------------------------------------------------- | |
145 | ||
146 | export { | |
147 | oembedValidator | |
148 | } |