1 import 'express-validator'
2 import * as express from 'express'
3 import * as Promise from 'bluebird'
4 import * as validator from 'validator'
6 import { database as db } from '../../initializers/database'
7 import { checkErrors } from './utils'
8 import { CONSTRAINTS_FIELDS, SEARCHABLE_COLUMNS } from '../../initializers'
9 import { logger, isVideoDurationValid } from '../../helpers'
10 import { VideoInstance } from '../../models'
12 function videosAddValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
13 // FIXME: Don't write an error message, it seems there is a bug with express-validator
14 // 'Should have a valid file'
15 req.checkBody('videofile').isVideoFile(req.files)
16 req.checkBody('name', 'Should have a valid name').isVideoNameValid()
17 req.checkBody('category', 'Should have a valid category').isVideoCategoryValid()
18 req.checkBody('licence', 'Should have a valid licence').isVideoLicenceValid()
19 req.checkBody('language', 'Should have a valid language').optional().isVideoLanguageValid()
20 req.checkBody('nsfw', 'Should have a valid NSFW attribute').isVideoNSFWValid()
21 req.checkBody('description', 'Should have a valid description').isVideoDescriptionValid()
22 req.checkBody('tags', 'Should have correct tags').optional().isVideoTagsValid()
24 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
26 checkErrors(req, res, () => {
27 const videoFile: Express.Multer.File = req.files['videofile'][0]
28 const user = res.locals.oauth.token.User
30 user.isAbleToUploadVideo(videoFile)
32 if (isAble === false) {
34 .json({ error: 'The user video quota is exceeded with this video.' })
40 return db.Video.getDurationFromFile(videoFile.path)
42 logger.error('Invalid input file in videosAddValidator.', err)
44 .json({ error: 'Invalid input file.' })
51 // Previous test failed, abort
52 if (duration === undefined) return
54 if (!isVideoDurationValid('' + duration)) {
55 return res.status(400)
57 error: 'Duration of the video file is too big (max: ' + CONSTRAINTS_FIELDS.VIDEOS.DURATION.max + 's).'
62 videoFile['duration'] = duration
66 logger.error('Error in video add validator', err)
75 function videosUpdateValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
76 req.checkParams('id', 'Should have a valid id').notEmpty().isVideoIdOrUUIDValid()
77 req.checkBody('name', 'Should have a valid name').optional().isVideoNameValid()
78 req.checkBody('category', 'Should have a valid category').optional().isVideoCategoryValid()
79 req.checkBody('licence', 'Should have a valid licence').optional().isVideoLicenceValid()
80 req.checkBody('language', 'Should have a valid language').optional().isVideoLanguageValid()
81 req.checkBody('nsfw', 'Should have a valid NSFW attribute').optional().isVideoNSFWValid()
82 req.checkBody('description', 'Should have a valid description').optional().isVideoDescriptionValid()
83 req.checkBody('tags', 'Should have correct tags').optional().isVideoTagsValid()
85 logger.debug('Checking videosUpdate parameters', { parameters: req.body })
87 checkErrors(req, res, () => {
88 checkVideoExists(req.params.id, res, () => {
89 // We need to make additional checks
90 if (res.locals.video.isOwned() === false) {
91 return res.status(403)
92 .json({ error: 'Cannot update video of another pod' })
96 if (res.locals.video.Author.userId !== res.locals.oauth.token.User.id) {
97 return res.status(403)
98 .json({ error: 'Cannot update video of another user' })
107 function videosGetValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
108 req.checkParams('id', 'Should have a valid id').notEmpty().isVideoIdOrUUIDValid()
110 logger.debug('Checking videosGet parameters', { parameters: req.params })
112 checkErrors(req, res, () => {
113 checkVideoExists(req.params.id, res, next)
117 function videosRemoveValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
118 req.checkParams('id', 'Should have a valid id').notEmpty().isVideoIdOrUUIDValid()
120 logger.debug('Checking videosRemove parameters', { parameters: req.params })
122 checkErrors(req, res, () => {
123 checkVideoExists(req.params.id, res, () => {
124 // Check if the user who did the request is able to delete the video
125 checkUserCanDeleteVideo(res.locals.oauth.token.User.id, res, () => {
132 function videosSearchValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
133 const searchableColumns = SEARCHABLE_COLUMNS.VIDEOS
134 req.checkParams('value', 'Should have a valid search').notEmpty()
135 req.checkQuery('field', 'Should have correct searchable column').optional().isIn(searchableColumns)
137 logger.debug('Checking videosSearch parameters', { parameters: req.params })
139 checkErrors(req, res, next)
142 function videoAbuseReportValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
143 req.checkParams('id', 'Should have a valid id').notEmpty().isVideoIdOrUUIDValid()
144 req.checkBody('reason', 'Should have a valid reason').isVideoAbuseReasonValid()
146 logger.debug('Checking videoAbuseReport parameters', { parameters: req.body })
148 checkErrors(req, res, () => {
149 checkVideoExists(req.params.id, res, next)
153 function videoRateValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
154 req.checkParams('id', 'Should have a valid id').notEmpty().isVideoIdOrUUIDValid()
155 req.checkBody('rating', 'Should have a valid rate type').isVideoRatingTypeValid()
157 logger.debug('Checking videoRate parameters', { parameters: req.body })
159 checkErrors(req, res, () => {
160 checkVideoExists(req.params.id, res, next)
164 function videosBlacklistValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
165 req.checkParams('id', 'Should have a valid id').notEmpty().isVideoIdOrUUIDValid()
167 logger.debug('Checking videosBlacklist parameters', { parameters: req.params })
169 checkErrors(req, res, () => {
170 checkVideoExists(req.params.id, res, () => {
171 checkVideoIsBlacklistable(req, res, next)
176 // ---------------------------------------------------------------------------
180 videosUpdateValidator,
182 videosRemoveValidator,
183 videosSearchValidator,
185 videoAbuseReportValidator,
189 videosBlacklistValidator
192 // ---------------------------------------------------------------------------
194 function checkVideoExists (id: string, res: express.Response, callback: () => void) {
195 let promise: Promise<VideoInstance>
196 if (validator.isInt(id)) {
197 promise = db.Video.loadAndPopulateAuthorAndPodAndTags(+id)
199 promise = db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(id)
202 promise.then(video => {
204 return res.status(404)
205 .json({ error: 'Video not found' })
209 res.locals.video = video
213 logger.error('Error in video request validator.', err)
214 return res.sendStatus(500)
218 function checkUserCanDeleteVideo (userId: number, res: express.Response, callback: () => void) {
219 // Retrieve the user who did the request
220 db.User.loadById(userId)
222 if (res.locals.video.isOwned() === false) {
223 return res.status(403)
224 .json({ error: 'Cannot remove video of another pod, blacklist it' })
228 // Check if the user can delete the video
229 // The user can delete it if s/he is an admin
230 // Or if s/he is the video's author
231 if (user.isAdmin() === false && res.locals.video.Author.userId !== res.locals.oauth.token.User.id) {
232 return res.status(403)
233 .json({ error: 'Cannot remove video of another user' })
237 // If we reach this comment, we can delete the video
241 logger.error('Error in video request validator.', err)
242 return res.sendStatus(500)
246 function checkVideoIsBlacklistable (req: express.Request, res: express.Response, callback: () => void) {
247 if (res.locals.video.isOwned() === true) {
248 return res.status(403)
249 .json({ error: 'Cannot blacklist a local video' })