Server: Add NSFW in user profile
[oweals/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('nsfw', 'Should have a valid NSFW attribute').isVideoNSFWValid()
27   req.checkBody('description', 'Should have a valid description').isVideoDescriptionValid()
28   req.checkBody('tags', 'Should have correct tags').optional().isVideoTagsValid()
29
30   logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
31
32   checkErrors(req, res, function () {
33     const videoFile = req.files.videofile[0]
34
35     db.Video.getDurationFromFile(videoFile.path, function (err, duration) {
36       if (err) {
37         return res.status(400).send('Cannot retrieve metadata of the file.')
38       }
39
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).')
42       }
43
44       videoFile.duration = duration
45       next()
46     })
47   })
48 }
49
50 function videosUpdate (req, res, next) {
51   req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
52   req.checkBody('name', 'Should have a valid name').optional().isVideoNameValid()
53   req.checkBody('category', 'Should have a valid category').optional().isVideoCategoryValid()
54   req.checkBody('licence', 'Should have a valid licence').optional().isVideoLicenceValid()
55   req.checkBody('nsfw', 'Should have a valid NSFW attribute').optional().isVideoNSFWValid()
56   req.checkBody('description', 'Should have a valid description').optional().isVideoDescriptionValid()
57   req.checkBody('tags', 'Should have correct tags').optional().isVideoTagsValid()
58
59   logger.debug('Checking videosUpdate parameters', { parameters: req.body })
60
61   checkErrors(req, res, function () {
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       }
67
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       }
71
72       next()
73     })
74   })
75 }
76
77 function videosGet (req, res, next) {
78   req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
79
80   logger.debug('Checking videosGet parameters', { parameters: req.params })
81
82   checkErrors(req, res, function () {
83     checkVideoExists(req.params.id, res, next)
84   })
85 }
86
87 function videosRemove (req, res, next) {
88   req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
89
90   logger.debug('Checking videosRemove parameters', { parameters: req.params })
91
92   checkErrors(req, res, function () {
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')
98       }
99
100       if (res.locals.video.Author.userId !== res.locals.oauth.token.User.id) {
101         return res.status(403).send('Cannot remove video of another user')
102       }
103
104       next()
105     })
106   })
107 }
108
109 function videosSearch (req, res, next) {
110   const searchableColumns = constants.SEARCHABLE_COLUMNS.VIDEOS
111   req.checkParams('value', 'Should have a valid search').notEmpty()
112   req.checkQuery('field', 'Should have correct searchable column').optional().isIn(searchableColumns)
113
114   logger.debug('Checking videosSearch parameters', { parameters: req.params })
115
116   checkErrors(req, res, next)
117 }
118
119 function 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
130 function 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
141 // ---------------------------------------------------------------------------
142
143 module.exports = validatorsVideos
144
145 // ---------------------------------------------------------------------------
146
147 function 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 }