Move to promises
[oweals/peertube.git] / server / middlewares / validators / videos.ts
1 import 'express-validator'
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 { logger, isVideoDurationValid } from '../../helpers'
8
9 function videosAddValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
10   // FIXME: Don't write an error message, it seems there is a bug with express-validator
11   // 'Should have a valid file'
12   req.checkBody('videofile').isVideoFile(req.files)
13   req.checkBody('name', 'Should have a valid name').isVideoNameValid()
14   req.checkBody('category', 'Should have a valid category').isVideoCategoryValid()
15   req.checkBody('licence', 'Should have a valid licence').isVideoLicenceValid()
16   req.checkBody('language', 'Should have a valid language').optional().isVideoLanguageValid()
17   req.checkBody('nsfw', 'Should have a valid NSFW attribute').isVideoNSFWValid()
18   req.checkBody('description', 'Should have a valid description').isVideoDescriptionValid()
19   req.checkBody('tags', 'Should have correct tags').optional().isVideoTagsValid()
20
21   logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
22
23   checkErrors(req, res, function () {
24     const videoFile = req.files.videofile[0]
25
26     db.Video.getDurationFromFile(videoFile.path)
27       .then(duration => {
28         if (!isVideoDurationValid('' + duration)) {
29           return res.status(400).send('Duration of the video file is too big (max: ' + CONSTRAINTS_FIELDS.VIDEOS.DURATION.max + 's).')
30         }
31
32         videoFile['duration'] = duration
33         next()
34       })
35       .catch(err => {
36         logger.error('Error in getting duration from file.', { error: err })
37         res.status(400).send('Cannot retrieve metadata of the file.')
38       })
39   })
40 }
41
42 function videosUpdateValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
43   req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
44   req.checkBody('name', 'Should have a valid name').optional().isVideoNameValid()
45   req.checkBody('category', 'Should have a valid category').optional().isVideoCategoryValid()
46   req.checkBody('licence', 'Should have a valid licence').optional().isVideoLicenceValid()
47   req.checkBody('language', 'Should have a valid language').optional().isVideoLanguageValid()
48   req.checkBody('nsfw', 'Should have a valid NSFW attribute').optional().isVideoNSFWValid()
49   req.checkBody('description', 'Should have a valid description').optional().isVideoDescriptionValid()
50   req.checkBody('tags', 'Should have correct tags').optional().isVideoTagsValid()
51
52   logger.debug('Checking videosUpdate parameters', { parameters: req.body })
53
54   checkErrors(req, res, function () {
55     checkVideoExists(req.params.id, res, function () {
56       // We need to make additional checks
57       if (res.locals.video.isOwned() === false) {
58         return res.status(403).send('Cannot update video of another pod')
59       }
60
61       if (res.locals.video.Author.userId !== res.locals.oauth.token.User.id) {
62         return res.status(403).send('Cannot update video of another user')
63       }
64
65       next()
66     })
67   })
68 }
69
70 function videosGetValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
71   req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
72
73   logger.debug('Checking videosGet parameters', { parameters: req.params })
74
75   checkErrors(req, res, function () {
76     checkVideoExists(req.params.id, res, next)
77   })
78 }
79
80 function videosRemoveValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
81   req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
82
83   logger.debug('Checking videosRemove parameters', { parameters: req.params })
84
85   checkErrors(req, res, function () {
86     checkVideoExists(req.params.id, res, function () {
87       // We need to make additional checks
88
89       // Check if the user who did the request is able to delete the video
90       checkUserCanDeleteVideo(res.locals.oauth.token.User.id, res, function () {
91         next()
92       })
93     })
94   })
95 }
96
97 function videosSearchValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
98   const searchableColumns = SEARCHABLE_COLUMNS.VIDEOS
99   req.checkParams('value', 'Should have a valid search').notEmpty()
100   req.checkQuery('field', 'Should have correct searchable column').optional().isIn(searchableColumns)
101
102   logger.debug('Checking videosSearch parameters', { parameters: req.params })
103
104   checkErrors(req, res, next)
105 }
106
107 function videoAbuseReportValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
108   req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
109   req.checkBody('reason', 'Should have a valid reason').isVideoAbuseReasonValid()
110
111   logger.debug('Checking videoAbuseReport parameters', { parameters: req.body })
112
113   checkErrors(req, res, function () {
114     checkVideoExists(req.params.id, res, next)
115   })
116 }
117
118 function videoRateValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
119   req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
120   req.checkBody('rating', 'Should have a valid rate type').isVideoRatingTypeValid()
121
122   logger.debug('Checking videoRate parameters', { parameters: req.body })
123
124   checkErrors(req, res, function () {
125     checkVideoExists(req.params.id, res, next)
126   })
127 }
128
129 function videosBlacklistValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
130   req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
131
132   logger.debug('Checking videosBlacklist parameters', { parameters: req.params })
133
134   checkErrors(req, res, function () {
135     checkVideoExists(req.params.id, res, function () {
136       checkVideoIsBlacklistable(req, res, next)
137     })
138   })
139 }
140
141 // ---------------------------------------------------------------------------
142
143 export {
144   videosAddValidator,
145   videosUpdateValidator,
146   videosGetValidator,
147   videosRemoveValidator,
148   videosSearchValidator,
149
150   videoAbuseReportValidator,
151
152   videoRateValidator,
153
154   videosBlacklistValidator
155 }
156
157 // ---------------------------------------------------------------------------
158
159 function checkVideoExists (id: string, res: express.Response, callback: () => void) {
160   db.Video.loadAndPopulateAuthorAndPodAndTags(id).then(video => {
161     if (!video) return res.status(404).send('Video not found')
162
163     res.locals.video = video
164     callback()
165   })
166   .catch(err => {
167     logger.error('Error in video request validator.', { error: err })
168     return res.sendStatus(500)
169   })
170 }
171
172 function checkUserCanDeleteVideo (userId: number, res: express.Response, callback: () => void) {
173   // Retrieve the user who did the request
174   db.User.loadById(userId)
175     .then(user => {
176       // Check if the user can delete the video
177       // The user can delete it if s/he is an admin
178       // Or if s/he is the video's author
179       if (user.isAdmin() === false) {
180         if (res.locals.video.isOwned() === false) {
181           return res.status(403).send('Cannot remove video of another pod')
182         }
183
184         if (res.locals.video.Author.userId !== res.locals.oauth.token.User.id) {
185           return res.status(403).send('Cannot remove video of another user')
186         }
187       }
188
189       // If we reach this comment, we can delete the video
190       callback()
191     })
192     .catch(err => {
193       logger.error('Error in video request validator.', { error: err })
194       return res.sendStatus(500)
195     })
196 }
197
198 function checkVideoIsBlacklistable (req: express.Request, res: express.Response, callback: () => void) {
199   if (res.locals.video.isOwned() === true) {
200     return res.status(403).send('Cannot blacklist a local video')
201   }
202
203   callback()
204 }