]>
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
.checkBody('videofile', 'Should have a valid file').isVideoFile(req
.files
)
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
, function () {
55 // We need to make additional checks
56 if (res
.locals
.video
.isOwned() === false) {
57 return res
.status(403).send('Cannot update video of another pod')
60 if (res
.locals
.video
.Author
.userId
!== res
.locals
.oauth
.token
.User
.id
) {
61 return res
.status(403).send('Cannot update video of another user')
69 function videosGet (req
, res
, next
) {
70 req
.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
72 logger
.debug('Checking videosGet parameters', { parameters: req
.params
})
74 checkErrors(req
, res
, function () {
75 checkVideoExists(req
.params
.id
, res
, next
)
79 function videosRemove (req
, res
, next
) {
80 req
.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
82 logger
.debug('Checking videosRemove parameters', { parameters: req
.params
})
84 checkErrors(req
, res
, function () {
85 checkVideoExists(req
.params
.id
, res
, function () {
86 // We need to make additional checks
88 if (res
.locals
.video
.isOwned() === false) {
89 return res
.status(403).send('Cannot remove video of another pod')
92 if (res
.locals
.video
.Author
.userId
!== res
.locals
.oauth
.token
.User
.id
) {
93 return res
.status(403).send('Cannot remove video of another user')
101 function videosSearch (req
, res
, next
) {
102 const searchableColumns
= constants
.SEARCHABLE_COLUMNS
.VIDEOS
103 req
.checkParams('value', 'Should have a valid search').notEmpty()
104 req
.checkQuery('field', 'Should have correct searchable column').optional().isIn(searchableColumns
)
106 logger
.debug('Checking videosSearch parameters', { parameters: req
.params
})
108 checkErrors(req
, res
, next
)
111 function videoAbuseReport (req
, res
, next
) {
112 req
.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
113 req
.checkBody('reason', 'Should have a valid reason').isVideoAbuseReasonValid()
115 logger
.debug('Checking videoAbuseReport parameters', { parameters: req
.body
})
117 checkErrors(req
, res
, function () {
118 checkVideoExists(req
.params
.id
, res
, next
)
122 // ---------------------------------------------------------------------------
124 module
.exports
= validatorsVideos
126 // ---------------------------------------------------------------------------
128 function checkVideoExists (id
, res
, callback
) {
129 db
.Video
.loadAndPopulateAuthorAndPodAndTags(id
, function (err
, video
) {
131 logger
.error('Error in video request validator.', { error: err
})
132 return res
.sendStatus(500)
135 if (!video
) return res
.status(404).send('Video not found')
137 res
.locals
.video
= video