]>
Commit | Line | Data |
---|---|---|
69818c93 | 1 | import * as express from 'express' |
3fd3ab2d | 2 | import 'express-validator' |
8d468a16 C |
3 | import { body, param, query } from 'express-validator/check' |
4 | import { UserRight, VideoPrivacy } from '../../../shared' | |
81ebea48 | 5 | import { isBooleanValid, isIdOrUUIDValid, isIdValid, isUUIDValid } from '../../helpers/custom-validators/misc' |
b60e5f38 | 6 | import { |
ac81d1a0 C |
7 | isVideoAbuseReasonValid, |
8 | isVideoCategoryValid, | |
9 | isVideoDescriptionValid, | |
10 | isVideoExist, | |
11 | isVideoFile, | |
12 | isVideoImage, | |
13 | isVideoLanguageValid, | |
14 | isVideoLicenceValid, | |
15 | isVideoNameValid, | |
16 | isVideoPrivacyValid, | |
2422c46b | 17 | isVideoRatingTypeValid, isVideoSupportValid, |
ac81d1a0 | 18 | isVideoTagsValid |
8d468a16 | 19 | } from '../../helpers/custom-validators/videos' |
da854ddd C |
20 | import { getDurationFromVideoFile } from '../../helpers/ffmpeg-utils' |
21 | import { logger } from '../../helpers/logger' | |
f3aaa9a9 | 22 | import { CONSTRAINTS_FIELDS } from '../../initializers' |
3fd3ab2d C |
23 | import { UserModel } from '../../models/account/user' |
24 | import { VideoModel } from '../../models/video/video' | |
25 | import { VideoChannelModel } from '../../models/video/video-channel' | |
26 | import { VideoShareModel } from '../../models/video/video-share' | |
11474c3c | 27 | import { authenticate } from '../oauth' |
a2431b7d | 28 | import { areValidationErrors } from './utils' |
34ca3b52 | 29 | |
b60e5f38 | 30 | const videosAddValidator = [ |
8376734e | 31 | body('videofile').custom((value, { req }) => isVideoFile(req.files)).withMessage( |
10db166b C |
32 | 'This file is not supported. Please, make sure it is of the following type : ' |
33 | + CONSTRAINTS_FIELDS.VIDEOS.EXTNAME.join(', ') | |
8376734e | 34 | ), |
ac81d1a0 C |
35 | body('thumbnailfile').custom((value, { req }) => isVideoImage(req.files, 'thumbnailfile')).withMessage( |
36 | 'This thumbnail file is not supported. Please, make sure it is of the following type : ' | |
37 | + CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME.join(', ') | |
38 | ), | |
39 | body('previewfile').custom((value, { req }) => isVideoImage(req.files, 'previewfile')).withMessage( | |
40 | 'This preview file is not supported. Please, make sure it is of the following type : ' | |
41 | + CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME.join(', ') | |
42 | ), | |
b60e5f38 | 43 | body('name').custom(isVideoNameValid).withMessage('Should have a valid name'), |
8e7f08b5 C |
44 | body('category').optional().custom(isVideoCategoryValid).withMessage('Should have a valid category'), |
45 | body('licence').optional().custom(isVideoLicenceValid).withMessage('Should have a valid licence'), | |
b60e5f38 | 46 | body('language').optional().custom(isVideoLanguageValid).withMessage('Should have a valid language'), |
47564bbe | 47 | body('nsfw').custom(isBooleanValid).withMessage('Should have a valid NSFW attribute'), |
8e7f08b5 | 48 | body('description').optional().custom(isVideoDescriptionValid).withMessage('Should have a valid description'), |
2422c46b | 49 | body('support').optional().custom(isVideoSupportValid).withMessage('Should have a valid support text'), |
72c7248b | 50 | body('channelId').custom(isIdValid).withMessage('Should have correct video channel id'), |
fd45e8f4 | 51 | body('privacy').custom(isVideoPrivacyValid).withMessage('Should have correct video privacy'), |
b60e5f38 | 52 | body('tags').optional().custom(isVideoTagsValid).withMessage('Should have correct tags'), |
47564bbe | 53 | body('commentsEnabled').custom(isBooleanValid).withMessage('Should have comments enabled boolean'), |
b60e5f38 | 54 | |
a2431b7d | 55 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
b60e5f38 C |
56 | logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files }) |
57 | ||
a2431b7d | 58 | if (areValidationErrors(req, res)) return |
ac81d1a0 | 59 | if (areErrorsInVideoImageFiles(req, res)) return |
a2431b7d C |
60 | |
61 | const videoFile: Express.Multer.File = req.files['videofile'][0] | |
62 | const user = res.locals.oauth.token.User | |
b60e5f38 | 63 | |
3fd3ab2d | 64 | const videoChannel = await VideoChannelModel.loadByIdAndAccount(req.body.channelId, user.Account.id) |
a2431b7d C |
65 | if (!videoChannel) { |
66 | res.status(400) | |
67 | .json({ error: 'Unknown video video channel for this account.' }) | |
68 | .end() | |
72c7248b | 69 | |
a2431b7d C |
70 | return |
71 | } | |
72 | ||
73 | res.locals.videoChannel = videoChannel | |
74 | ||
75 | const isAble = await user.isAbleToUploadVideo(videoFile) | |
76 | if (isAble === false) { | |
77 | res.status(403) | |
78 | .json({ error: 'The user video quota is exceeded with this video.' }) | |
79 | .end() | |
80 | ||
81 | return | |
82 | } | |
83 | ||
84 | let duration: number | |
85 | ||
86 | try { | |
87 | duration = await getDurationFromVideoFile(videoFile.path) | |
88 | } catch (err) { | |
d5b7d911 | 89 | logger.error('Invalid input file in videosAddValidator.', { err }) |
a2431b7d C |
90 | res.status(400) |
91 | .json({ error: 'Invalid input file.' }) | |
92 | .end() | |
93 | ||
94 | return | |
95 | } | |
96 | ||
a2431b7d C |
97 | videoFile['duration'] = duration |
98 | ||
99 | return next() | |
b60e5f38 C |
100 | } |
101 | ] | |
102 | ||
103 | const videosUpdateValidator = [ | |
72c7248b | 104 | param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), |
ac81d1a0 C |
105 | body('thumbnailfile').custom((value, { req }) => isVideoImage(req.files, 'thumbnailfile')).withMessage( |
106 | 'This thumbnail file is not supported. Please, make sure it is of the following type : ' | |
107 | + CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME.join(', ') | |
108 | ), | |
109 | body('previewfile').custom((value, { req }) => isVideoImage(req.files, 'previewfile')).withMessage( | |
110 | 'This preview file is not supported. Please, make sure it is of the following type : ' | |
111 | + CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME.join(', ') | |
112 | ), | |
b60e5f38 C |
113 | body('name').optional().custom(isVideoNameValid).withMessage('Should have a valid name'), |
114 | body('category').optional().custom(isVideoCategoryValid).withMessage('Should have a valid category'), | |
115 | body('licence').optional().custom(isVideoLicenceValid).withMessage('Should have a valid licence'), | |
116 | body('language').optional().custom(isVideoLanguageValid).withMessage('Should have a valid language'), | |
47564bbe | 117 | body('nsfw').optional().custom(isBooleanValid).withMessage('Should have a valid NSFW attribute'), |
11474c3c | 118 | body('privacy').optional().custom(isVideoPrivacyValid).withMessage('Should have correct video privacy'), |
b60e5f38 | 119 | body('description').optional().custom(isVideoDescriptionValid).withMessage('Should have a valid description'), |
2422c46b | 120 | body('support').optional().custom(isVideoSupportValid).withMessage('Should have a valid support text'), |
b60e5f38 | 121 | body('tags').optional().custom(isVideoTagsValid).withMessage('Should have correct tags'), |
47564bbe | 122 | body('commentsEnabled').optional().custom(isBooleanValid).withMessage('Should have comments enabled boolean'), |
b60e5f38 | 123 | |
a2431b7d | 124 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
b60e5f38 C |
125 | logger.debug('Checking videosUpdate parameters', { parameters: req.body }) |
126 | ||
a2431b7d | 127 | if (areValidationErrors(req, res)) return |
ac81d1a0 | 128 | if (areErrorsInVideoImageFiles(req, res)) return |
a2431b7d C |
129 | if (!await isVideoExist(req.params.id, res)) return |
130 | ||
131 | const video = res.locals.video | |
132 | ||
6221f311 C |
133 | // Check if the user who did the request is able to update the video |
134 | if (!checkUserCanManageVideo(res.locals.oauth.token.User, res.locals.video, UserRight.UPDATE_ANY_VIDEO, res)) return | |
a2431b7d C |
135 | |
136 | if (video.privacy !== VideoPrivacy.PRIVATE && req.body.privacy === VideoPrivacy.PRIVATE) { | |
137 | return res.status(409) | |
138 | .json({ error: 'Cannot set "private" a video that was not private anymore.' }) | |
139 | .end() | |
140 | } | |
141 | ||
142 | return next() | |
b60e5f38 C |
143 | } |
144 | ] | |
c173e565 | 145 | |
b60e5f38 | 146 | const videosGetValidator = [ |
72c7248b | 147 | param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), |
34ca3b52 | 148 | |
a2431b7d | 149 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
b60e5f38 | 150 | logger.debug('Checking videosGet parameters', { parameters: req.params }) |
7b1f49de | 151 | |
a2431b7d C |
152 | if (areValidationErrors(req, res)) return |
153 | if (!await isVideoExist(req.params.id, res)) return | |
11474c3c | 154 | |
a2431b7d | 155 | const video = res.locals.video |
11474c3c | 156 | |
81ebea48 C |
157 | // Video is public, anyone can access it |
158 | if (video.privacy === VideoPrivacy.PUBLIC) return next() | |
11474c3c | 159 | |
81ebea48 C |
160 | // Video is unlisted, check we used the uuid to fetch it |
161 | if (video.privacy === VideoPrivacy.UNLISTED) { | |
162 | if (isUUIDValid(req.params.id)) return next() | |
163 | ||
164 | // Don't leak this unlisted video | |
165 | return res.status(404).end() | |
166 | } | |
167 | ||
168 | // Video is private, check the user | |
a2431b7d C |
169 | authenticate(req, res, () => { |
170 | if (video.VideoChannel.Account.userId !== res.locals.oauth.token.User.id) { | |
171 | return res.status(403) | |
172 | .json({ error: 'Cannot get this private video of another user' }) | |
173 | .end() | |
174 | } | |
175 | ||
176 | return next() | |
b60e5f38 C |
177 | }) |
178 | } | |
179 | ] | |
34ca3b52 | 180 | |
b60e5f38 | 181 | const videosRemoveValidator = [ |
72c7248b | 182 | param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), |
34ca3b52 | 183 | |
a2431b7d | 184 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
b60e5f38 | 185 | logger.debug('Checking videosRemove parameters', { parameters: req.params }) |
34ca3b52 | 186 | |
a2431b7d C |
187 | if (areValidationErrors(req, res)) return |
188 | if (!await isVideoExist(req.params.id, res)) return | |
189 | ||
190 | // Check if the user who did the request is able to delete the video | |
6221f311 | 191 | if (!checkUserCanManageVideo(res.locals.oauth.token.User, res.locals.video, UserRight.REMOVE_ANY_VIDEO, res)) return |
a2431b7d C |
192 | |
193 | return next() | |
b60e5f38 C |
194 | } |
195 | ] | |
34ca3b52 | 196 | |
b60e5f38 | 197 | const videosSearchValidator = [ |
f3aaa9a9 | 198 | query('search').not().isEmpty().withMessage('Should have a valid search'), |
c45f7f84 | 199 | |
b60e5f38 C |
200 | (req: express.Request, res: express.Response, next: express.NextFunction) => { |
201 | logger.debug('Checking videosSearch parameters', { parameters: req.params }) | |
c45f7f84 | 202 | |
a2431b7d C |
203 | if (areValidationErrors(req, res)) return |
204 | ||
205 | return next() | |
b60e5f38 C |
206 | } |
207 | ] | |
c45f7f84 | 208 | |
b60e5f38 | 209 | const videoAbuseReportValidator = [ |
72c7248b | 210 | param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), |
b60e5f38 | 211 | body('reason').custom(isVideoAbuseReasonValid).withMessage('Should have a valid reason'), |
55fa55a9 | 212 | |
a2431b7d | 213 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
b60e5f38 | 214 | logger.debug('Checking videoAbuseReport parameters', { parameters: req.body }) |
55fa55a9 | 215 | |
a2431b7d C |
216 | if (areValidationErrors(req, res)) return |
217 | if (!await isVideoExist(req.params.id, res)) return | |
218 | ||
219 | return next() | |
b60e5f38 C |
220 | } |
221 | ] | |
55fa55a9 | 222 | |
b60e5f38 | 223 | const videoRateValidator = [ |
72c7248b | 224 | param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), |
b60e5f38 | 225 | body('rating').custom(isVideoRatingTypeValid).withMessage('Should have a valid rate type'), |
d38b8281 | 226 | |
a2431b7d | 227 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
b60e5f38 | 228 | logger.debug('Checking videoRate parameters', { parameters: req.body }) |
d38b8281 | 229 | |
a2431b7d C |
230 | if (areValidationErrors(req, res)) return |
231 | if (!await isVideoExist(req.params.id, res)) return | |
232 | ||
233 | return next() | |
b60e5f38 C |
234 | } |
235 | ] | |
d38b8281 | 236 | |
4e50b6a1 C |
237 | const videosShareValidator = [ |
238 | param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), | |
239 | param('accountId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid account id'), | |
240 | ||
241 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
242 | logger.debug('Checking videoShare parameters', { parameters: req.params }) | |
243 | ||
244 | if (areValidationErrors(req, res)) return | |
a2431b7d | 245 | if (!await isVideoExist(req.params.id, res)) return |
4e50b6a1 | 246 | |
3fd3ab2d | 247 | const share = await VideoShareModel.load(req.params.accountId, res.locals.video.id, undefined) |
4e50b6a1 C |
248 | if (!share) { |
249 | return res.status(404) | |
250 | .end() | |
251 | } | |
252 | ||
253 | res.locals.videoShare = share | |
4e50b6a1 C |
254 | return next() |
255 | } | |
256 | ] | |
257 | ||
9f10b292 | 258 | // --------------------------------------------------------------------------- |
c45f7f84 | 259 | |
65fcc311 C |
260 | export { |
261 | videosAddValidator, | |
262 | videosUpdateValidator, | |
263 | videosGetValidator, | |
264 | videosRemoveValidator, | |
265 | videosSearchValidator, | |
4e50b6a1 | 266 | videosShareValidator, |
65fcc311 C |
267 | |
268 | videoAbuseReportValidator, | |
269 | ||
35bf0c83 | 270 | videoRateValidator |
65fcc311 | 271 | } |
7b1f49de C |
272 | |
273 | // --------------------------------------------------------------------------- | |
274 | ||
6221f311 | 275 | function checkUserCanManageVideo (user: UserModel, video: VideoModel, right: UserRight, res: express.Response) { |
198b205c | 276 | // Retrieve the user who did the request |
a2431b7d C |
277 | if (video.isOwned() === false) { |
278 | res.status(403) | |
60862425 | 279 | .json({ error: 'Cannot remove video of another server, blacklist it' }) |
11474c3c | 280 | .end() |
a2431b7d | 281 | return false |
11474c3c C |
282 | } |
283 | ||
284 | // Check if the user can delete the video | |
4cb6d457 | 285 | // The user can delete it if he has the right |
38fa2065 | 286 | // Or if s/he is the video's account |
a2431b7d | 287 | const account = video.VideoChannel.Account |
6221f311 | 288 | if (user.hasRight(right) === false && account.userId !== user.id) { |
a2431b7d | 289 | res.status(403) |
11474c3c C |
290 | .json({ error: 'Cannot remove video of another user' }) |
291 | .end() | |
a2431b7d | 292 | return false |
11474c3c C |
293 | } |
294 | ||
a2431b7d | 295 | return true |
198b205c | 296 | } |
ac81d1a0 C |
297 | |
298 | function areErrorsInVideoImageFiles (req: express.Request, res: express.Response) { | |
299 | // Files are optional | |
300 | if (!req.files) return false | |
301 | ||
302 | for (const imageField of [ 'thumbnail', 'preview' ]) { | |
303 | if (!req.files[ imageField ]) continue | |
304 | ||
305 | const imageFile = req.files[ imageField ][ 0 ] as Express.Multer.File | |
306 | if (imageFile.size > CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max) { | |
307 | res.status(400) | |
308 | .send({ error: `The size of the ${imageField} is too big (>${CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max}).` }) | |
309 | .end() | |
310 | return true | |
311 | } | |
312 | } | |
313 | ||
314 | return false | |
315 | } |