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('licence', 'Should have a valid licence').isVideoLicenceValid()
26 req.checkBody('language', 'Should have a valid language').optional().isVideoLanguageValid()
27 req.checkBody('nsfw', 'Should have a valid NSFW attribute').isVideoNSFWValid()
28 req.checkBody('description', 'Should have a valid description').isVideoDescriptionValid()
29 req.checkBody('tags', 'Should have correct tags').optional().isVideoTagsValid()
31 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
33 checkErrors(req, res, function () {
34 const videoFile = req.files.videofile[0]
36 db.Video.getDurationFromFile(videoFile.path, function (err, duration) {
38 return res.status(400).send('Cannot retrieve metadata of the file.')
41 if (!customVideosValidators.isVideoDurationValid(duration)) {
42 return res.status(400).send('Duration of the video file is too big (max: ' + constants.CONSTRAINTS_FIELDS.VIDEOS.DURATION.max + 's).')
45 videoFile.duration = duration
51 function videosUpdate (req, res, next) {
52 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
53 req.checkBody('name', 'Should have a valid name').optional().isVideoNameValid()
54 req.checkBody('category', 'Should have a valid category').optional().isVideoCategoryValid()
55 req.checkBody('licence', 'Should have a valid licence').optional().isVideoLicenceValid()
56 req.checkBody('language', 'Should have a valid language').optional().isVideoLanguageValid()
57 req.checkBody('nsfw', 'Should have a valid NSFW attribute').optional().isVideoNSFWValid()
58 req.checkBody('description', 'Should have a valid description').optional().isVideoDescriptionValid()
59 req.checkBody('tags', 'Should have correct tags').optional().isVideoTagsValid()
61 logger.debug('Checking videosUpdate parameters', { parameters: req.body })
63 checkErrors(req, res, function () {
64 checkVideoExists(req.params.id, res, function () {
65 // We need to make additional checks
66 if (res.locals.video.isOwned() === false) {
67 return res.status(403).send('Cannot update video of another pod')
70 if (res.locals.video.Author.userId !== res.locals.oauth.token.User.id) {
71 return res.status(403).send('Cannot update video of another user')
79 function videosGet (req, res, next) {
80 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
82 logger.debug('Checking videosGet parameters', { parameters: req.params })
84 checkErrors(req, res, function () {
85 checkVideoExists(req.params.id, res, next)
89 function videosRemove (req, res, next) {
90 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
92 logger.debug('Checking videosRemove parameters', { parameters: req.params })
94 checkErrors(req, res, function () {
95 checkVideoExists(req.params.id, res, function () {
96 // We need to make additional checks
98 if (res.locals.video.isOwned() === false) {
99 return res.status(403).send('Cannot remove video of another pod')
102 if (res.locals.video.Author.userId !== res.locals.oauth.token.User.id) {
103 return res.status(403).send('Cannot remove video of another user')
111 function videosSearch (req, res, next) {
112 const searchableColumns = constants.SEARCHABLE_COLUMNS.VIDEOS
113 req.checkParams('value', 'Should have a valid search').notEmpty()
114 req.checkQuery('field', 'Should have correct searchable column').optional().isIn(searchableColumns)
116 logger.debug('Checking videosSearch parameters', { parameters: req.params })
118 checkErrors(req, res, next)
121 function videoAbuseReport (req, res, next) {
122 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
123 req.checkBody('reason', 'Should have a valid reason').isVideoAbuseReasonValid()
125 logger.debug('Checking videoAbuseReport parameters', { parameters: req.body })
127 checkErrors(req, res, function () {
128 checkVideoExists(req.params.id, res, next)
132 function videoRate (req, res, next) {
133 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
134 req.checkBody('rating', 'Should have a valid rate type').isVideoRatingTypeValid()
136 logger.debug('Checking videoRate parameters', { parameters: req.body })
138 checkErrors(req, res, function () {
139 checkVideoExists(req.params.id, res, next)
143 // ---------------------------------------------------------------------------
145 module.exports = validatorsVideos
147 // ---------------------------------------------------------------------------
149 function checkVideoExists (id, res, callback) {
150 db.Video.loadAndPopulateAuthorAndPodAndTags(id, function (err, video) {
152 logger.error('Error in video request validator.', { error: err })
153 return res.sendStatus(500)
156 if (!video) return res.status(404).send('Video not found')
158 res.locals.video = video