]>
Commit | Line | Data |
---|---|---|
69818c93 | 1 | import * as express from 'express' |
8d468a16 C |
2 | import { body, param, query } from 'express-validator/check' |
3 | import { UserRight, VideoPrivacy } from '../../../shared' | |
4 | import { isIdOrUUIDValid, isIdValid } from '../../helpers/custom-validators/misc' | |
b60e5f38 | 5 | import { |
8d468a16 | 6 | isVideoAbuseReasonValid, |
b60e5f38 | 7 | isVideoCategoryValid, |
b60e5f38 | 8 | isVideoDescriptionValid, |
a2431b7d | 9 | isVideoExist, |
8d468a16 | 10 | isVideoFile, |
b60e5f38 | 11 | isVideoLanguageValid, |
8d468a16 C |
12 | isVideoLicenceValid, |
13 | isVideoNameValid, | |
b60e5f38 | 14 | isVideoNSFWValid, |
8d468a16 | 15 | isVideoPrivacyValid, |
14d3270f | 16 | isVideoRatingTypeValid, |
8d468a16 C |
17 | isVideoTagsValid |
18 | } from '../../helpers/custom-validators/videos' | |
19 | import { getDurationFromVideoFile } from '../../helpers/ffmpeg-utils' | |
20 | import { logger } from '../../helpers/logger' | |
21 | import { CONSTRAINTS_FIELDS, SEARCHABLE_COLUMNS } from '../../initializers' | |
8d468a16 C |
22 | import { database as db } from '../../initializers/database' |
23 | import { UserInstance } from '../../models/account/user-interface' | |
a2431b7d | 24 | import { VideoInstance } from '../../models/video/video-interface' |
11474c3c | 25 | import { authenticate } from '../oauth' |
a2431b7d | 26 | import { areValidationErrors } from './utils' |
34ca3b52 | 27 | |
b60e5f38 | 28 | const videosAddValidator = [ |
8376734e | 29 | body('videofile').custom((value, { req }) => isVideoFile(req.files)).withMessage( |
10db166b C |
30 | 'This file is not supported. Please, make sure it is of the following type : ' |
31 | + CONSTRAINTS_FIELDS.VIDEOS.EXTNAME.join(', ') | |
8376734e | 32 | ), |
b60e5f38 C |
33 | body('name').custom(isVideoNameValid).withMessage('Should have a valid name'), |
34 | body('category').custom(isVideoCategoryValid).withMessage('Should have a valid category'), | |
35 | body('licence').custom(isVideoLicenceValid).withMessage('Should have a valid licence'), | |
36 | body('language').optional().custom(isVideoLanguageValid).withMessage('Should have a valid language'), | |
37 | body('nsfw').custom(isVideoNSFWValid).withMessage('Should have a valid NSFW attribute'), | |
38 | body('description').custom(isVideoDescriptionValid).withMessage('Should have a valid description'), | |
72c7248b | 39 | body('channelId').custom(isIdValid).withMessage('Should have correct video channel id'), |
fd45e8f4 | 40 | body('privacy').custom(isVideoPrivacyValid).withMessage('Should have correct video privacy'), |
b60e5f38 C |
41 | body('tags').optional().custom(isVideoTagsValid).withMessage('Should have correct tags'), |
42 | ||
a2431b7d | 43 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
b60e5f38 C |
44 | logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files }) |
45 | ||
a2431b7d C |
46 | if (areValidationErrors(req, res)) return |
47 | ||
48 | const videoFile: Express.Multer.File = req.files['videofile'][0] | |
49 | const user = res.locals.oauth.token.User | |
b60e5f38 | 50 | |
a2431b7d C |
51 | const videoChannel = await db.VideoChannel.loadByIdAndAccount(req.body.channelId, user.Account.id) |
52 | if (!videoChannel) { | |
53 | res.status(400) | |
54 | .json({ error: 'Unknown video video channel for this account.' }) | |
55 | .end() | |
72c7248b | 56 | |
a2431b7d C |
57 | return |
58 | } | |
59 | ||
60 | res.locals.videoChannel = videoChannel | |
61 | ||
62 | const isAble = await user.isAbleToUploadVideo(videoFile) | |
63 | if (isAble === false) { | |
64 | res.status(403) | |
65 | .json({ error: 'The user video quota is exceeded with this video.' }) | |
66 | .end() | |
67 | ||
68 | return | |
69 | } | |
70 | ||
71 | let duration: number | |
72 | ||
73 | try { | |
74 | duration = await getDurationFromVideoFile(videoFile.path) | |
75 | } catch (err) { | |
76 | logger.error('Invalid input file in videosAddValidator.', err) | |
77 | res.status(400) | |
78 | .json({ error: 'Invalid input file.' }) | |
79 | .end() | |
80 | ||
81 | return | |
82 | } | |
83 | ||
a2431b7d C |
84 | videoFile['duration'] = duration |
85 | ||
86 | return next() | |
b60e5f38 C |
87 | } |
88 | ] | |
89 | ||
90 | const videosUpdateValidator = [ | |
72c7248b | 91 | param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), |
b60e5f38 C |
92 | body('name').optional().custom(isVideoNameValid).withMessage('Should have a valid name'), |
93 | body('category').optional().custom(isVideoCategoryValid).withMessage('Should have a valid category'), | |
94 | body('licence').optional().custom(isVideoLicenceValid).withMessage('Should have a valid licence'), | |
95 | body('language').optional().custom(isVideoLanguageValid).withMessage('Should have a valid language'), | |
96 | body('nsfw').optional().custom(isVideoNSFWValid).withMessage('Should have a valid NSFW attribute'), | |
11474c3c | 97 | body('privacy').optional().custom(isVideoPrivacyValid).withMessage('Should have correct video privacy'), |
b60e5f38 C |
98 | body('description').optional().custom(isVideoDescriptionValid).withMessage('Should have a valid description'), |
99 | body('tags').optional().custom(isVideoTagsValid).withMessage('Should have correct tags'), | |
100 | ||
a2431b7d | 101 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
b60e5f38 C |
102 | logger.debug('Checking videosUpdate parameters', { parameters: req.body }) |
103 | ||
a2431b7d C |
104 | if (areValidationErrors(req, res)) return |
105 | if (!await isVideoExist(req.params.id, res)) return | |
106 | ||
107 | const video = res.locals.video | |
108 | ||
109 | // We need to make additional checks | |
110 | if (video.isOwned() === false) { | |
111 | return res.status(403) | |
112 | .json({ error: 'Cannot update video of another server' }) | |
113 | .end() | |
114 | } | |
115 | ||
116 | if (video.VideoChannel.Account.userId !== res.locals.oauth.token.User.id) { | |
117 | return res.status(403) | |
118 | .json({ error: 'Cannot update video of another user' }) | |
119 | .end() | |
120 | } | |
121 | ||
122 | if (video.privacy !== VideoPrivacy.PRIVATE && req.body.privacy === VideoPrivacy.PRIVATE) { | |
123 | return res.status(409) | |
124 | .json({ error: 'Cannot set "private" a video that was not private anymore.' }) | |
125 | .end() | |
126 | } | |
127 | ||
128 | return next() | |
b60e5f38 C |
129 | } |
130 | ] | |
c173e565 | 131 | |
b60e5f38 | 132 | const videosGetValidator = [ |
72c7248b | 133 | param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), |
34ca3b52 | 134 | |
a2431b7d | 135 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
b60e5f38 | 136 | logger.debug('Checking videosGet parameters', { parameters: req.params }) |
7b1f49de | 137 | |
a2431b7d C |
138 | if (areValidationErrors(req, res)) return |
139 | if (!await isVideoExist(req.params.id, res)) return | |
11474c3c | 140 | |
a2431b7d | 141 | const video = res.locals.video |
11474c3c | 142 | |
a2431b7d C |
143 | // Video is not private, anyone can access it |
144 | if (video.privacy !== VideoPrivacy.PRIVATE) return next() | |
11474c3c | 145 | |
a2431b7d C |
146 | authenticate(req, res, () => { |
147 | if (video.VideoChannel.Account.userId !== res.locals.oauth.token.User.id) { | |
148 | return res.status(403) | |
149 | .json({ error: 'Cannot get this private video of another user' }) | |
150 | .end() | |
151 | } | |
152 | ||
153 | return next() | |
b60e5f38 C |
154 | }) |
155 | } | |
156 | ] | |
34ca3b52 | 157 | |
b60e5f38 | 158 | const videosRemoveValidator = [ |
72c7248b | 159 | param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), |
34ca3b52 | 160 | |
a2431b7d | 161 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
b60e5f38 | 162 | logger.debug('Checking videosRemove parameters', { parameters: req.params }) |
34ca3b52 | 163 | |
a2431b7d C |
164 | if (areValidationErrors(req, res)) return |
165 | if (!await isVideoExist(req.params.id, res)) return | |
166 | ||
167 | // Check if the user who did the request is able to delete the video | |
168 | if (!checkUserCanDeleteVideo(res.locals.oauth.token.User, res.locals.video, res)) return | |
169 | ||
170 | return next() | |
b60e5f38 C |
171 | } |
172 | ] | |
34ca3b52 | 173 | |
b60e5f38 C |
174 | const videosSearchValidator = [ |
175 | param('value').not().isEmpty().withMessage('Should have a valid search'), | |
176 | query('field').optional().isIn(SEARCHABLE_COLUMNS.VIDEOS).withMessage('Should have correct searchable column'), | |
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 | |
98ec8b8e | 225 | const share = await db.VideoShare.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 | ||
a2431b7d | 253 | function checkUserCanDeleteVideo (user: UserInstance, video: VideoInstance, 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 | } |