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