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