1 import { body, param, query } from 'express-validator/check'
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'
16 isVideoDescriptionValid,
21 isVideoAbuseReasonValid,
22 isVideoRatingTypeValid
23 } from '../../helpers'
24 import { VideoInstance } from '../../models'
26 const videosAddValidator = [
27 body('videofile').custom((value, { req }) => isVideoFile(req.files)).withMessage('Should have a valid file'),
28 body('name').custom(isVideoNameValid).withMessage('Should have a valid name'),
29 body('category').custom(isVideoCategoryValid).withMessage('Should have a valid category'),
30 body('licence').custom(isVideoLicenceValid).withMessage('Should have a valid licence'),
31 body('language').optional().custom(isVideoLanguageValid).withMessage('Should have a valid language'),
32 body('nsfw').custom(isVideoNSFWValid).withMessage('Should have a valid NSFW attribute'),
33 body('description').custom(isVideoDescriptionValid).withMessage('Should have a valid description'),
34 body('tags').optional().custom(isVideoTagsValid).withMessage('Should have correct tags'),
36 (req: express.Request, res: express.Response, next: express.NextFunction) => {
37 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
39 checkErrors(req, res, () => {
40 const videoFile: Express.Multer.File = req.files['videofile'][0]
41 const user = res.locals.oauth.token.User
43 user.isAbleToUploadVideo(videoFile)
45 if (isAble === false) {
47 .json({ error: 'The user video quota is exceeded with this video.' })
53 return db.Video.getDurationFromFile(videoFile.path)
55 logger.error('Invalid input file in videosAddValidator.', err)
57 .json({ error: 'Invalid input file.' })
64 // Previous test failed, abort
65 if (duration === undefined) return
67 if (!isVideoDurationValid('' + duration)) {
68 return res.status(400)
70 error: 'Duration of the video file is too big (max: ' + CONSTRAINTS_FIELDS.VIDEOS.DURATION.max + 's).'
75 videoFile['duration'] = duration
79 logger.error('Error in video add validator', err)
88 const videosUpdateValidator = [
89 param('id').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
90 body('name').optional().custom(isVideoNameValid).withMessage('Should have a valid name'),
91 body('category').optional().custom(isVideoCategoryValid).withMessage('Should have a valid category'),
92 body('licence').optional().custom(isVideoLicenceValid).withMessage('Should have a valid licence'),
93 body('language').optional().custom(isVideoLanguageValid).withMessage('Should have a valid language'),
94 body('nsfw').optional().custom(isVideoNSFWValid).withMessage('Should have a valid NSFW attribute'),
95 body('description').optional().custom(isVideoDescriptionValid).withMessage('Should have a valid description'),
96 body('tags').optional().custom(isVideoTagsValid).withMessage('Should have correct tags'),
98 (req: express.Request, res: express.Response, next: express.NextFunction) => {
99 logger.debug('Checking videosUpdate parameters', { parameters: req.body })
101 checkErrors(req, res, () => {
102 checkVideoExists(req.params.id, res, () => {
103 // We need to make additional checks
104 if (res.locals.video.isOwned() === false) {
105 return res.status(403)
106 .json({ error: 'Cannot update video of another pod' })
110 if (res.locals.video.Author.userId !== res.locals.oauth.token.User.id) {
111 return res.status(403)
112 .json({ error: 'Cannot update video of another user' })
122 const videosGetValidator = [
123 param('id').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
125 (req: express.Request, res: express.Response, next: express.NextFunction) => {
126 logger.debug('Checking videosGet parameters', { parameters: req.params })
128 checkErrors(req, res, () => {
129 checkVideoExists(req.params.id, res, next)
134 const videosRemoveValidator = [
135 param('id').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
137 (req: express.Request, res: express.Response, next: express.NextFunction) => {
138 logger.debug('Checking videosRemove parameters', { parameters: req.params })
140 checkErrors(req, res, () => {
141 checkVideoExists(req.params.id, res, () => {
142 // Check if the user who did the request is able to delete the video
143 checkUserCanDeleteVideo(res.locals.oauth.token.User.id, res, () => {
151 const videosSearchValidator = [
152 param('value').not().isEmpty().withMessage('Should have a valid search'),
153 query('field').optional().isIn(SEARCHABLE_COLUMNS.VIDEOS).withMessage('Should have correct searchable column'),
155 (req: express.Request, res: express.Response, next: express.NextFunction) => {
156 logger.debug('Checking videosSearch parameters', { parameters: req.params })
158 checkErrors(req, res, next)
162 const videoAbuseReportValidator = [
163 param('id').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
164 body('reason').custom(isVideoAbuseReasonValid).withMessage('Should have a valid reason'),
166 (req: express.Request, res: express.Response, next: express.NextFunction) => {
167 logger.debug('Checking videoAbuseReport parameters', { parameters: req.body })
169 checkErrors(req, res, () => {
170 checkVideoExists(req.params.id, res, next)
175 const videoRateValidator = [
176 param('id').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
177 body('rating').custom(isVideoRatingTypeValid).withMessage('Should have a valid rate type'),
179 (req: express.Request, res: express.Response, next: express.NextFunction) => {
180 logger.debug('Checking videoRate parameters', { parameters: req.body })
182 checkErrors(req, res, () => {
183 checkVideoExists(req.params.id, res, next)
188 const videosBlacklistValidator = [
189 param('id').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
191 (req: express.Request, res: express.Response, next: express.NextFunction) => {
192 logger.debug('Checking videosBlacklist parameters', { parameters: req.params })
194 checkErrors(req, res, () => {
195 checkVideoExists(req.params.id, res, () => {
196 checkVideoIsBlacklistable(req, res, next)
202 // ---------------------------------------------------------------------------
206 videosUpdateValidator,
208 videosRemoveValidator,
209 videosSearchValidator,
211 videoAbuseReportValidator,
215 videosBlacklistValidator
218 // ---------------------------------------------------------------------------
220 function checkVideoExists (id: string, res: express.Response, callback: () => void) {
221 let promise: Promise<VideoInstance>
222 if (validator.isInt(id)) {
223 promise = db.Video.loadAndPopulateAuthorAndPodAndTags(+id)
225 promise = db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(id)
228 promise.then(video => {
230 return res.status(404)
231 .json({ error: 'Video not found' })
235 res.locals.video = video
239 logger.error('Error in video request validator.', err)
240 return res.sendStatus(500)
244 function checkUserCanDeleteVideo (userId: number, res: express.Response, callback: () => void) {
245 // Retrieve the user who did the request
246 db.User.loadById(userId)
248 if (res.locals.video.isOwned() === false) {
249 return res.status(403)
250 .json({ error: 'Cannot remove video of another pod, blacklist it' })
254 // Check if the user can delete the video
255 // The user can delete it if s/he is an admin
256 // Or if s/he is the video's author
257 if (user.isAdmin() === false && res.locals.video.Author.userId !== res.locals.oauth.token.User.id) {
258 return res.status(403)
259 .json({ error: 'Cannot remove video of another user' })
263 // If we reach this comment, we can delete the video
267 logger.error('Error in video request validator.', err)
268 return res.sendStatus(500)
272 function checkVideoIsBlacklistable (req: express.Request, res: express.Response, callback: () => void) {
273 if (res.locals.video.isOwned() === true) {
274 return res.status(403)
275 .json({ error: 'Cannot blacklist a local video' })