aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators/videos.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/middlewares/validators/videos.js')
-rw-r--r--server/middlewares/validators/videos.js87
1 files changed, 65 insertions, 22 deletions
diff --git a/server/middlewares/validators/videos.js b/server/middlewares/validators/videos.js
index 76e943e77..4fe6dcd8b 100644
--- a/server/middlewares/validators/videos.js
+++ b/server/middlewares/validators/videos.js
@@ -1,19 +1,19 @@
1'use strict' 1'use strict'
2 2
3const mongoose = require('mongoose')
4
5const checkErrors = require('./utils').checkErrors 3const checkErrors = require('./utils').checkErrors
6const constants = require('../../initializers/constants') 4const constants = require('../../initializers/constants')
7const customVideosValidators = require('../../helpers/custom-validators').videos 5const customVideosValidators = require('../../helpers/custom-validators').videos
6const db = require('../../initializers/database')
8const logger = require('../../helpers/logger') 7const logger = require('../../helpers/logger')
9 8
10const Video = mongoose.model('Video')
11
12const validatorsVideos = { 9const validatorsVideos = {
13 videosAdd, 10 videosAdd,
11 videosUpdate,
14 videosGet, 12 videosGet,
15 videosRemove, 13 videosRemove,
16 videosSearch 14 videosSearch,
15
16 videoAbuseReport
17} 17}
18 18
19function videosAdd (req, res, next) { 19function videosAdd (req, res, next) {
@@ -29,7 +29,7 @@ function videosAdd (req, res, next) {
29 checkErrors(req, res, function () { 29 checkErrors(req, res, function () {
30 const videoFile = req.files.videofile[0] 30 const videoFile = req.files.videofile[0]
31 31
32 Video.getDurationFromFile(videoFile.path, function (err, duration) { 32 db.Video.getDurationFromFile(videoFile.path, function (err, duration) {
33 if (err) { 33 if (err) {
34 return res.status(400).send('Cannot retrieve metadata of the file.') 34 return res.status(400).send('Cannot retrieve metadata of the file.')
35 } 35 }
@@ -44,40 +44,56 @@ function videosAdd (req, res, next) {
44 }) 44 })
45} 45}
46 46
47function videosGet (req, res, next) { 47function videosUpdate (req, res, next) {
48 req.checkParams('id', 'Should have a valid id').notEmpty().isMongoId() 48 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
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()
49 52
50 logger.debug('Checking videosGet parameters', { parameters: req.params }) 53 logger.debug('Checking videosUpdate parameters', { parameters: req.body })
51 54
52 checkErrors(req, res, function () { 55 checkErrors(req, res, function () {
53 Video.load(req.params.id, function (err, video) { 56 checkVideoExists(req.params.id, res, function () {
54 if (err) { 57 // We need to make additional checks
55 logger.error('Error in videosGet request validator.', { error: err }) 58 if (res.locals.video.isOwned() === false) {
56 return res.sendStatus(500) 59 return res.status(403).send('Cannot update video of another pod')
57 } 60 }
58 61
59 if (!video) return res.status(404).send('Video not found') 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 }
60 65
61 next() 66 next()
62 }) 67 })
63 }) 68 })
64} 69}
65 70
71function videosGet (req, res, next) {
72 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
73
74 logger.debug('Checking videosGet parameters', { parameters: req.params })
75
76 checkErrors(req, res, function () {
77 checkVideoExists(req.params.id, res, next)
78 })
79}
80
66function videosRemove (req, res, next) { 81function videosRemove (req, res, next) {
67 req.checkParams('id', 'Should have a valid id').notEmpty().isMongoId() 82 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
68 83
69 logger.debug('Checking videosRemove parameters', { parameters: req.params }) 84 logger.debug('Checking videosRemove parameters', { parameters: req.params })
70 85
71 checkErrors(req, res, function () { 86 checkErrors(req, res, function () {
72 Video.load(req.params.id, function (err, video) { 87 checkVideoExists(req.params.id, res, function () {
73 if (err) { 88 // We need to make additional checks
74 logger.error('Error in videosRemove request validator.', { error: err }) 89
75 return res.sendStatus(500) 90 if (res.locals.video.isOwned() === false) {
91 return res.status(403).send('Cannot remove video of another pod')
76 } 92 }
77 93
78 if (!video) return res.status(404).send('Video not found') 94 if (res.locals.video.Author.userId !== res.locals.oauth.token.User.id) {
79 else if (video.isOwned() === false) return res.status(403).send('Cannot remove video of another pod') 95 return res.status(403).send('Cannot remove video of another user')
80 else if (video.author !== res.locals.oauth.token.user.username) return res.status(403).send('Cannot remove video of another user') 96 }
81 97
82 next() 98 next()
83 }) 99 })
@@ -94,6 +110,33 @@ function videosSearch (req, res, next) {
94 checkErrors(req, res, next) 110 checkErrors(req, res, next)
95} 111}
96 112
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
97// --------------------------------------------------------------------------- 124// ---------------------------------------------------------------------------
98 125
99module.exports = validatorsVideos 126module.exports = validatorsVideos
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}