]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/videos.ts
Fix crash with websocket tracker
[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 {
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
81ebea48
C
137 // Video is public, anyone can access it
138 if (video.privacy === VideoPrivacy.PUBLIC) return next()
11474c3c 139
81ebea48
C
140 // Video is unlisted, check we used the uuid to fetch it
141 if (video.privacy === VideoPrivacy.UNLISTED) {
142 if (isUUIDValid(req.params.id)) return next()
143
144 // Don't leak this unlisted video
145 return res.status(404).end()
146 }
147
148 // Video is private, check the user
a2431b7d
C
149 authenticate(req, res, () => {
150 if (video.VideoChannel.Account.userId !== res.locals.oauth.token.User.id) {
151 return res.status(403)
152 .json({ error: 'Cannot get this private video of another user' })
153 .end()
154 }
155
156 return next()
b60e5f38
C
157 })
158 }
159]
34ca3b52 160
b60e5f38 161const videosRemoveValidator = [
72c7248b 162 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
34ca3b52 163
a2431b7d 164 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
b60e5f38 165 logger.debug('Checking videosRemove parameters', { parameters: req.params })
34ca3b52 166
a2431b7d
C
167 if (areValidationErrors(req, res)) return
168 if (!await isVideoExist(req.params.id, res)) return
169
170 // Check if the user who did the request is able to delete the video
171 if (!checkUserCanDeleteVideo(res.locals.oauth.token.User, res.locals.video, res)) return
172
173 return next()
b60e5f38
C
174 }
175]
34ca3b52 176
b60e5f38 177const videosSearchValidator = [
f3aaa9a9 178 query('search').not().isEmpty().withMessage('Should have a valid search'),
c45f7f84 179
b60e5f38
C
180 (req: express.Request, res: express.Response, next: express.NextFunction) => {
181 logger.debug('Checking videosSearch parameters', { parameters: req.params })
c45f7f84 182
a2431b7d
C
183 if (areValidationErrors(req, res)) return
184
185 return next()
b60e5f38
C
186 }
187]
c45f7f84 188
b60e5f38 189const videoAbuseReportValidator = [
72c7248b 190 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
b60e5f38 191 body('reason').custom(isVideoAbuseReasonValid).withMessage('Should have a valid reason'),
55fa55a9 192
a2431b7d 193 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
b60e5f38 194 logger.debug('Checking videoAbuseReport parameters', { parameters: req.body })
55fa55a9 195
a2431b7d
C
196 if (areValidationErrors(req, res)) return
197 if (!await isVideoExist(req.params.id, res)) return
198
199 return next()
b60e5f38
C
200 }
201]
55fa55a9 202
b60e5f38 203const videoRateValidator = [
72c7248b 204 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
b60e5f38 205 body('rating').custom(isVideoRatingTypeValid).withMessage('Should have a valid rate type'),
d38b8281 206
a2431b7d 207 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
b60e5f38 208 logger.debug('Checking videoRate parameters', { parameters: req.body })
d38b8281 209
a2431b7d
C
210 if (areValidationErrors(req, res)) return
211 if (!await isVideoExist(req.params.id, res)) return
212
213 return next()
b60e5f38
C
214 }
215]
d38b8281 216
4e50b6a1
C
217const videosShareValidator = [
218 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
219 param('accountId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid account id'),
220
221 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
222 logger.debug('Checking videoShare parameters', { parameters: req.params })
223
224 if (areValidationErrors(req, res)) return
a2431b7d 225 if (!await isVideoExist(req.params.id, res)) return
4e50b6a1 226
3fd3ab2d 227 const share = await VideoShareModel.load(req.params.accountId, res.locals.video.id, undefined)
4e50b6a1
C
228 if (!share) {
229 return res.status(404)
230 .end()
231 }
232
233 res.locals.videoShare = share
4e50b6a1
C
234 return next()
235 }
236]
237
9f10b292 238// ---------------------------------------------------------------------------
c45f7f84 239
65fcc311
C
240export {
241 videosAddValidator,
242 videosUpdateValidator,
243 videosGetValidator,
244 videosRemoveValidator,
245 videosSearchValidator,
4e50b6a1 246 videosShareValidator,
65fcc311
C
247
248 videoAbuseReportValidator,
249
35bf0c83 250 videoRateValidator
65fcc311 251}
7b1f49de
C
252
253// ---------------------------------------------------------------------------
254
3fd3ab2d 255function checkUserCanDeleteVideo (user: UserModel, video: VideoModel, res: express.Response) {
198b205c 256 // Retrieve the user who did the request
a2431b7d
C
257 if (video.isOwned() === false) {
258 res.status(403)
60862425 259 .json({ error: 'Cannot remove video of another server, blacklist it' })
11474c3c 260 .end()
a2431b7d 261 return false
11474c3c
C
262 }
263
264 // Check if the user can delete the video
4cb6d457 265 // The user can delete it if he has the right
38fa2065 266 // Or if s/he is the video's account
a2431b7d 267 const account = video.VideoChannel.Account
38fa2065 268 if (user.hasRight(UserRight.REMOVE_ANY_VIDEO) === false && account.userId !== user.id) {
a2431b7d 269 res.status(403)
11474c3c
C
270 .json({ error: 'Cannot remove video of another user' })
271 .end()
a2431b7d 272 return false
11474c3c
C
273 }
274
a2431b7d 275 return true
198b205c 276}