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