]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/videos.js
Server: error if we add a pod that already exists
[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 10 videosAdd,
7b1f49de 11 videosUpdate,
c4403b29
C
12 videosGet,
13 videosRemove,
55fa55a9
C
14 videosSearch,
15
16 videoAbuseReport
9f10b292 17}
34ca3b52 18
9f10b292 19function videosAdd (req, res, next) {
a4c15751 20 req.checkFiles('videofile[0].originalname', 'Should have an input video').notEmpty()
9bd26629 21 // TODO: move to constants and function
a4c15751 22 req.checkFiles('videofile[0].mimetype', 'Should have a correct mime type').matches(/video\/(webm)|(mp4)|(ogg)/i)
be587647
C
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()
34ca3b52 26
9f10b292 27 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
34ca3b52 28
67100f1f
C
29 checkErrors(req, res, function () {
30 const videoFile = req.files.videofile[0]
31
feb4bdfd 32 db.Video.getDurationFromFile(videoFile.path, function (err, duration) {
67100f1f
C
33 if (err) {
34 return res.status(400).send('Cannot retrieve metadata of the file.')
35 }
36
e4c55619
C
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).')
67100f1f
C
39 }
40
41 videoFile.duration = duration
42 next()
43 })
44 })
9f10b292 45}
34ca3b52 46
7b1f49de 47function videosUpdate (req, res, next) {
feb4bdfd 48 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
7b1f49de
C
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()
34ca3b52 52
7b1f49de 53 logger.debug('Checking videosUpdate parameters', { parameters: req.body })
34ca3b52 54
9f10b292 55 checkErrors(req, res, function () {
63d00f5d
C
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')
60 }
45abb8b9 61
63d00f5d
C
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')
64 }
45abb8b9 65
63d00f5d
C
66 next()
67 })
7b1f49de
C
68 })
69}
c173e565 70
7b1f49de
C
71function videosGet (req, res, next) {
72 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
34ca3b52 73
7b1f49de
C
74 logger.debug('Checking videosGet parameters', { parameters: req.params })
75
76 checkErrors(req, res, function () {
77 checkVideoExists(req.params.id, res, next)
9f10b292
C
78 })
79}
34ca3b52 80
9f10b292 81function videosRemove (req, res, next) {
feb4bdfd 82 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
34ca3b52 83
9f10b292 84 logger.debug('Checking videosRemove parameters', { parameters: req.params })
34ca3b52 85
9f10b292 86 checkErrors(req, res, function () {
818f7987
C
87 checkVideoExists(req.params.id, res, function () {
88 // We need to make additional checks
89
90 if (res.locals.video.isOwned() === false) {
91 return res.status(403).send('Cannot remove video of another pod')
9f10b292 92 }
c173e565 93
8fd66b75 94 if (res.locals.video.Author.userId !== res.locals.oauth.token.User.id) {
818f7987
C
95 return res.status(403).send('Cannot remove video of another user')
96 }
34ca3b52 97
2df82d42 98 next()
34ca3b52 99 })
9f10b292
C
100 })
101}
34ca3b52 102
9f10b292 103function videosSearch (req, res, next) {
46246b5f 104 const searchableColumns = constants.SEARCHABLE_COLUMNS.VIDEOS
be587647 105 req.checkParams('value', 'Should have a valid search').notEmpty()
46246b5f 106 req.checkQuery('field', 'Should have correct searchable column').optional().isIn(searchableColumns)
c45f7f84 107
9f10b292 108 logger.debug('Checking videosSearch parameters', { parameters: req.params })
c45f7f84 109
9f10b292
C
110 checkErrors(req, res, next)
111}
c45f7f84 112
55fa55a9
C
113function 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()
116
117 logger.debug('Checking videoAbuseReport parameters', { parameters: req.body })
118
119 checkErrors(req, res, function () {
120 checkVideoExists(req.params.id, res, next)
121 })
122}
123
9f10b292 124// ---------------------------------------------------------------------------
c45f7f84 125
fc51fde0 126module.exports = validatorsVideos
7b1f49de
C
127
128// ---------------------------------------------------------------------------
129
130function checkVideoExists (id, res, callback) {
131 db.Video.loadAndPopulateAuthorAndPodAndTags(id, function (err, video) {
132 if (err) {
133 logger.error('Error in video request validator.', { error: err })
134 return res.sendStatus(500)
135 }
136
137 if (!video) return res.status(404).send('Video not found')
138
139 res.locals.video = video
140 callback()
141 })
142}