]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/videos.js
Server: add database field validations
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos.js
CommitLineData
9f10b292 1'use strict'
34ca3b52 2
f0f5567b 3const checkErrors = require('./utils').checkErrors
67100f1f 4const constants = require('../../initializers/constants')
e4c55619 5const customVideosValidators = require('../../helpers/custom-validators').videos
feb4bdfd 6const db = require('../../initializers/database')
f0f5567b 7const logger = require('../../helpers/logger')
aaf61f38 8
fc51fde0 9const validatorsVideos = {
c4403b29
C
10 videosAdd,
11 videosGet,
12 videosRemove,
13 videosSearch
9f10b292 14}
34ca3b52 15
9f10b292 16function videosAdd (req, res, next) {
a4c15751 17 req.checkFiles('videofile[0].originalname', 'Should have an input video').notEmpty()
9bd26629 18 // TODO: move to constants and function
a4c15751 19 req.checkFiles('videofile[0].mimetype', 'Should have a correct mime type').matches(/video\/(webm)|(mp4)|(ogg)/i)
be587647
C
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()
34ca3b52 23
9f10b292 24 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
34ca3b52 25
67100f1f
C
26 checkErrors(req, res, function () {
27 const videoFile = req.files.videofile[0]
28
feb4bdfd 29 db.Video.getDurationFromFile(videoFile.path, function (err, duration) {
67100f1f
C
30 if (err) {
31 return res.status(400).send('Cannot retrieve metadata of the file.')
32 }
33
e4c55619
C
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).')
67100f1f
C
36 }
37
38 videoFile.duration = duration
39 next()
40 })
41 })
9f10b292 42}
34ca3b52 43
9f10b292 44function videosGet (req, res, next) {
feb4bdfd 45 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
34ca3b52 46
9f10b292 47 logger.debug('Checking videosGet parameters', { parameters: req.params })
34ca3b52 48
9f10b292 49 checkErrors(req, res, function () {
feb4bdfd 50 db.Video.load(req.params.id, function (err, video) {
9f10b292
C
51 if (err) {
52 logger.error('Error in videosGet request validator.', { error: err })
be587647 53 return res.sendStatus(500)
9f10b292 54 }
c173e565 55
aaf61f38 56 if (!video) return res.status(404).send('Video not found')
34ca3b52 57
2df82d42 58 next()
34ca3b52 59 })
9f10b292
C
60 })
61}
34ca3b52 62
9f10b292 63function videosRemove (req, res, next) {
feb4bdfd 64 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
34ca3b52 65
9f10b292 66 logger.debug('Checking videosRemove parameters', { parameters: req.params })
34ca3b52 67
9f10b292 68 checkErrors(req, res, function () {
feb4bdfd 69 db.Video.loadAndPopulateAuthor(req.params.id, function (err, video) {
9f10b292
C
70 if (err) {
71 logger.error('Error in videosRemove request validator.', { error: err })
be587647 72 return res.sendStatus(500)
9f10b292 73 }
c173e565 74
aaf61f38
C
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')
feb4bdfd 77 else if (video.Author.name !== res.locals.oauth.token.user.username) return res.status(403).send('Cannot remove video of another user')
34ca3b52 78
2df82d42 79 next()
34ca3b52 80 })
9f10b292
C
81 })
82}
34ca3b52 83
9f10b292 84function videosSearch (req, res, next) {
46246b5f 85 const searchableColumns = constants.SEARCHABLE_COLUMNS.VIDEOS
be587647 86 req.checkParams('value', 'Should have a valid search').notEmpty()
46246b5f 87 req.checkQuery('field', 'Should have correct searchable column').optional().isIn(searchableColumns)
c45f7f84 88
9f10b292 89 logger.debug('Checking videosSearch parameters', { parameters: req.params })
c45f7f84 90
9f10b292
C
91 checkErrors(req, res, next)
92}
c45f7f84 93
9f10b292 94// ---------------------------------------------------------------------------
c45f7f84 95
fc51fde0 96module.exports = validatorsVideos