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