3 const checkErrors = require('./utils').checkErrors
4 const constants = require('../../initializers/constants')
5 const customVideosValidators = require('../../helpers/custom-validators').videos
6 const db = require('../../initializers/database')
7 const logger = require('../../helpers/logger')
9 const validatorsVideos = {
21 function videosAdd (req, res, next) {
22 req.checkBody('videofile', 'Should have a valid file').isVideoFile(req.files)
23 req.checkBody('name', 'Should have a valid name').isVideoNameValid()
24 req.checkBody('category', 'Should have a valid category').isVideoCategoryValid()
25 req.checkBody('description', 'Should have a valid description').isVideoDescriptionValid()
26 req.checkBody('tags', 'Should have correct tags').optional().isVideoTagsValid()
28 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
30 checkErrors(req, res, function () {
31 const videoFile = req.files.videofile[0]
33 db.Video.getDurationFromFile(videoFile.path, function (err, duration) {
35 return res.status(400).send('Cannot retrieve metadata of the file.')
38 if (!customVideosValidators.isVideoDurationValid(duration)) {
39 return res.status(400).send('Duration of the video file is too big (max: ' + constants.CONSTRAINTS_FIELDS.VIDEOS.DURATION.max + 's).')
42 videoFile.duration = duration
48 function videosUpdate (req, res, next) {
49 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
50 req.checkBody('name', 'Should have a valid name').optional().isVideoNameValid()
51 req.checkBody('category', 'Should have a valid category').optional().isVideoCategoryValid()
52 req.checkBody('description', 'Should have a valid description').optional().isVideoDescriptionValid()
53 req.checkBody('tags', 'Should have correct tags').optional().isVideoTagsValid()
55 logger.debug('Checking videosUpdate parameters', { parameters: req.body })
57 checkErrors(req, res, function () {
58 checkVideoExists(req.params.id, res, function () {
59 // We need to make additional checks
60 if (res.locals.video.isOwned() === false) {
61 return res.status(403).send('Cannot update video of another pod')
64 if (res.locals.video.Author.userId !== res.locals.oauth.token.User.id) {
65 return res.status(403).send('Cannot update video of another user')
73 function videosGet (req, res, next) {
74 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
76 logger.debug('Checking videosGet parameters', { parameters: req.params })
78 checkErrors(req, res, function () {
79 checkVideoExists(req.params.id, res, next)
83 function videosRemove (req, res, next) {
84 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
86 logger.debug('Checking videosRemove parameters', { parameters: req.params })
88 checkErrors(req, res, function () {
89 checkVideoExists(req.params.id, res, function () {
90 // We need to make additional checks
92 if (res.locals.video.isOwned() === false) {
93 return res.status(403).send('Cannot remove video of another pod')
96 if (res.locals.video.Author.userId !== res.locals.oauth.token.User.id) {
97 return res.status(403).send('Cannot remove video of another user')
105 function videosSearch (req, res, next) {
106 const searchableColumns = constants.SEARCHABLE_COLUMNS.VIDEOS
107 req.checkParams('value', 'Should have a valid search').notEmpty()
108 req.checkQuery('field', 'Should have correct searchable column').optional().isIn(searchableColumns)
110 logger.debug('Checking videosSearch parameters', { parameters: req.params })
112 checkErrors(req, res, next)
115 function videoAbuseReport (req, res, next) {
116 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
117 req.checkBody('reason', 'Should have a valid reason').isVideoAbuseReasonValid()
119 logger.debug('Checking videoAbuseReport parameters', { parameters: req.body })
121 checkErrors(req, res, function () {
122 checkVideoExists(req.params.id, res, next)
126 function videoRate (req, res, next) {
127 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
128 req.checkBody('rating', 'Should have a valid rate type').isVideoRatingTypeValid()
130 logger.debug('Checking videoRate parameters', { parameters: req.body })
132 checkErrors(req, res, function () {
133 checkVideoExists(req.params.id, res, next)
137 // ---------------------------------------------------------------------------
139 module.exports = validatorsVideos
141 // ---------------------------------------------------------------------------
143 function checkVideoExists (id, res, callback) {
144 db.Video.loadAndPopulateAuthorAndPodAndTags(id, function (err, video) {
146 logger.error('Error in video request validator.', { error: err })
147 return res.sendStatus(500)
150 if (!video) return res.status(404).send('Video not found')
152 res.locals.video = video