]>
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
= {
21 function videosAdd (req
, res
, next
) {
22 req
.checkBody('videofile', 'Should have a valid file').isVideoFile(req
.files
)
23 req
.checkBody('name', 'Should have a valid name').isVideoNameValid()
24 req
.checkBody('category', 'Should have a valid category').isVideoCategoryValid()
25 req
.checkBody('licence', 'Should have a valid licence').isVideoLicenceValid()
26 req
.checkBody('description', 'Should have a valid description').isVideoDescriptionValid()
27 req
.checkBody('tags', 'Should have correct tags').optional().isVideoTagsValid()
29 logger
.debug('Checking videosAdd parameters', { parameters: req
.body
, files: req
.files
})
31 checkErrors(req
, res
, function () {
32 const videoFile
= req
.files
.videofile
[0]
34 db
.Video
.getDurationFromFile(videoFile
.path
, function (err
, duration
) {
36 return res
.status(400).send('Cannot retrieve metadata of the file.')
39 if (!customVideosValidators
.isVideoDurationValid(duration
)) {
40 return res
.status(400).send('Duration of the video file is too big (max: ' + constants
.CONSTRAINTS_FIELDS
.VIDEOS
.DURATION
.max
+ 's).')
43 videoFile
.duration
= duration
49 function videosUpdate (req
, res
, next
) {
50 req
.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
51 req
.checkBody('name', 'Should have a valid name').optional().isVideoNameValid()
52 req
.checkBody('category', 'Should have a valid category').optional().isVideoCategoryValid()
53 req
.checkBody('licence', 'Should have a valid licence').optional().isVideoLicenceValid()
54 req
.checkBody('description', 'Should have a valid description').optional().isVideoDescriptionValid()
55 req
.checkBody('tags', 'Should have correct tags').optional().isVideoTagsValid()
57 logger
.debug('Checking videosUpdate parameters', { parameters: req
.body
})
59 checkErrors(req
, res
, function () {
60 checkVideoExists(req
.params
.id
, res
, function () {
61 // We need to make additional checks
62 if (res
.locals
.video
.isOwned() === false) {
63 return res
.status(403).send('Cannot update video of another pod')
66 if (res
.locals
.video
.Author
.userId
!== res
.locals
.oauth
.token
.User
.id
) {
67 return res
.status(403).send('Cannot update video of another user')
75 function videosGet (req
, res
, next
) {
76 req
.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
78 logger
.debug('Checking videosGet parameters', { parameters: req
.params
})
80 checkErrors(req
, res
, function () {
81 checkVideoExists(req
.params
.id
, res
, next
)
85 function videosRemove (req
, res
, next
) {
86 req
.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
88 logger
.debug('Checking videosRemove parameters', { parameters: req
.params
})
90 checkErrors(req
, res
, function () {
91 checkVideoExists(req
.params
.id
, res
, function () {
92 // We need to make additional checks
94 if (res
.locals
.video
.isOwned() === false) {
95 return res
.status(403).send('Cannot remove video of another pod')
98 if (res
.locals
.video
.Author
.userId
!== res
.locals
.oauth
.token
.User
.id
) {
99 return res
.status(403).send('Cannot remove video of another user')
107 function videosSearch (req
, res
, next
) {
108 const searchableColumns
= constants
.SEARCHABLE_COLUMNS
.VIDEOS
109 req
.checkParams('value', 'Should have a valid search').notEmpty()
110 req
.checkQuery('field', 'Should have correct searchable column').optional().isIn(searchableColumns
)
112 logger
.debug('Checking videosSearch parameters', { parameters: req
.params
})
114 checkErrors(req
, res
, next
)
117 function videoAbuseReport (req
, res
, next
) {
118 req
.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
119 req
.checkBody('reason', 'Should have a valid reason').isVideoAbuseReasonValid()
121 logger
.debug('Checking videoAbuseReport parameters', { parameters: req
.body
})
123 checkErrors(req
, res
, function () {
124 checkVideoExists(req
.params
.id
, res
, next
)
128 function videoRate (req
, res
, next
) {
129 req
.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
130 req
.checkBody('rating', 'Should have a valid rate type').isVideoRatingTypeValid()
132 logger
.debug('Checking videoRate parameters', { parameters: req
.body
})
134 checkErrors(req
, res
, function () {
135 checkVideoExists(req
.params
.id
, res
, next
)
139 // ---------------------------------------------------------------------------
141 module
.exports
= validatorsVideos
143 // ---------------------------------------------------------------------------
145 function checkVideoExists (id
, res
, callback
) {
146 db
.Video
.loadAndPopulateAuthorAndPodAndTags(id
, function (err
, video
) {
148 logger
.error('Error in video request validator.', { error: err
})
149 return res
.sendStatus(500)
152 if (!video
) return res
.status(404).send('Video not found')
154 res
.locals
.video
= video