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