]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/videos.ts
Fix lint
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos.ts
CommitLineData
69818c93 1import * as express from 'express'
3fd3ab2d 2import 'express-validator'
8d468a16
C
3import { body, param, query } from 'express-validator/check'
4import { UserRight, VideoPrivacy } from '../../../shared'
81ebea48 5import { isBooleanValid, isIdOrUUIDValid, isIdValid, isUUIDValid } from '../../helpers/custom-validators/misc'
b60e5f38 6import {
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
20import { getDurationFromVideoFile } from '../../helpers/ffmpeg-utils'
21import { logger } from '../../helpers/logger'
f3aaa9a9 22import { CONSTRAINTS_FIELDS } from '../../initializers'
3fd3ab2d
C
23import { UserModel } from '../../models/account/user'
24import { VideoModel } from '../../models/video/video'
25import { VideoChannelModel } from '../../models/video/video-channel'
26import { VideoShareModel } from '../../models/video/video-share'
11474c3c 27import { authenticate } from '../oauth'
a2431b7d 28import { areValidationErrors } from './utils'
34ca3b52 29
b60e5f38 30const 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) {
89 logger.error('Invalid input file in videosAddValidator.', err)
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
103const 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
133 // We need to make additional checks
134 if (video.isOwned() === false) {
135 return res.status(403)
136 .json({ error: 'Cannot update video of another server' })
137 .end()
138 }
139
140 if (video.VideoChannel.Account.userId !== res.locals.oauth.token.User.id) {
141 return res.status(403)
142 .json({ error: 'Cannot update video of another user' })
143 .end()
144 }
145
146 if (video.privacy !== VideoPrivacy.PRIVATE && req.body.privacy === VideoPrivacy.PRIVATE) {
147 return res.status(409)
148 .json({ error: 'Cannot set "private" a video that was not private anymore.' })
149 .end()
150 }
151
152 return next()
b60e5f38
C
153 }
154]
c173e565 155
b60e5f38 156const videosGetValidator = [
72c7248b 157 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
34ca3b52 158
a2431b7d 159 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
b60e5f38 160 logger.debug('Checking videosGet parameters', { parameters: req.params })
7b1f49de 161
a2431b7d
C
162 if (areValidationErrors(req, res)) return
163 if (!await isVideoExist(req.params.id, res)) return
11474c3c 164
a2431b7d 165 const video = res.locals.video
11474c3c 166
81ebea48
C
167 // Video is public, anyone can access it
168 if (video.privacy === VideoPrivacy.PUBLIC) return next()
11474c3c 169
81ebea48
C
170 // Video is unlisted, check we used the uuid to fetch it
171 if (video.privacy === VideoPrivacy.UNLISTED) {
172 if (isUUIDValid(req.params.id)) return next()
173
174 // Don't leak this unlisted video
175 return res.status(404).end()
176 }
177
178 // Video is private, check the user
a2431b7d
C
179 authenticate(req, res, () => {
180 if (video.VideoChannel.Account.userId !== res.locals.oauth.token.User.id) {
181 return res.status(403)
182 .json({ error: 'Cannot get this private video of another user' })
183 .end()
184 }
185
186 return next()
b60e5f38
C
187 })
188 }
189]
34ca3b52 190
b60e5f38 191const videosRemoveValidator = [
72c7248b 192 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
34ca3b52 193
a2431b7d 194 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
b60e5f38 195 logger.debug('Checking videosRemove parameters', { parameters: req.params })
34ca3b52 196
a2431b7d
C
197 if (areValidationErrors(req, res)) return
198 if (!await isVideoExist(req.params.id, res)) return
199
200 // Check if the user who did the request is able to delete the video
201 if (!checkUserCanDeleteVideo(res.locals.oauth.token.User, res.locals.video, res)) return
202
203 return next()
b60e5f38
C
204 }
205]
34ca3b52 206
b60e5f38 207const videosSearchValidator = [
f3aaa9a9 208 query('search').not().isEmpty().withMessage('Should have a valid search'),
c45f7f84 209
b60e5f38
C
210 (req: express.Request, res: express.Response, next: express.NextFunction) => {
211 logger.debug('Checking videosSearch parameters', { parameters: req.params })
c45f7f84 212
a2431b7d
C
213 if (areValidationErrors(req, res)) return
214
215 return next()
b60e5f38
C
216 }
217]
c45f7f84 218
b60e5f38 219const videoAbuseReportValidator = [
72c7248b 220 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
b60e5f38 221 body('reason').custom(isVideoAbuseReasonValid).withMessage('Should have a valid reason'),
55fa55a9 222
a2431b7d 223 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
b60e5f38 224 logger.debug('Checking videoAbuseReport parameters', { parameters: req.body })
55fa55a9 225
a2431b7d
C
226 if (areValidationErrors(req, res)) return
227 if (!await isVideoExist(req.params.id, res)) return
228
229 return next()
b60e5f38
C
230 }
231]
55fa55a9 232
b60e5f38 233const videoRateValidator = [
72c7248b 234 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
b60e5f38 235 body('rating').custom(isVideoRatingTypeValid).withMessage('Should have a valid rate type'),
d38b8281 236
a2431b7d 237 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
b60e5f38 238 logger.debug('Checking videoRate parameters', { parameters: req.body })
d38b8281 239
a2431b7d
C
240 if (areValidationErrors(req, res)) return
241 if (!await isVideoExist(req.params.id, res)) return
242
243 return next()
b60e5f38
C
244 }
245]
d38b8281 246
4e50b6a1
C
247const videosShareValidator = [
248 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
249 param('accountId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid account id'),
250
251 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
252 logger.debug('Checking videoShare parameters', { parameters: req.params })
253
254 if (areValidationErrors(req, res)) return
a2431b7d 255 if (!await isVideoExist(req.params.id, res)) return
4e50b6a1 256
3fd3ab2d 257 const share = await VideoShareModel.load(req.params.accountId, res.locals.video.id, undefined)
4e50b6a1
C
258 if (!share) {
259 return res.status(404)
260 .end()
261 }
262
263 res.locals.videoShare = share
4e50b6a1
C
264 return next()
265 }
266]
267
9f10b292 268// ---------------------------------------------------------------------------
c45f7f84 269
65fcc311
C
270export {
271 videosAddValidator,
272 videosUpdateValidator,
273 videosGetValidator,
274 videosRemoveValidator,
275 videosSearchValidator,
4e50b6a1 276 videosShareValidator,
65fcc311
C
277
278 videoAbuseReportValidator,
279
35bf0c83 280 videoRateValidator
65fcc311 281}
7b1f49de
C
282
283// ---------------------------------------------------------------------------
284
3fd3ab2d 285function checkUserCanDeleteVideo (user: UserModel, video: VideoModel, res: express.Response) {
198b205c 286 // Retrieve the user who did the request
a2431b7d
C
287 if (video.isOwned() === false) {
288 res.status(403)
60862425 289 .json({ error: 'Cannot remove video of another server, blacklist it' })
11474c3c 290 .end()
a2431b7d 291 return false
11474c3c
C
292 }
293
294 // Check if the user can delete the video
4cb6d457 295 // The user can delete it if he has the right
38fa2065 296 // Or if s/he is the video's account
a2431b7d 297 const account = video.VideoChannel.Account
38fa2065 298 if (user.hasRight(UserRight.REMOVE_ANY_VIDEO) === false && account.userId !== user.id) {
a2431b7d 299 res.status(403)
11474c3c
C
300 .json({ error: 'Cannot remove video of another user' })
301 .end()
a2431b7d 302 return false
11474c3c
C
303 }
304
a2431b7d 305 return true
198b205c 306}
ac81d1a0
C
307
308function areErrorsInVideoImageFiles (req: express.Request, res: express.Response) {
309 // Files are optional
310 if (!req.files) return false
311
312 for (const imageField of [ 'thumbnail', 'preview' ]) {
313 if (!req.files[ imageField ]) continue
314
315 const imageFile = req.files[ imageField ][ 0 ] as Express.Multer.File
316 if (imageFile.size > CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max) {
317 res.status(400)
318 .send({ error: `The size of the ${imageField} is too big (>${CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max}).` })
319 .end()
320 return true
321 }
322 }
323
324 return false
325}