]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/reqValidators/videos.js
3 const checkErrors
= require('./utils').checkErrors
4 const constants
= require('../../initializers/constants')
5 const customValidators
= require('../../helpers/customValidators')
6 const logger
= require('../../helpers/logger')
7 const videos
= require('../../lib/videos')
8 const Videos
= require('../../models/videos')
10 const reqValidatorsVideos
= {
13 videosRemove: videosRemove
,
14 videosSearch: videosSearch
17 function videosAdd (req
, res
, next
) {
18 req
.checkFiles('videofile[0].originalname', 'Should have an input video').notEmpty()
19 req
.checkFiles('videofile[0].mimetype', 'Should have a correct mime type').matches(/video\/(webm)|(mp4)|(ogg)/i)
20 req
.checkBody('name', 'Should have a valid name').isVideoNameValid()
21 req
.checkBody('description', 'Should have a valid description').isVideoDescriptionValid()
22 req
.checkBody('tags', 'Should have correct tags').isVideoTagsValid()
24 logger
.debug('Checking videosAdd parameters', { parameters: req
.body
, files: req
.files
})
26 checkErrors(req
, res
, function () {
27 const videoFile
= req
.files
.videofile
[0]
29 videos
.getVideoDuration(videoFile
.path
, function (err
, duration
) {
31 return res
.status(400).send('Cannot retrieve metadata of the file.')
34 if (!customValidators
.isVideoDurationValid(duration
)) {
35 return res
.status(400).send('Duration of the video file is too big (max: ' + constants
.MAXIMUM_VIDEO_DURATION
+ 's).')
38 videoFile
.duration
= duration
44 function videosGet (req
, res
, next
) {
45 req
.checkParams('id', 'Should have a valid id').notEmpty().isMongoId()
47 logger
.debug('Checking videosGet parameters', { parameters: req
.params
})
49 checkErrors(req
, res
, function () {
50 Videos
.get(req
.params
.id
, function (err
, video
) {
52 logger
.error('Error in videosGet request validator.', { error: err
})
53 return res
.sendStatus(500)
56 const state
= videos
.getVideoState(video
)
57 if (state
.exist
=== false) return res
.status(404).send('Video not found')
64 function videosRemove (req
, res
, next
) {
65 req
.checkParams('id', 'Should have a valid id').notEmpty().isMongoId()
67 logger
.debug('Checking videosRemove parameters', { parameters: req
.params
})
69 checkErrors(req
, res
, function () {
70 Videos
.get(req
.params
.id
, function (err
, video
) {
72 logger
.error('Error in videosRemove request validator.', { error: err
})
73 return res
.sendStatus(500)
76 const state
= videos
.getVideoState(video
)
77 if (state
.exist
=== false) return res
.status(404).send('Video not found')
78 else if (state
.owned
=== 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