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,
25 } from '../../helpers'
26 import { UserRight, VideoPrivacy } from '../../../shared'
28 const videosAddValidator = [
29 body('videofile').custom((value, { req }) => isVideoFile(req.files)).withMessage(
30 'This file is not supported. Please, make sure it is of the following type : '
31 + CONSTRAINTS_FIELDS.VIDEOS.EXTNAME.join(', ')
33 body('name').custom(isVideoNameValid).withMessage('Should have a valid name'),
34 body('category').custom(isVideoCategoryValid).withMessage('Should have a valid category'),
35 body('licence').custom(isVideoLicenceValid).withMessage('Should have a valid licence'),
36 body('language').optional().custom(isVideoLanguageValid).withMessage('Should have a valid language'),
37 body('nsfw').custom(isVideoNSFWValid).withMessage('Should have a valid NSFW attribute'),
38 body('description').custom(isVideoDescriptionValid).withMessage('Should have a valid description'),
39 body('channelId').custom(isIdValid).withMessage('Should have correct video channel id'),
40 body('privacy').custom(isVideoPrivacyValid).withMessage('Should have correct video privacy'),
41 body('tags').optional().custom(isVideoTagsValid).withMessage('Should have correct tags'),
43 (req: express.Request, res: express.Response, next: express.NextFunction) => {
44 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
46 checkErrors(req, res, () => {
47 const videoFile: Express.Multer.File = req.files['videofile'][0]
48 const user = res.locals.oauth.token.User
50 return db.VideoChannel.loadByIdAndAuthor(req.body.channelId, user.Author.id)
51 .then(videoChannel => {
54 .json({ error: 'Unknown video video channel for this author.' })
60 res.locals.videoChannel = videoChannel
62 return user.isAbleToUploadVideo(videoFile)
65 if (isAble === false) {
67 .json({ error: 'The user video quota is exceeded with this video.' })
73 return getDurationFromVideoFile(videoFile.path)
75 logger.error('Invalid input file in videosAddValidator.', err)
77 .json({ error: 'Invalid input file.' })
84 // Previous test failed, abort
85 if (duration === undefined) return undefined
87 if (!isVideoDurationValid('' + duration)) {
88 return res.status(400)
90 error: 'Duration of the video file is too big (max: ' + CONSTRAINTS_FIELDS.VIDEOS.DURATION.max + 's).'
95 videoFile['duration'] = duration
99 logger.error('Error in video add validator', err)
108 const videosUpdateValidator = [
109 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
110 body('name').optional().custom(isVideoNameValid).withMessage('Should have a valid name'),
111 body('category').optional().custom(isVideoCategoryValid).withMessage('Should have a valid category'),
112 body('licence').optional().custom(isVideoLicenceValid).withMessage('Should have a valid licence'),
113 body('language').optional().custom(isVideoLanguageValid).withMessage('Should have a valid language'),
114 body('nsfw').optional().custom(isVideoNSFWValid).withMessage('Should have a valid NSFW attribute'),
115 body('privacy').custom(isVideoPrivacyValid).withMessage('Should have correct video privacy'),
116 body('description').optional().custom(isVideoDescriptionValid).withMessage('Should have a valid description'),
117 body('tags').optional().custom(isVideoTagsValid).withMessage('Should have correct tags'),
119 (req: express.Request, res: express.Response, next: express.NextFunction) => {
120 logger.debug('Checking videosUpdate parameters', { parameters: req.body })
122 checkErrors(req, res, () => {
123 checkVideoExists(req.params.id, res, () => {
124 const video = res.locals.video
126 // We need to make additional checks
127 if (video.isOwned() === false) {
128 return res.status(403)
129 .json({ error: 'Cannot update video of another pod' })
133 if (video.VideoChannel.Author.userId !== res.locals.oauth.token.User.id) {
134 return res.status(403)
135 .json({ error: 'Cannot update video of another user' })
139 if (video.privacy !== VideoPrivacy.PRIVATE && req.body.privacy === VideoPrivacy.PRIVATE) {
140 return res.status(409)
141 .json({ error: 'Cannot set "private" a video that was not private anymore.' })
151 const videosGetValidator = [
152 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
154 (req: express.Request, res: express.Response, next: express.NextFunction) => {
155 logger.debug('Checking videosGet parameters', { parameters: req.params })
157 checkErrors(req, res, () => {
158 checkVideoExists(req.params.id, res, next)
163 const videosRemoveValidator = [
164 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
166 (req: express.Request, res: express.Response, next: express.NextFunction) => {
167 logger.debug('Checking videosRemove parameters', { parameters: req.params })
169 checkErrors(req, res, () => {
170 checkVideoExists(req.params.id, res, () => {
171 // Check if the user who did the request is able to delete the video
172 checkUserCanDeleteVideo(res.locals.oauth.token.User.id, res, () => {
180 const videosSearchValidator = [
181 param('value').not().isEmpty().withMessage('Should have a valid search'),
182 query('field').optional().isIn(SEARCHABLE_COLUMNS.VIDEOS).withMessage('Should have correct searchable column'),
184 (req: express.Request, res: express.Response, next: express.NextFunction) => {
185 logger.debug('Checking videosSearch parameters', { parameters: req.params })
187 checkErrors(req, res, next)
191 const videoAbuseReportValidator = [
192 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
193 body('reason').custom(isVideoAbuseReasonValid).withMessage('Should have a valid reason'),
195 (req: express.Request, res: express.Response, next: express.NextFunction) => {
196 logger.debug('Checking videoAbuseReport parameters', { parameters: req.body })
198 checkErrors(req, res, () => {
199 checkVideoExists(req.params.id, res, next)
204 const videoRateValidator = [
205 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
206 body('rating').custom(isVideoRatingTypeValid).withMessage('Should have a valid rate type'),
208 (req: express.Request, res: express.Response, next: express.NextFunction) => {
209 logger.debug('Checking videoRate parameters', { parameters: req.body })
211 checkErrors(req, res, () => {
212 checkVideoExists(req.params.id, res, next)
217 // ---------------------------------------------------------------------------
221 videosUpdateValidator,
223 videosRemoveValidator,
224 videosSearchValidator,
226 videoAbuseReportValidator,
231 // ---------------------------------------------------------------------------
233 function checkUserCanDeleteVideo (userId: number, res: express.Response, callback: () => void) {
234 // Retrieve the user who did the request
235 db.User.loadById(userId)
237 if (res.locals.video.isOwned() === false) {
238 return res.status(403)
239 .json({ error: 'Cannot remove video of another pod, blacklist it' })
243 // Check if the user can delete the video
244 // The user can delete it if s/he is an admin
245 // Or if s/he is the video's author
246 if (user.hasRight(UserRight.REMOVE_ANY_VIDEO) === false && res.locals.video.Author.userId !== res.locals.oauth.token.User.id) {
247 return res.status(403)
248 .json({ error: 'Cannot remove video of another user' })
252 // If we reach this comment, we can delete the video
256 logger.error('Error in video request validator.', err)
257 return res.sendStatus(500)