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) {
33 res.status(403).send('The user video quota is exceeded with this video.')
38 return db.Video.getDurationFromFile(videoFile.path)
40 logger.error('Invalid input file in videosAddValidator.', err)
41 res.status(400).send('Invalid input file.')
47 // Previous test failed, abort
48 if (duration === undefined) return undefined
50 if (!isVideoDurationValid('' + duration)) {
51 return res.status(400).send('Duration of the video file is too big (max: ' + CONSTRAINTS_FIELDS.VIDEOS.DURATION.max + 's).')
54 videoFile['duration'] = duration
58 logger.error('Error in video add validator', err)
67 function videosUpdateValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
68 req.checkParams('id', 'Should have a valid id').notEmpty().isVideoIdOrUUIDValid()
69 req.checkBody('name', 'Should have a valid name').optional().isVideoNameValid()
70 req.checkBody('category', 'Should have a valid category').optional().isVideoCategoryValid()
71 req.checkBody('licence', 'Should have a valid licence').optional().isVideoLicenceValid()
72 req.checkBody('language', 'Should have a valid language').optional().isVideoLanguageValid()
73 req.checkBody('nsfw', 'Should have a valid NSFW attribute').optional().isVideoNSFWValid()
74 req.checkBody('description', 'Should have a valid description').optional().isVideoDescriptionValid()
75 req.checkBody('tags', 'Should have correct tags').optional().isVideoTagsValid()
77 logger.debug('Checking videosUpdate parameters', { parameters: req.body })
79 checkErrors(req, res, () => {
80 checkVideoExists(req.params.id, res, () => {
81 // We need to make additional checks
82 if (res.locals.video.isOwned() === false) {
83 return res.status(403).send('Cannot update video of another pod')
86 if (res.locals.video.Author.userId !== res.locals.oauth.token.User.id) {
87 return res.status(403).send('Cannot update video of another user')
95 function videosGetValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
96 req.checkParams('id', 'Should have a valid id').notEmpty().isVideoIdOrUUIDValid()
98 logger.debug('Checking videosGet parameters', { parameters: req.params })
100 checkErrors(req, res, () => {
101 checkVideoExists(req.params.id, res, next)
105 function videosRemoveValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
106 req.checkParams('id', 'Should have a valid id').notEmpty().isVideoIdOrUUIDValid()
108 logger.debug('Checking videosRemove parameters', { parameters: req.params })
110 checkErrors(req, res, () => {
111 checkVideoExists(req.params.id, res, () => {
112 // Check if the user who did the request is able to delete the video
113 checkUserCanDeleteVideo(res.locals.oauth.token.User.id, res, () => {
120 function videosSearchValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
121 const searchableColumns = SEARCHABLE_COLUMNS.VIDEOS
122 req.checkParams('value', 'Should have a valid search').notEmpty()
123 req.checkQuery('field', 'Should have correct searchable column').optional().isIn(searchableColumns)
125 logger.debug('Checking videosSearch parameters', { parameters: req.params })
127 checkErrors(req, res, next)
130 function videoAbuseReportValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
131 req.checkParams('id', 'Should have a valid id').notEmpty().isVideoIdOrUUIDValid()
132 req.checkBody('reason', 'Should have a valid reason').isVideoAbuseReasonValid()
134 logger.debug('Checking videoAbuseReport parameters', { parameters: req.body })
136 checkErrors(req, res, () => {
137 checkVideoExists(req.params.id, res, next)
141 function videoRateValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
142 req.checkParams('id', 'Should have a valid id').notEmpty().isVideoIdOrUUIDValid()
143 req.checkBody('rating', 'Should have a valid rate type').isVideoRatingTypeValid()
145 logger.debug('Checking videoRate parameters', { parameters: req.body })
147 checkErrors(req, res, () => {
148 checkVideoExists(req.params.id, res, next)
152 function videosBlacklistValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
153 req.checkParams('id', 'Should have a valid id').notEmpty().isVideoIdOrUUIDValid()
155 logger.debug('Checking videosBlacklist parameters', { parameters: req.params })
157 checkErrors(req, res, () => {
158 checkVideoExists(req.params.id, res, () => {
159 checkVideoIsBlacklistable(req, res, next)
164 // ---------------------------------------------------------------------------
168 videosUpdateValidator,
170 videosRemoveValidator,
171 videosSearchValidator,
173 videoAbuseReportValidator,
177 videosBlacklistValidator
180 // ---------------------------------------------------------------------------
182 function checkVideoExists (id: string, res: express.Response, callback: () => void) {
183 let promise: Promise<VideoInstance>
184 if (validator.isInt(id)) {
185 promise = db.Video.loadAndPopulateAuthorAndPodAndTags(+id)
187 promise = db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(id)
190 promise.then(video => {
191 if (!video) return res.status(404).send('Video not found')
193 res.locals.video = video
197 logger.error('Error in video request validator.', err)
198 return res.sendStatus(500)
202 function checkUserCanDeleteVideo (userId: number, res: express.Response, callback: () => void) {
203 // Retrieve the user who did the request
204 db.User.loadById(userId)
206 if (res.locals.video.isOwned() === false) {
207 return res.status(403).send('Cannot remove video of another pod, blacklist it')
210 // Check if the user can delete the video
211 // The user can delete it if s/he is an admin
212 // Or if s/he is the video's author
213 if (user.isAdmin() === false && res.locals.video.Author.userId !== res.locals.oauth.token.User.id) {
214 return res.status(403).send('Cannot remove video of another user')
217 // If we reach this comment, we can delete the video
221 logger.error('Error in video request validator.', err)
222 return res.sendStatus(500)
226 function checkVideoIsBlacklistable (req: express.Request, res: express.Response, callback: () => void) {
227 if (res.locals.video.isOwned() === true) {
228 return res.status(403).send('Cannot blacklist a local video')