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('nsfw', 'Should have a valid NSFW attribute').isVideoNSFWValid()
27 req.checkBody('description', 'Should have a valid description').isVideoDescriptionValid()
28 req.checkBody('tags', 'Should have correct tags').optional().isVideoTagsValid()
30 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
32 checkErrors(req, res, function () {
33 const videoFile = req.files.videofile[0]
35 db.Video.getDurationFromFile(videoFile.path, function (err, duration) {
37 return res.status(400).send('Cannot retrieve metadata of the file.')
40 if (!customVideosValidators.isVideoDurationValid(duration)) {
41 return res.status(400).send('Duration of the video file is too big (max: ' + constants.CONSTRAINTS_FIELDS.VIDEOS.DURATION.max + 's).')
44 videoFile.duration = duration
50 function videosUpdate (req, res, next) {
51 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
52 req.checkBody('name', 'Should have a valid name').optional().isVideoNameValid()
53 req.checkBody('category', 'Should have a valid category').optional().isVideoCategoryValid()
54 req.checkBody('licence', 'Should have a valid licence').optional().isVideoLicenceValid()
55 req.checkBody('nsfw', 'Should have a valid NSFW attribute').optional().isVideoNSFWValid()
56 req.checkBody('description', 'Should have a valid description').optional().isVideoDescriptionValid()
57 req.checkBody('tags', 'Should have correct tags').optional().isVideoTagsValid()
59 logger.debug('Checking videosUpdate parameters', { parameters: req.body })
61 checkErrors(req, res, function () {
62 checkVideoExists(req.params.id, res, function () {
63 // We need to make additional checks
64 if (res.locals.video.isOwned() === false) {
65 return res.status(403).send('Cannot update video of another pod')
68 if (res.locals.video.Author.userId !== res.locals.oauth.token.User.id) {
69 return res.status(403).send('Cannot update video of another user')
77 function videosGet (req, res, next) {
78 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
80 logger.debug('Checking videosGet parameters', { parameters: req.params })
82 checkErrors(req, res, function () {
83 checkVideoExists(req.params.id, res, next)
87 function videosRemove (req, res, next) {
88 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
90 logger.debug('Checking videosRemove parameters', { parameters: req.params })
92 checkErrors(req, res, function () {
93 checkVideoExists(req.params.id, res, function () {
94 // We need to make additional checks
96 if (res.locals.video.isOwned() === false) {
97 return res.status(403).send('Cannot remove video of another pod')
100 if (res.locals.video.Author.userId !== res.locals.oauth.token.User.id) {
101 return res.status(403).send('Cannot remove video of another user')
109 function videosSearch (req, res, next) {
110 const searchableColumns = constants.SEARCHABLE_COLUMNS.VIDEOS
111 req.checkParams('value', 'Should have a valid search').notEmpty()
112 req.checkQuery('field', 'Should have correct searchable column').optional().isIn(searchableColumns)
114 logger.debug('Checking videosSearch parameters', { parameters: req.params })
116 checkErrors(req, res, next)
119 function videoAbuseReport (req, res, next) {
120 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
121 req.checkBody('reason', 'Should have a valid reason').isVideoAbuseReasonValid()
123 logger.debug('Checking videoAbuseReport parameters', { parameters: req.body })
125 checkErrors(req, res, function () {
126 checkVideoExists(req.params.id, res, next)
130 function videoRate (req, res, next) {
131 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
132 req.checkBody('rating', 'Should have a valid rate type').isVideoRatingTypeValid()
134 logger.debug('Checking videoRate parameters', { parameters: req.body })
136 checkErrors(req, res, function () {
137 checkVideoExists(req.params.id, res, next)
141 // ---------------------------------------------------------------------------
143 module.exports = validatorsVideos
145 // ---------------------------------------------------------------------------
147 function checkVideoExists (id, res, callback) {
148 db.Video.loadAndPopulateAuthorAndPodAndTags(id, function (err, video) {
150 logger.error('Error in video request validator.', { error: err })
151 return res.sendStatus(500)
154 if (!video) return res.status(404).send('Video not found')
156 res.locals.video = video