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