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