]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/videos.js
Server: update express-validator
[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.checkBody('videofile', 'Should have a valid file').isVideoFile(req.files)
21 req.checkBody('name', 'Should have a valid name').isVideoNameValid()
22 req.checkBody('description', 'Should have a valid description').isVideoDescriptionValid()
23 req.checkBody('tags', 'Should have correct tags').isVideoTagsValid()
24
25 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
26
27 checkErrors(req, res, function () {
28 const videoFile = req.files.videofile[0]
29
30 db.Video.getDurationFromFile(videoFile.path, function (err, duration) {
31 if (err) {
32 return res.status(400).send('Cannot retrieve metadata of the file.')
33 }
34
35 if (!customVideosValidators.isVideoDurationValid(duration)) {
36 return res.status(400).send('Duration of the video file is too big (max: ' + constants.CONSTRAINTS_FIELDS.VIDEOS.DURATION.max + 's).')
37 }
38
39 videoFile.duration = duration
40 next()
41 })
42 })
43 }
44
45 function videosUpdate (req, res, next) {
46 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
47 req.checkBody('name', 'Should have a valid name').optional().isVideoNameValid()
48 req.checkBody('description', 'Should have a valid description').optional().isVideoDescriptionValid()
49 req.checkBody('tags', 'Should have correct tags').optional().isVideoTagsValid()
50
51 logger.debug('Checking videosUpdate parameters', { parameters: req.body })
52
53 checkErrors(req, res, function () {
54 checkVideoExists(req.params.id, res, function () {
55 // We need to make additional checks
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 next()
65 })
66 })
67 }
68
69 function videosGet (req, res, next) {
70 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
71
72 logger.debug('Checking videosGet parameters', { parameters: req.params })
73
74 checkErrors(req, res, function () {
75 checkVideoExists(req.params.id, res, next)
76 })
77 }
78
79 function videosRemove (req, res, next) {
80 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
81
82 logger.debug('Checking videosRemove parameters', { parameters: req.params })
83
84 checkErrors(req, res, function () {
85 checkVideoExists(req.params.id, res, function () {
86 // We need to make additional checks
87
88 if (res.locals.video.isOwned() === false) {
89 return res.status(403).send('Cannot remove video of another pod')
90 }
91
92 if (res.locals.video.Author.userId !== res.locals.oauth.token.User.id) {
93 return res.status(403).send('Cannot remove video of another user')
94 }
95
96 next()
97 })
98 })
99 }
100
101 function videosSearch (req, res, next) {
102 const searchableColumns = constants.SEARCHABLE_COLUMNS.VIDEOS
103 req.checkParams('value', 'Should have a valid search').notEmpty()
104 req.checkQuery('field', 'Should have correct searchable column').optional().isIn(searchableColumns)
105
106 logger.debug('Checking videosSearch parameters', { parameters: req.params })
107
108 checkErrors(req, res, next)
109 }
110
111 function videoAbuseReport (req, res, next) {
112 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
113 req.checkBody('reason', 'Should have a valid reason').isVideoAbuseReasonValid()
114
115 logger.debug('Checking videoAbuseReport parameters', { parameters: req.body })
116
117 checkErrors(req, res, function () {
118 checkVideoExists(req.params.id, res, next)
119 })
120 }
121
122 // ---------------------------------------------------------------------------
123
124 module.exports = validatorsVideos
125
126 // ---------------------------------------------------------------------------
127
128 function checkVideoExists (id, res, callback) {
129 db.Video.loadAndPopulateAuthorAndPodAndTags(id, function (err, video) {
130 if (err) {
131 logger.error('Error in video request validator.', { error: err })
132 return res.sendStatus(500)
133 }
134
135 if (!video) return res.status(404).send('Video not found')
136
137 res.locals.video = video
138 callback()
139 })
140 }