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'
26 const videosAddValidator = [
27 body('videofile').custom((value, { req }) => isVideoFile(req.files)).withMessage(
28 'This file is not supported. Please, make sure it is of the following type : '
29 + CONSTRAINTS_FIELDS.VIDEOS.EXTNAME.join(', ')
31 body('name').custom(isVideoNameValid).withMessage('Should have a valid name'),
32 body('category').custom(isVideoCategoryValid).withMessage('Should have a valid category'),
33 body('licence').custom(isVideoLicenceValid).withMessage('Should have a valid licence'),
34 body('language').optional().custom(isVideoLanguageValid).withMessage('Should have a valid language'),
35 body('nsfw').custom(isVideoNSFWValid).withMessage('Should have a valid NSFW attribute'),
36 body('description').custom(isVideoDescriptionValid).withMessage('Should have a valid description'),
37 body('channelId').custom(isIdValid).withMessage('Should have correct video channel id'),
38 body('tags').optional().custom(isVideoTagsValid).withMessage('Should have correct tags'),
40 (req: express.Request, res: express.Response, next: express.NextFunction) => {
41 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
43 checkErrors(req, res, () => {
44 const videoFile: Express.Multer.File = req.files['videofile'][0]
45 const user = res.locals.oauth.token.User
47 return db.VideoChannel.loadByIdAndAuthor(req.body.channelId, user.Author.id)
48 .then(videoChannel => {
51 .json({ error: 'Unknown video video channel for this author.' })
57 res.locals.videoChannel = videoChannel
59 return user.isAbleToUploadVideo(videoFile)
62 if (isAble === false) {
64 .json({ error: 'The user video quota is exceeded with this video.' })
70 return getDurationFromVideoFile(videoFile.path)
72 logger.error('Invalid input file in videosAddValidator.', err)
74 .json({ error: 'Invalid input file.' })
81 // Previous test failed, abort
82 if (duration === undefined) return undefined
84 if (!isVideoDurationValid('' + duration)) {
85 return res.status(400)
87 error: 'Duration of the video file is too big (max: ' + CONSTRAINTS_FIELDS.VIDEOS.DURATION.max + 's).'
92 videoFile['duration'] = duration
96 logger.error('Error in video add validator', err)
105 const videosUpdateValidator = [
106 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
107 body('name').optional().custom(isVideoNameValid).withMessage('Should have a valid name'),
108 body('category').optional().custom(isVideoCategoryValid).withMessage('Should have a valid category'),
109 body('licence').optional().custom(isVideoLicenceValid).withMessage('Should have a valid licence'),
110 body('language').optional().custom(isVideoLanguageValid).withMessage('Should have a valid language'),
111 body('nsfw').optional().custom(isVideoNSFWValid).withMessage('Should have a valid NSFW attribute'),
112 body('description').optional().custom(isVideoDescriptionValid).withMessage('Should have a valid description'),
113 body('tags').optional().custom(isVideoTagsValid).withMessage('Should have correct tags'),
115 (req: express.Request, res: express.Response, next: express.NextFunction) => {
116 logger.debug('Checking videosUpdate parameters', { parameters: req.body })
118 checkErrors(req, res, () => {
119 checkVideoExists(req.params.id, res, () => {
120 // We need to make additional checks
121 if (res.locals.video.isOwned() === false) {
122 return res.status(403)
123 .json({ error: 'Cannot update video of another pod' })
127 if (res.locals.video.VideoChannel.Author.userId !== res.locals.oauth.token.User.id) {
128 return res.status(403)
129 .json({ error: 'Cannot update video of another user' })
139 const videosGetValidator = [
140 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
142 (req: express.Request, res: express.Response, next: express.NextFunction) => {
143 logger.debug('Checking videosGet parameters', { parameters: req.params })
145 checkErrors(req, res, () => {
146 checkVideoExists(req.params.id, res, next)
151 const videosRemoveValidator = [
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 videosRemove parameters', { parameters: req.params })
157 checkErrors(req, res, () => {
158 checkVideoExists(req.params.id, res, () => {
159 // Check if the user who did the request is able to delete the video
160 checkUserCanDeleteVideo(res.locals.oauth.token.User.id, res, () => {
168 const videosSearchValidator = [
169 param('value').not().isEmpty().withMessage('Should have a valid search'),
170 query('field').optional().isIn(SEARCHABLE_COLUMNS.VIDEOS).withMessage('Should have correct searchable column'),
172 (req: express.Request, res: express.Response, next: express.NextFunction) => {
173 logger.debug('Checking videosSearch parameters', { parameters: req.params })
175 checkErrors(req, res, next)
179 const videoAbuseReportValidator = [
180 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
181 body('reason').custom(isVideoAbuseReasonValid).withMessage('Should have a valid reason'),
183 (req: express.Request, res: express.Response, next: express.NextFunction) => {
184 logger.debug('Checking videoAbuseReport parameters', { parameters: req.body })
186 checkErrors(req, res, () => {
187 checkVideoExists(req.params.id, res, next)
192 const videoRateValidator = [
193 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
194 body('rating').custom(isVideoRatingTypeValid).withMessage('Should have a valid rate type'),
196 (req: express.Request, res: express.Response, next: express.NextFunction) => {
197 logger.debug('Checking videoRate parameters', { parameters: req.body })
199 checkErrors(req, res, () => {
200 checkVideoExists(req.params.id, res, next)
205 // ---------------------------------------------------------------------------
209 videosUpdateValidator,
211 videosRemoveValidator,
212 videosSearchValidator,
214 videoAbuseReportValidator,
219 // ---------------------------------------------------------------------------
221 function checkUserCanDeleteVideo (userId: number, res: express.Response, callback: () => void) {
222 // Retrieve the user who did the request
223 db.User.loadById(userId)
225 if (res.locals.video.isOwned() === false) {
226 return res.status(403)
227 .json({ error: 'Cannot remove video of another pod, blacklist it' })
231 // Check if the user can delete the video
232 // The user can delete it if s/he is an admin
233 // Or if s/he is the video's author
234 if (user.isAdmin() === false && res.locals.video.Author.userId !== res.locals.oauth.token.User.id) {
235 return res.status(403)
236 .json({ error: 'Cannot remove video of another user' })
240 // If we reach this comment, we can delete the video
244 logger.error('Error in video request validator.', err)
245 return res.sendStatus(500)