]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/videos.ts
Add ability to delete comments
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos.ts
1 import * as express from 'express'
2 import 'express-validator'
3 import { body, param, query } from 'express-validator/check'
4 import { UserRight, VideoPrivacy } from '../../../shared'
5 import { isBooleanValid, isIdOrUUIDValid, isIdValid } from '../../helpers/custom-validators/misc'
6 import {
7 isVideoAbuseReasonValid, isVideoCategoryValid, isVideoDescriptionValid, isVideoExist, isVideoFile, isVideoLanguageValid,
8 isVideoLicenceValid, isVideoNameValid, isVideoPrivacyValid, isVideoRatingTypeValid, isVideoTagsValid
9 } from '../../helpers/custom-validators/videos'
10 import { getDurationFromVideoFile } from '../../helpers/ffmpeg-utils'
11 import { logger } from '../../helpers/logger'
12 import { CONSTRAINTS_FIELDS } from '../../initializers'
13 import { UserModel } from '../../models/account/user'
14 import { VideoModel } from '../../models/video/video'
15 import { VideoChannelModel } from '../../models/video/video-channel'
16 import { VideoShareModel } from '../../models/video/video-share'
17 import { authenticate } from '../oauth'
18 import { areValidationErrors } from './utils'
19
20 const videosAddValidator = [
21 body('videofile').custom((value, { req }) => isVideoFile(req.files)).withMessage(
22 'This file is not supported. Please, make sure it is of the following type : '
23 + CONSTRAINTS_FIELDS.VIDEOS.EXTNAME.join(', ')
24 ),
25 body('name').custom(isVideoNameValid).withMessage('Should have a valid name'),
26 body('category').optional().custom(isVideoCategoryValid).withMessage('Should have a valid category'),
27 body('licence').optional().custom(isVideoLicenceValid).withMessage('Should have a valid licence'),
28 body('language').optional().custom(isVideoLanguageValid).withMessage('Should have a valid language'),
29 body('nsfw').custom(isBooleanValid).withMessage('Should have a valid NSFW attribute'),
30 body('description').optional().custom(isVideoDescriptionValid).withMessage('Should have a valid description'),
31 body('channelId').custom(isIdValid).withMessage('Should have correct video channel id'),
32 body('privacy').custom(isVideoPrivacyValid).withMessage('Should have correct video privacy'),
33 body('tags').optional().custom(isVideoTagsValid).withMessage('Should have correct tags'),
34 body('commentsEnabled').custom(isBooleanValid).withMessage('Should have comments enabled boolean'),
35
36 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
37 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
38
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
43
44 const videoChannel = await VideoChannelModel.loadByIdAndAccount(req.body.channelId, user.Account.id)
45 if (!videoChannel) {
46 res.status(400)
47 .json({ error: 'Unknown video video channel for this account.' })
48 .end()
49
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
77 videoFile['duration'] = duration
78
79 return next()
80 }
81 ]
82
83 const videosUpdateValidator = [
84 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
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'),
89 body('nsfw').optional().custom(isBooleanValid).withMessage('Should have a valid NSFW attribute'),
90 body('privacy').optional().custom(isVideoPrivacyValid).withMessage('Should have correct video privacy'),
91 body('description').optional().custom(isVideoDescriptionValid).withMessage('Should have a valid description'),
92 body('tags').optional().custom(isVideoTagsValid).withMessage('Should have correct tags'),
93 body('commentsEnabled').optional().custom(isBooleanValid).withMessage('Should have comments enabled boolean'),
94
95 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
96 logger.debug('Checking videosUpdate parameters', { parameters: req.body })
97
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()
123 }
124 ]
125
126 const videosGetValidator = [
127 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
128
129 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
130 logger.debug('Checking videosGet parameters', { parameters: req.params })
131
132 if (areValidationErrors(req, res)) return
133 if (!await isVideoExist(req.params.id, res)) return
134
135 const video = res.locals.video
136
137 // Video is not private, anyone can access it
138 if (video.privacy !== VideoPrivacy.PRIVATE) return next()
139
140 authenticate(req, res, () => {
141 if (video.VideoChannel.Account.userId !== res.locals.oauth.token.User.id) {
142 return res.status(403)
143 .json({ error: 'Cannot get this private video of another user' })
144 .end()
145 }
146
147 return next()
148 })
149 }
150 ]
151
152 const videosRemoveValidator = [
153 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
154
155 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
156 logger.debug('Checking videosRemove parameters', { parameters: req.params })
157
158 if (areValidationErrors(req, res)) return
159 if (!await isVideoExist(req.params.id, res)) return
160
161 // Check if the user who did the request is able to delete the video
162 if (!checkUserCanDeleteVideo(res.locals.oauth.token.User, res.locals.video, res)) return
163
164 return next()
165 }
166 ]
167
168 const videosSearchValidator = [
169 query('search').not().isEmpty().withMessage('Should have a valid search'),
170
171 (req: express.Request, res: express.Response, next: express.NextFunction) => {
172 logger.debug('Checking videosSearch parameters', { parameters: req.params })
173
174 if (areValidationErrors(req, res)) return
175
176 return next()
177 }
178 ]
179
180 const videoAbuseReportValidator = [
181 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
182 body('reason').custom(isVideoAbuseReasonValid).withMessage('Should have a valid reason'),
183
184 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
185 logger.debug('Checking videoAbuseReport parameters', { parameters: req.body })
186
187 if (areValidationErrors(req, res)) return
188 if (!await isVideoExist(req.params.id, res)) return
189
190 return next()
191 }
192 ]
193
194 const videoRateValidator = [
195 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
196 body('rating').custom(isVideoRatingTypeValid).withMessage('Should have a valid rate type'),
197
198 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
199 logger.debug('Checking videoRate parameters', { parameters: req.body })
200
201 if (areValidationErrors(req, res)) return
202 if (!await isVideoExist(req.params.id, res)) return
203
204 return next()
205 }
206 ]
207
208 const videosShareValidator = [
209 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
210 param('accountId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid account id'),
211
212 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
213 logger.debug('Checking videoShare parameters', { parameters: req.params })
214
215 if (areValidationErrors(req, res)) return
216 if (!await isVideoExist(req.params.id, res)) return
217
218 const share = await VideoShareModel.load(req.params.accountId, res.locals.video.id, undefined)
219 if (!share) {
220 return res.status(404)
221 .end()
222 }
223
224 res.locals.videoShare = share
225 return next()
226 }
227 ]
228
229 // ---------------------------------------------------------------------------
230
231 export {
232 videosAddValidator,
233 videosUpdateValidator,
234 videosGetValidator,
235 videosRemoveValidator,
236 videosSearchValidator,
237 videosShareValidator,
238
239 videoAbuseReportValidator,
240
241 videoRateValidator
242 }
243
244 // ---------------------------------------------------------------------------
245
246 function checkUserCanDeleteVideo (user: UserModel, video: VideoModel, res: express.Response) {
247 // Retrieve the user who did the request
248 if (video.isOwned() === false) {
249 res.status(403)
250 .json({ error: 'Cannot remove video of another server, blacklist it' })
251 .end()
252 return false
253 }
254
255 // Check if the user can delete the video
256 // The user can delete it if he has the right
257 // Or if s/he is the video's account
258 const account = video.VideoChannel.Account
259 if (user.hasRight(UserRight.REMOVE_ANY_VIDEO) === false && account.userId !== user.id) {
260 res.status(403)
261 .json({ error: 'Cannot remove video of another user' })
262 .end()
263 return false
264 }
265
266 return true
267 }