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