3 const mongoose
= require('mongoose')
5 const checkErrors
= require('./utils').checkErrors
6 const constants
= require('../../initializers/constants')
7 const customValidators
= require('../../helpers/customValidators')
8 const logger
= require('../../helpers/logger')
10 const Video
= mongoose
.model('Video')
12 const reqValidatorsVideos
= {
15 videosRemove: videosRemove
,
16 videosSearch: videosSearch
19 function videosAdd (req
, res
, next
) {
20 req
.checkFiles('videofile[0].originalname', 'Should have an input video').notEmpty()
21 req
.checkFiles('videofile[0].mimetype', 'Should have a correct mime type').matches(/video\/(webm)|(mp4)|(ogg)/i)
22 req
.checkBody('name', 'Should have a valid name').isVideoNameValid()
23 req
.checkBody('description', 'Should have a valid description').isVideoDescriptionValid()
24 req
.checkBody('tags', 'Should have correct tags').isVideoTagsValid()
26 logger
.debug('Checking videosAdd parameters', { parameters: req
.body
, files: req
.files
})
28 checkErrors(req
, res
, function () {
29 const videoFile
= req
.files
.videofile
[0]
31 Video
.getDurationFromFile(videoFile
.path
, function (err
, duration
) {
33 return res
.status(400).send('Cannot retrieve metadata of the file.')
36 if (!customValidators
.isVideoDurationValid(duration
)) {
37 return res
.status(400).send('Duration of the video file is too big (max: ' + constants
.VIDEOS_CONSTRAINTS_FIELDS
.DURATION
.max
+ 's).')
40 videoFile
.duration
= duration
46 function videosGet (req
, res
, next
) {
47 req
.checkParams('id', 'Should have a valid id').notEmpty().isMongoId()
49 logger
.debug('Checking videosGet parameters', { parameters: req
.params
})
51 checkErrors(req
, res
, function () {
52 Video
.load(req
.params
.id
, function (err
, video
) {
54 logger
.error('Error in videosGet request validator.', { error: err
})
55 return res
.sendStatus(500)
58 if (!video
) return res
.status(404).send('Video not found')
65 function videosRemove (req
, res
, next
) {
66 req
.checkParams('id', 'Should have a valid id').notEmpty().isMongoId()
68 logger
.debug('Checking videosRemove parameters', { parameters: req
.params
})
70 checkErrors(req
, res
, function () {
71 Video
.load(req
.params
.id
, function (err
, video
) {
73 logger
.error('Error in videosRemove request validator.', { error: err
})
74 return res
.sendStatus(500)
77 if (!video
) return res
.status(404).send('Video not found')
78 else if (video
.isOwned() === false) return res
.status(403).send('Cannot remove video of another pod')
85 function videosSearch (req
, res
, next
) {
86 const searchableColumns
= constants
.SEARCHABLE_COLUMNS
.VIDEOS
87 req
.checkParams('value', 'Should have a valid search').notEmpty()
88 req
.checkQuery('field', 'Should have correct searchable column').optional().isIn(searchableColumns
)
90 logger
.debug('Checking videosSearch parameters', { parameters: req
.params
})
92 checkErrors(req
, res
, next
)
95 // ---------------------------------------------------------------------------
97 module
.exports
= reqValidatorsVideos