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 = {
17 function videosAdd (req, res, next) {
18 req.checkFiles('videofile[0].originalname', 'Should have an input video').notEmpty()
19 // TODO: move to constants and function
20 req.checkFiles('videofile[0].mimetype', 'Should have a correct mime type').matches(/video\/(webm)|(mp4)|(ogg)/i)
21 req.checkBody('name', 'Should have a valid name').isVideoNameValid()
22 req.checkBody('description', 'Should have a valid description').isVideoDescriptionValid()
23 req.checkBody('tags', 'Should have correct tags').isVideoTagsValid()
25 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
27 checkErrors(req, res, function () {
28 const videoFile = req.files.videofile[0]
30 db.Video.getDurationFromFile(videoFile.path, function (err, duration) {
32 return res.status(400).send('Cannot retrieve metadata of the file.')
35 if (!customVideosValidators.isVideoDurationValid(duration)) {
36 return res.status(400).send('Duration of the video file is too big (max: ' + constants.CONSTRAINTS_FIELDS.VIDEOS.DURATION.max + 's).')
39 videoFile.duration = duration
45 function videosUpdate (req, res, next) {
46 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
47 req.checkBody('name', 'Should have a valid name').optional().isVideoNameValid()
48 req.checkBody('description', 'Should have a valid description').optional().isVideoDescriptionValid()
49 req.checkBody('tags', 'Should have correct tags').optional().isVideoTagsValid()
51 logger.debug('Checking videosUpdate parameters', { parameters: req.body })
53 checkErrors(req, res, function () {
54 checkVideoExists(req.params.id, res, next)
58 function videosGet (req, res, next) {
59 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
61 logger.debug('Checking videosGet parameters', { parameters: req.params })
63 checkErrors(req, res, function () {
64 checkVideoExists(req.params.id, res, next)
68 function videosRemove (req, res, next) {
69 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
71 logger.debug('Checking videosRemove parameters', { parameters: req.params })
73 checkErrors(req, res, function () {
74 checkVideoExists(req.params.id, res, function () {
75 // We need to make additional checks
77 if (res.locals.video.isOwned() === false) {
78 return res.status(403).send('Cannot remove video of another pod')
81 if (res.locals.video.authorId !== res.locals.oauth.token.User.id) {
82 return res.status(403).send('Cannot remove video of another user')
90 function videosSearch (req, res, next) {
91 const searchableColumns = constants.SEARCHABLE_COLUMNS.VIDEOS
92 req.checkParams('value', 'Should have a valid search').notEmpty()
93 req.checkQuery('field', 'Should have correct searchable column').optional().isIn(searchableColumns)
95 logger.debug('Checking videosSearch parameters', { parameters: req.params })
97 checkErrors(req, res, next)
100 // ---------------------------------------------------------------------------
102 module.exports = validatorsVideos
104 // ---------------------------------------------------------------------------
106 function checkVideoExists (id, res, callback) {
107 db.Video.loadAndPopulateAuthorAndPodAndTags(id, function (err, video) {
109 logger.error('Error in video request validator.', { error: err })
110 return res.sendStatus(500)
113 if (!video) return res.status(404).send('Video not found')
115 res.locals.video = video