]>
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
= {
16 function videosAdd (req
, res
, next
) {
17 req
.checkFiles('videofile[0].originalname', 'Should have an input video').notEmpty()
18 // TODO: move to constants and function
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 db
.Video
.getDurationFromFile(videoFile
.path
, function (err
, duration
) {
31 return res
.status(400).send('Cannot retrieve metadata of the file.')
34 if (!customVideosValidators
.isVideoDurationValid(duration
)) {
35 return res
.status(400).send('Duration of the video file is too big (max: ' + constants
.CONSTRAINTS_FIELDS
.VIDEOS
.DURATION
.max
+ 's).')
38 videoFile
.duration
= duration
44 function videosGet (req
, res
, next
) {
45 req
.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
47 logger
.debug('Checking videosGet parameters', { parameters: req
.params
})
49 checkErrors(req
, res
, function () {
50 db
.Video
.load(req
.params
.id
, function (err
, video
) {
52 logger
.error('Error in videosGet request validator.', { error: err
})
53 return res
.sendStatus(500)
56 if (!video
) return res
.status(404).send('Video not found')
63 function videosRemove (req
, res
, next
) {
64 req
.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
66 logger
.debug('Checking videosRemove parameters', { parameters: req
.params
})
68 checkErrors(req
, res
, function () {
69 db
.Video
.loadAndPopulateAuthor(req
.params
.id
, function (err
, video
) {
71 logger
.error('Error in videosRemove request validator.', { error: err
})
72 return res
.sendStatus(500)
75 if (!video
) return res
.status(404).send('Video not found')
76 else if (video
.isOwned() === false) return res
.status(403).send('Cannot remove video of another pod')
77 else if (video
.Author
.name
!== res
.locals
.oauth
.token
.user
.username
) return res
.status(403).send('Cannot remove video of another user')
84 function videosSearch (req
, res
, next
) {
85 const searchableColumns
= constants
.SEARCHABLE_COLUMNS
.VIDEOS
86 req
.checkParams('value', 'Should have a valid search').notEmpty()
87 req
.checkQuery('field', 'Should have correct searchable column').optional().isIn(searchableColumns
)
89 logger
.debug('Checking videosSearch parameters', { parameters: req
.params
})
91 checkErrors(req
, res
, next
)
94 // ---------------------------------------------------------------------------
96 module
.exports
= validatorsVideos