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