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