1 import { body, param, query } from 'express-validator/check'
2 import * as express from 'express'
4 import { database as db } from '../../initializers/database'
5 import { checkErrors } from './utils'
6 import { CONSTRAINTS_FIELDS, SEARCHABLE_COLUMNS } from '../../initializers'
14 isVideoDescriptionValid,
19 isVideoAbuseReasonValid,
20 isVideoRatingTypeValid,
21 getDurationFromVideoFile,
24 } from '../../helpers'
25 import { UserRight } from '../../../shared'
27 const videosAddValidator = [
28 body('videofile').custom((value, { req }) => isVideoFile(req.files)).withMessage(
29 'This file is not supported. Please, make sure it is of the following type : '
30 + CONSTRAINTS_FIELDS.VIDEOS.EXTNAME.join(', ')
32 body('name').custom(isVideoNameValid).withMessage('Should have a valid name'),
33 body('category').custom(isVideoCategoryValid).withMessage('Should have a valid category'),
34 body('licence').custom(isVideoLicenceValid).withMessage('Should have a valid licence'),
35 body('language').optional().custom(isVideoLanguageValid).withMessage('Should have a valid language'),
36 body('nsfw').custom(isVideoNSFWValid).withMessage('Should have a valid NSFW attribute'),
37 body('description').custom(isVideoDescriptionValid).withMessage('Should have a valid description'),
38 body('channelId').custom(isIdValid).withMessage('Should have correct video channel id'),
39 body('tags').optional().custom(isVideoTagsValid).withMessage('Should have correct tags'),
41 (req: express.Request, res: express.Response, next: express.NextFunction) => {
42 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
44 checkErrors(req, res, () => {
45 const videoFile: Express.Multer.File = req.files['videofile'][0]
46 const user = res.locals.oauth.token.User
48 return db.VideoChannel.loadByIdAndAuthor(req.body.channelId, user.Author.id)
49 .then(videoChannel => {
52 .json({ error: 'Unknown video video channel for this author.' })
58 res.locals.videoChannel = videoChannel
60 return user.isAbleToUploadVideo(videoFile)
63 if (isAble === false) {
65 .json({ error: 'The user video quota is exceeded with this video.' })
71 return getDurationFromVideoFile(videoFile.path)
73 logger.error('Invalid input file in videosAddValidator.', err)
75 .json({ error: 'Invalid input file.' })
82 // Previous test failed, abort
83 if (duration === undefined) return undefined
85 if (!isVideoDurationValid('' + duration)) {
86 return res.status(400)
88 error: 'Duration of the video file is too big (max: ' + CONSTRAINTS_FIELDS.VIDEOS.DURATION.max + 's).'
93 videoFile['duration'] = duration
97 logger.error('Error in video add validator', err)
106 const videosUpdateValidator = [
107 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
108 body('name').optional().custom(isVideoNameValid).withMessage('Should have a valid name'),
109 body('category').optional().custom(isVideoCategoryValid).withMessage('Should have a valid category'),
110 body('licence').optional().custom(isVideoLicenceValid).withMessage('Should have a valid licence'),
111 body('language').optional().custom(isVideoLanguageValid).withMessage('Should have a valid language'),
112 body('nsfw').optional().custom(isVideoNSFWValid).withMessage('Should have a valid NSFW attribute'),
113 body('description').optional().custom(isVideoDescriptionValid).withMessage('Should have a valid description'),
114 body('tags').optional().custom(isVideoTagsValid).withMessage('Should have correct tags'),
116 (req: express.Request, res: express.Response, next: express.NextFunction) => {
117 logger.debug('Checking videosUpdate parameters', { parameters: req.body })
119 checkErrors(req, res, () => {
120 checkVideoExists(req.params.id, res, () => {
121 // We need to make additional checks
122 if (res.locals.video.isOwned() === false) {
123 return res.status(403)
124 .json({ error: 'Cannot update video of another pod' })
128 if (res.locals.video.VideoChannel.Author.userId !== res.locals.oauth.token.User.id) {
129 return res.status(403)
130 .json({ error: 'Cannot update video of another user' })
140 const videosGetValidator = [
141 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
143 (req: express.Request, res: express.Response, next: express.NextFunction) => {
144 logger.debug('Checking videosGet parameters', { parameters: req.params })
146 checkErrors(req, res, () => {
147 checkVideoExists(req.params.id, res, next)
152 const videosRemoveValidator = [
153 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
155 (req: express.Request, res: express.Response, next: express.NextFunction) => {
156 logger.debug('Checking videosRemove parameters', { parameters: req.params })
158 checkErrors(req, res, () => {
159 checkVideoExists(req.params.id, res, () => {
160 // Check if the user who did the request is able to delete the video
161 checkUserCanDeleteVideo(res.locals.oauth.token.User.id, res, () => {
169 const videosSearchValidator = [
170 param('value').not().isEmpty().withMessage('Should have a valid search'),
171 query('field').optional().isIn(SEARCHABLE_COLUMNS.VIDEOS).withMessage('Should have correct searchable column'),
173 (req: express.Request, res: express.Response, next: express.NextFunction) => {
174 logger.debug('Checking videosSearch parameters', { parameters: req.params })
176 checkErrors(req, res, next)
180 const videoAbuseReportValidator = [
181 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
182 body('reason').custom(isVideoAbuseReasonValid).withMessage('Should have a valid reason'),
184 (req: express.Request, res: express.Response, next: express.NextFunction) => {
185 logger.debug('Checking videoAbuseReport parameters', { parameters: req.body })
187 checkErrors(req, res, () => {
188 checkVideoExists(req.params.id, res, next)
193 const videoRateValidator = [
194 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
195 body('rating').custom(isVideoRatingTypeValid).withMessage('Should have a valid rate type'),
197 (req: express.Request, res: express.Response, next: express.NextFunction) => {
198 logger.debug('Checking videoRate parameters', { parameters: req.body })
200 checkErrors(req, res, () => {
201 checkVideoExists(req.params.id, res, next)
206 // ---------------------------------------------------------------------------
210 videosUpdateValidator,
212 videosRemoveValidator,
213 videosSearchValidator,
215 videoAbuseReportValidator,
220 // ---------------------------------------------------------------------------
222 function checkUserCanDeleteVideo (userId: number, res: express.Response, callback: () => void) {
223 // Retrieve the user who did the request
224 db.User.loadById(userId)
226 if (res.locals.video.isOwned() === false) {
227 return res.status(403)
228 .json({ error: 'Cannot remove video of another pod, blacklist it' })
232 // Check if the user can delete the video
233 // The user can delete it if s/he is an admin
234 // Or if s/he is the video's author
235 if (user.hasRight(UserRight.REMOVE_ANY_VIDEO) === false && res.locals.video.Author.userId !== res.locals.oauth.token.User.id) {
236 return res.status(403)
237 .json({ error: 'Cannot remove video of another user' })
241 // If we reach this comment, we can delete the video
245 logger.error('Error in video request validator.', err)
246 return res.sendStatus(500)