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