]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/videos.js
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
= {
19 function videosAdd (req
, res
, next
) {
20 req
.checkFiles('videofile[0].originalname', 'Should have an input video').notEmpty()
21 // TODO: move to constants and function
22 req
.checkFiles('videofile[0].mimetype', 'Should have a correct mime type').matches(/video\/(webm)|(mp4)|(ogg)/i)
23 req
.checkBody('name', 'Should have a valid name').isVideoNameValid()
24 req
.checkBody('description', 'Should have a valid description').isVideoDescriptionValid()
25 req
.checkBody('tags', 'Should have correct tags').isVideoTagsValid()
27 logger
.debug('Checking videosAdd parameters', { parameters: req
.body
, files: req
.files
})
29 checkErrors(req
, res
, function () {
30 const videoFile
= req
.files
.videofile
[0]
32 db
.Video
.getDurationFromFile(videoFile
.path
, function (err
, duration
) {
34 return res
.status(400).send('Cannot retrieve metadata of the file.')
37 if (!customVideosValidators
.isVideoDurationValid(duration
)) {
38 return res
.status(400).send('Duration of the video file is too big (max: ' + constants
.CONSTRAINTS_FIELDS
.VIDEOS
.DURATION
.max
+ 's).')
41 videoFile
.duration
= duration
47 function videosUpdate (req
, res
, next
) {
48 req
.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
49 req
.checkBody('name', 'Should have a valid name').optional().isVideoNameValid()
50 req
.checkBody('description', 'Should have a valid description').optional().isVideoDescriptionValid()
51 req
.checkBody('tags', 'Should have correct tags').optional().isVideoTagsValid()
53 logger
.debug('Checking videosUpdate parameters', { parameters: req
.body
})
55 checkErrors(req
, res
, function () {
56 checkVideoExists(req
.params
.id
, res
, function () {
57 // We need to make additional checks
58 if (res
.locals
.video
.isOwned() === false) {
59 return res
.status(403).send('Cannot update video of another pod')
62 if (res
.locals
.video
.Author
.userId
!== res
.locals
.oauth
.token
.User
.id
) {
63 return res
.status(403).send('Cannot update video of another user')
71 function videosGet (req
, res
, next
) {
72 req
.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
74 logger
.debug('Checking videosGet parameters', { parameters: req
.params
})
76 checkErrors(req
, res
, function () {
77 checkVideoExists(req
.params
.id
, res
, next
)
81 function videosRemove (req
, res
, next
) {
82 req
.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
84 logger
.debug('Checking videosRemove parameters', { parameters: req
.params
})
86 checkErrors(req
, res
, function () {
87 checkVideoExists(req
.params
.id
, res
, function () {
88 // We need to make additional checks
90 if (res
.locals
.video
.isOwned() === false) {
91 return res
.status(403).send('Cannot remove video of another pod')
94 if (res
.locals
.video
.Author
.userId
!== res
.locals
.oauth
.token
.User
.id
) {
95 return res
.status(403).send('Cannot remove video of another user')
103 function videosSearch (req
, res
, next
) {
104 const searchableColumns
= constants
.SEARCHABLE_COLUMNS
.VIDEOS
105 req
.checkParams('value', 'Should have a valid search').notEmpty()
106 req
.checkQuery('field', 'Should have correct searchable column').optional().isIn(searchableColumns
)
108 logger
.debug('Checking videosSearch parameters', { parameters: req
.params
})
110 checkErrors(req
, res
, next
)
113 function videoAbuseReport (req
, res
, next
) {
114 req
.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
115 req
.checkBody('reason', 'Should have a valid reason').isVideoAbuseReasonValid()
117 logger
.debug('Checking videoAbuseReport parameters', { parameters: req
.body
})
119 checkErrors(req
, res
, function () {
120 checkVideoExists(req
.params
.id
, res
, next
)
124 // ---------------------------------------------------------------------------
126 module
.exports
= validatorsVideos
128 // ---------------------------------------------------------------------------
130 function checkVideoExists (id
, res
, callback
) {
131 db
.Video
.loadAndPopulateAuthorAndPodAndTags(id
, function (err
, video
) {
133 logger
.error('Error in video request validator.', { error: err
})
134 return res
.sendStatus(500)
137 if (!video
) return res
.status(404).send('Video not found')
139 res
.locals
.video
= video