]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/videos.ts
Fix lint
[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, SEARCHABLE_COLUMNS } 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').custom(isVideoCategoryValid).withMessage('Should have a valid category'),
35 body('licence').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').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 param('value').not().isEmpty().withMessage('Should have a valid search'),
176 query('field').optional().isIn(SEARCHABLE_COLUMNS.VIDEOS).withMessage('Should have correct searchable column'),
177
178 (req: express.Request, res: express.Response, next: express.NextFunction) => {
179 logger.debug('Checking videosSearch parameters', { parameters: req.params })
180
181 if (areValidationErrors(req, res)) return
182
183 return next()
184 }
185 ]
186
187 const videoAbuseReportValidator = [
188 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
189 body('reason').custom(isVideoAbuseReasonValid).withMessage('Should have a valid reason'),
190
191 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
192 logger.debug('Checking videoAbuseReport parameters', { parameters: req.body })
193
194 if (areValidationErrors(req, res)) return
195 if (!await isVideoExist(req.params.id, res)) return
196
197 return next()
198 }
199 ]
200
201 const videoRateValidator = [
202 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
203 body('rating').custom(isVideoRatingTypeValid).withMessage('Should have a valid rate type'),
204
205 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
206 logger.debug('Checking videoRate parameters', { parameters: req.body })
207
208 if (areValidationErrors(req, res)) return
209 if (!await isVideoExist(req.params.id, res)) return
210
211 return next()
212 }
213 ]
214
215 const videosShareValidator = [
216 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
217 param('accountId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid account id'),
218
219 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
220 logger.debug('Checking videoShare parameters', { parameters: req.params })
221
222 if (areValidationErrors(req, res)) return
223 if (!await isVideoExist(req.params.id, res)) return
224
225 const share = await db.VideoShare.load(req.params.accountId, res.locals.video.id, undefined)
226 if (!share) {
227 return res.status(404)
228 .end()
229 }
230
231 res.locals.videoShare = share
232 return next()
233 }
234 ]
235
236 // ---------------------------------------------------------------------------
237
238 export {
239 videosAddValidator,
240 videosUpdateValidator,
241 videosGetValidator,
242 videosRemoveValidator,
243 videosSearchValidator,
244 videosShareValidator,
245
246 videoAbuseReportValidator,
247
248 videoRateValidator
249 }
250
251 // ---------------------------------------------------------------------------
252
253 function checkUserCanDeleteVideo (user: UserInstance, video: VideoInstance, res: express.Response) {
254 // Retrieve the user who did the request
255 if (video.isOwned() === false) {
256 res.status(403)
257 .json({ error: 'Cannot remove video of another server, blacklist it' })
258 .end()
259 return false
260 }
261
262 // Check if the user can delete the video
263 // The user can delete it if s/he is an admin
264 // Or if s/he is the video's account
265 const account = video.VideoChannel.Account
266 if (user.hasRight(UserRight.REMOVE_ANY_VIDEO) === false && account.userId !== user.id) {
267 res.status(403)
268 .json({ error: 'Cannot remove video of another user' })
269 .end()
270 return false
271 }
272
273 return true
274 }