]>
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, | |
14 | videosSearch | |
9f10b292 | 15 | } |
34ca3b52 | 16 | |
9f10b292 | 17 | function videosAdd (req, res, next) { |
a4c15751 | 18 | req.checkFiles('videofile[0].originalname', 'Should have an input video').notEmpty() |
9bd26629 | 19 | // TODO: move to constants and function |
a4c15751 | 20 | req.checkFiles('videofile[0].mimetype', 'Should have a correct mime type').matches(/video\/(webm)|(mp4)|(ogg)/i) |
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 () { |
7b1f49de C |
54 | checkVideoExists(req.params.id, res, next) |
55 | }) | |
56 | } | |
c173e565 | 57 | |
7b1f49de C |
58 | function videosGet (req, res, next) { |
59 | req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) | |
34ca3b52 | 60 | |
7b1f49de C |
61 | logger.debug('Checking videosGet parameters', { parameters: req.params }) |
62 | ||
63 | checkErrors(req, res, function () { | |
64 | checkVideoExists(req.params.id, res, next) | |
9f10b292 C |
65 | }) |
66 | } | |
34ca3b52 | 67 | |
9f10b292 | 68 | function videosRemove (req, res, next) { |
feb4bdfd | 69 | req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) |
34ca3b52 | 70 | |
9f10b292 | 71 | logger.debug('Checking videosRemove parameters', { parameters: req.params }) |
34ca3b52 | 72 | |
9f10b292 | 73 | checkErrors(req, res, function () { |
818f7987 C |
74 | checkVideoExists(req.params.id, res, function () { |
75 | // We need to make additional checks | |
76 | ||
77 | if (res.locals.video.isOwned() === false) { | |
78 | return res.status(403).send('Cannot remove video of another pod') | |
9f10b292 | 79 | } |
c173e565 | 80 | |
818f7987 C |
81 | if (res.locals.video.authorId !== res.locals.oauth.token.User.id) { |
82 | return res.status(403).send('Cannot remove video of another user') | |
83 | } | |
34ca3b52 | 84 | |
2df82d42 | 85 | next() |
34ca3b52 | 86 | }) |
9f10b292 C |
87 | }) |
88 | } | |
34ca3b52 | 89 | |
9f10b292 | 90 | function videosSearch (req, res, next) { |
46246b5f | 91 | const searchableColumns = constants.SEARCHABLE_COLUMNS.VIDEOS |
be587647 | 92 | req.checkParams('value', 'Should have a valid search').notEmpty() |
46246b5f | 93 | req.checkQuery('field', 'Should have correct searchable column').optional().isIn(searchableColumns) |
c45f7f84 | 94 | |
9f10b292 | 95 | logger.debug('Checking videosSearch parameters', { parameters: req.params }) |
c45f7f84 | 96 | |
9f10b292 C |
97 | checkErrors(req, res, next) |
98 | } | |
c45f7f84 | 99 | |
9f10b292 | 100 | // --------------------------------------------------------------------------- |
c45f7f84 | 101 | |
fc51fde0 | 102 | module.exports = validatorsVideos |
7b1f49de C |
103 | |
104 | // --------------------------------------------------------------------------- | |
105 | ||
106 | function checkVideoExists (id, res, callback) { | |
107 | db.Video.loadAndPopulateAuthorAndPodAndTags(id, function (err, video) { | |
108 | if (err) { | |
109 | logger.error('Error in video request validator.', { error: err }) | |
110 | return res.sendStatus(500) | |
111 | } | |
112 | ||
113 | if (!video) return res.status(404).send('Video not found') | |
114 | ||
115 | res.locals.video = video | |
116 | callback() | |
117 | }) | |
118 | } |