1 import { database as db } from '../../initializers/database'
2 import { checkErrors } from './utils'
3 import { CONSTRAINTS_FIELDS, SEARCHABLE_COLUMNS } from '../../initializers'
4 import { logger, isVideoDurationValid } from '../../helpers'
6 function videosAddValidator (req, res, next) {
7 req.checkBody('videofile', 'Should have a valid file').isVideoFile(req.files)
8 req.checkBody('name', 'Should have a valid name').isVideoNameValid()
9 req.checkBody('category', 'Should have a valid category').isVideoCategoryValid()
10 req.checkBody('licence', 'Should have a valid licence').isVideoLicenceValid()
11 req.checkBody('language', 'Should have a valid language').optional().isVideoLanguageValid()
12 req.checkBody('nsfw', 'Should have a valid NSFW attribute').isVideoNSFWValid()
13 req.checkBody('description', 'Should have a valid description').isVideoDescriptionValid()
14 req.checkBody('tags', 'Should have correct tags').optional().isVideoTagsValid()
16 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
18 checkErrors(req, res, function () {
19 const videoFile = req.files.videofile[0]
21 db.Video.getDurationFromFile(videoFile.path, function (err, duration) {
23 return res.status(400).send('Cannot retrieve metadata of the file.')
26 if (!isVideoDurationValid(duration)) {
27 return res.status(400).send('Duration of the video file is too big (max: ' + CONSTRAINTS_FIELDS.VIDEOS.DURATION.max + 's).')
30 videoFile.duration = duration
36 function videosUpdateValidator (req, res, next) {
37 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
38 req.checkBody('name', 'Should have a valid name').optional().isVideoNameValid()
39 req.checkBody('category', 'Should have a valid category').optional().isVideoCategoryValid()
40 req.checkBody('licence', 'Should have a valid licence').optional().isVideoLicenceValid()
41 req.checkBody('language', 'Should have a valid language').optional().isVideoLanguageValid()
42 req.checkBody('nsfw', 'Should have a valid NSFW attribute').optional().isVideoNSFWValid()
43 req.checkBody('description', 'Should have a valid description').optional().isVideoDescriptionValid()
44 req.checkBody('tags', 'Should have correct tags').optional().isVideoTagsValid()
46 logger.debug('Checking videosUpdate parameters', { parameters: req.body })
48 checkErrors(req, res, function () {
49 checkVideoExists(req.params.id, res, function () {
50 // We need to make additional checks
51 if (res.locals.video.isOwned() === false) {
52 return res.status(403).send('Cannot update video of another pod')
55 if (res.locals.video.Author.userId !== res.locals.oauth.token.User.id) {
56 return res.status(403).send('Cannot update video of another user')
64 function videosGetValidator (req, res, next) {
65 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
67 logger.debug('Checking videosGet parameters', { parameters: req.params })
69 checkErrors(req, res, function () {
70 checkVideoExists(req.params.id, res, next)
74 function videosRemoveValidator (req, res, next) {
75 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
77 logger.debug('Checking videosRemove parameters', { parameters: req.params })
79 checkErrors(req, res, function () {
80 checkVideoExists(req.params.id, res, function () {
81 // We need to make additional checks
83 // Check if the user who did the request is able to delete the video
84 checkUserCanDeleteVideo(res.locals.oauth.token.User.id, res, function () {
91 function videosSearchValidator (req, res, next) {
92 const searchableColumns = SEARCHABLE_COLUMNS.VIDEOS
93 req.checkParams('value', 'Should have a valid search').notEmpty()
94 req.checkQuery('field', 'Should have correct searchable column').optional().isIn(searchableColumns)
96 logger.debug('Checking videosSearch parameters', { parameters: req.params })
98 checkErrors(req, res, next)
101 function videoAbuseReportValidator (req, res, next) {
102 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
103 req.checkBody('reason', 'Should have a valid reason').isVideoAbuseReasonValid()
105 logger.debug('Checking videoAbuseReport parameters', { parameters: req.body })
107 checkErrors(req, res, function () {
108 checkVideoExists(req.params.id, res, next)
112 function videoRateValidator (req, res, next) {
113 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
114 req.checkBody('rating', 'Should have a valid rate type').isVideoRatingTypeValid()
116 logger.debug('Checking videoRate parameters', { parameters: req.body })
118 checkErrors(req, res, function () {
119 checkVideoExists(req.params.id, res, next)
123 function videosBlacklistValidator (req, res, next) {
124 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
126 logger.debug('Checking videosBlacklist parameters', { parameters: req.params })
128 checkErrors(req, res, function () {
129 checkVideoExists(req.params.id, res, function () {
130 checkVideoIsBlacklistable(req, res, next)
135 // ---------------------------------------------------------------------------
139 videosUpdateValidator,
141 videosRemoveValidator,
142 videosSearchValidator,
144 videoAbuseReportValidator,
148 videosBlacklistValidator
151 // ---------------------------------------------------------------------------
153 function checkVideoExists (id, res, callback) {
154 db.Video.loadAndPopulateAuthorAndPodAndTags(id, function (err, video) {
156 logger.error('Error in video request validator.', { error: err })
157 return res.sendStatus(500)
160 if (!video) return res.status(404).send('Video not found')
162 res.locals.video = video
167 function checkUserCanDeleteVideo (userId, res, callback) {
168 // Retrieve the user who did the request
169 db.User.loadById(userId, function (err, user) {
171 logger.error('Error in video request validator.', { error: err })
172 return res.sendStatus(500)
175 // Check if the user can delete the video
176 // The user can delete it if s/he is an admin
177 // Or if s/he is the video's author
178 if (user.isAdmin() === false) {
179 if (res.locals.video.isOwned() === false) {
180 return res.status(403).send('Cannot remove video of another pod')
183 if (res.locals.video.Author.userId !== res.locals.oauth.token.User.id) {
184 return res.status(403).send('Cannot remove video of another user')
188 // If we reach this comment, we can delete the video
193 function checkVideoIsBlacklistable (req, res, callback) {
194 if (res.locals.video.isOwned() === true) {
195 return res.status(403).send('Cannot blacklist a local video')