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