]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/videos.ts
10625e41d450a16f3b9a2b64188a39e7a51ab588
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos.ts
1 import * as express from 'express'
2 import { body, param, query } from 'express-validator/check'
3 import { UserRight, VideoPrivacy } from '../../../shared'
4 import { isIdOrUUIDValid, isIdValid } from '../../helpers/custom-validators/misc'
5 import {
6 isVideoAbuseReasonValid,
7 isVideoCategoryValid,
8 isVideoDescriptionValid,
9 isVideoExist,
10 isVideoFile,
11 isVideoLanguageValid,
12 isVideoLicenceValid,
13 isVideoNameValid,
14 isVideoNSFWValid,
15 isVideoPrivacyValid,
16 isVideoRatingTypeValid,
17 isVideoTagsValid
18 } from '../../helpers/custom-validators/videos'
19 import { getDurationFromVideoFile } from '../../helpers/ffmpeg-utils'
20 import { logger } from '../../helpers/logger'
21 import { CONSTRAINTS_FIELDS } from '../../initializers'
22 import { database as db } from '../../initializers/database'
23 import { UserInstance } from '../../models/account/user-interface'
24 import { VideoInstance } from '../../models/video/video-interface'
25 import { authenticate } from '../oauth'
26 import { areValidationErrors } from './utils'
27
28 const videosAddValidator = [
29 body('videofile').custom((value, { req }) => isVideoFile(req.files)).withMessage(
30 'This file is not supported. Please, make sure it is of the following type : '
31 + CONSTRAINTS_FIELDS.VIDEOS.EXTNAME.join(', ')
32 ),
33 body('name').custom(isVideoNameValid).withMessage('Should have a valid name'),
34 body('category').optional().custom(isVideoCategoryValid).withMessage('Should have a valid category'),
35 body('licence').optional().custom(isVideoLicenceValid).withMessage('Should have a valid licence'),
36 body('language').optional().custom(isVideoLanguageValid).withMessage('Should have a valid language'),
37 body('nsfw').custom(isVideoNSFWValid).withMessage('Should have a valid NSFW attribute'),
38 body('description').optional().custom(isVideoDescriptionValid).withMessage('Should have a valid description'),
39 body('channelId').custom(isIdValid).withMessage('Should have correct video channel id'),
40 body('privacy').custom(isVideoPrivacyValid).withMessage('Should have correct video privacy'),
41 body('tags').optional().custom(isVideoTagsValid).withMessage('Should have correct tags'),
42
43 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
44 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
45
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
50
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()
56
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
84 videoFile['duration'] = duration
85
86 return next()
87 }
88 ]
89
90 const videosUpdateValidator = [
91 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
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'),
97 body('privacy').optional().custom(isVideoPrivacyValid).withMessage('Should have correct video privacy'),
98 body('description').optional().custom(isVideoDescriptionValid).withMessage('Should have a valid description'),
99 body('tags').optional().custom(isVideoTagsValid).withMessage('Should have correct tags'),
100
101 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
102 logger.debug('Checking videosUpdate parameters', { parameters: req.body })
103
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()
129 }
130 ]
131
132 const videosGetValidator = [
133 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
134
135 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
136 logger.debug('Checking videosGet parameters', { parameters: req.params })
137
138 if (areValidationErrors(req, res)) return
139 if (!await isVideoExist(req.params.id, res)) return
140
141 const video = res.locals.video
142
143 // Video is not private, anyone can access it
144 if (video.privacy !== VideoPrivacy.PRIVATE) return next()
145
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()
154 })
155 }
156 ]
157
158 const videosRemoveValidator = [
159 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
160
161 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
162 logger.debug('Checking videosRemove parameters', { parameters: req.params })
163
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()
171 }
172 ]
173
174 const videosSearchValidator = [
175 query('search').not().isEmpty().withMessage('Should have a valid search'),
176
177 (req: express.Request, res: express.Response, next: express.NextFunction) => {
178 logger.debug('Checking videosSearch parameters', { parameters: req.params })
179
180 if (areValidationErrors(req, res)) return
181
182 return next()
183 }
184 ]
185
186 const videoAbuseReportValidator = [
187 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
188 body('reason').custom(isVideoAbuseReasonValid).withMessage('Should have a valid reason'),
189
190 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
191 logger.debug('Checking videoAbuseReport parameters', { parameters: req.body })
192
193 if (areValidationErrors(req, res)) return
194 if (!await isVideoExist(req.params.id, res)) return
195
196 return next()
197 }
198 ]
199
200 const videoRateValidator = [
201 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
202 body('rating').custom(isVideoRatingTypeValid).withMessage('Should have a valid rate type'),
203
204 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
205 logger.debug('Checking videoRate parameters', { parameters: req.body })
206
207 if (areValidationErrors(req, res)) return
208 if (!await isVideoExist(req.params.id, res)) return
209
210 return next()
211 }
212 ]
213
214 const 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
222 if (!await isVideoExist(req.params.id, res)) return
223
224 const share = await db.VideoShare.load(req.params.accountId, res.locals.video.id, undefined)
225 if (!share) {
226 return res.status(404)
227 .end()
228 }
229
230 res.locals.videoShare = share
231 return next()
232 }
233 ]
234
235 // ---------------------------------------------------------------------------
236
237 export {
238 videosAddValidator,
239 videosUpdateValidator,
240 videosGetValidator,
241 videosRemoveValidator,
242 videosSearchValidator,
243 videosShareValidator,
244
245 videoAbuseReportValidator,
246
247 videoRateValidator
248 }
249
250 // ---------------------------------------------------------------------------
251
252 function checkUserCanDeleteVideo (user: UserInstance, video: VideoInstance, res: express.Response) {
253 // Retrieve the user who did the request
254 if (video.isOwned() === false) {
255 res.status(403)
256 .json({ error: 'Cannot remove video of another server, blacklist it' })
257 .end()
258 return false
259 }
260
261 // Check if the user can delete the video
262 // The user can delete it if s/he is an admin
263 // Or if s/he is the video's account
264 const account = video.VideoChannel.Account
265 if (user.hasRight(UserRight.REMOVE_ANY_VIDEO) === false && account.userId !== user.id) {
266 res.status(403)
267 .json({ error: 'Cannot remove video of another user' })
268 .end()
269 return false
270 }
271
272 return true
273 }