]>
Commit | Line | Data |
---|---|---|
9f10b292 | 1 | 'use strict' |
34ca3b52 | 2 | |
aaf61f38 C |
3 | const mongoose = require('mongoose') |
4 | ||
f0f5567b | 5 | const checkErrors = require('./utils').checkErrors |
67100f1f | 6 | const constants = require('../../initializers/constants') |
e4c55619 | 7 | const customVideosValidators = require('../../helpers/custom-validators').videos |
f0f5567b | 8 | const logger = require('../../helpers/logger') |
aaf61f38 C |
9 | |
10 | const Video = mongoose.model('Video') | |
34ca3b52 | 11 | |
fc51fde0 | 12 | const validatorsVideos = { |
9f10b292 C |
13 | videosAdd: videosAdd, |
14 | videosGet: videosGet, | |
15 | videosRemove: videosRemove, | |
16 | videosSearch: videosSearch | |
17 | } | |
34ca3b52 | 18 | |
9f10b292 | 19 | function videosAdd (req, res, next) { |
a4c15751 | 20 | req.checkFiles('videofile[0].originalname', 'Should have an input video').notEmpty() |
9bd26629 | 21 | // TODO: move to constants and function |
a4c15751 | 22 | req.checkFiles('videofile[0].mimetype', 'Should have a correct mime type').matches(/video\/(webm)|(mp4)|(ogg)/i) |
be587647 C |
23 | req.checkBody('name', 'Should have a valid name').isVideoNameValid() |
24 | req.checkBody('description', 'Should have a valid description').isVideoDescriptionValid() | |
25 | req.checkBody('tags', 'Should have correct tags').isVideoTagsValid() | |
34ca3b52 | 26 | |
9f10b292 | 27 | logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files }) |
34ca3b52 | 28 | |
67100f1f C |
29 | checkErrors(req, res, function () { |
30 | const videoFile = req.files.videofile[0] | |
31 | ||
aaf61f38 | 32 | Video.getDurationFromFile(videoFile.path, function (err, duration) { |
67100f1f C |
33 | if (err) { |
34 | return res.status(400).send('Cannot retrieve metadata of the file.') | |
35 | } | |
36 | ||
e4c55619 C |
37 | if (!customVideosValidators.isVideoDurationValid(duration)) { |
38 | return res.status(400).send('Duration of the video file is too big (max: ' + constants.CONSTRAINTS_FIELDS.VIDEOS.DURATION.max + 's).') | |
67100f1f C |
39 | } |
40 | ||
41 | videoFile.duration = duration | |
42 | next() | |
43 | }) | |
44 | }) | |
9f10b292 | 45 | } |
34ca3b52 | 46 | |
9f10b292 C |
47 | function videosGet (req, res, next) { |
48 | req.checkParams('id', 'Should have a valid id').notEmpty().isMongoId() | |
34ca3b52 | 49 | |
9f10b292 | 50 | logger.debug('Checking videosGet parameters', { parameters: req.params }) |
34ca3b52 | 51 | |
9f10b292 | 52 | checkErrors(req, res, function () { |
aaf61f38 | 53 | Video.load(req.params.id, function (err, video) { |
9f10b292 C |
54 | if (err) { |
55 | logger.error('Error in videosGet request validator.', { error: err }) | |
be587647 | 56 | return res.sendStatus(500) |
9f10b292 | 57 | } |
c173e565 | 58 | |
aaf61f38 | 59 | if (!video) return res.status(404).send('Video not found') |
34ca3b52 | 60 | |
2df82d42 | 61 | next() |
34ca3b52 | 62 | }) |
9f10b292 C |
63 | }) |
64 | } | |
34ca3b52 | 65 | |
9f10b292 C |
66 | function videosRemove (req, res, next) { |
67 | req.checkParams('id', 'Should have a valid id').notEmpty().isMongoId() | |
34ca3b52 | 68 | |
9f10b292 | 69 | logger.debug('Checking videosRemove parameters', { parameters: req.params }) |
34ca3b52 | 70 | |
9f10b292 | 71 | checkErrors(req, res, function () { |
aaf61f38 | 72 | Video.load(req.params.id, function (err, video) { |
9f10b292 C |
73 | if (err) { |
74 | logger.error('Error in videosRemove request validator.', { error: err }) | |
be587647 | 75 | return res.sendStatus(500) |
9f10b292 | 76 | } |
c173e565 | 77 | |
aaf61f38 C |
78 | if (!video) return res.status(404).send('Video not found') |
79 | else if (video.isOwned() === false) return res.status(403).send('Cannot remove video of another pod') | |
58b2ba55 | 80 | else if (video.author !== res.locals.oauth.token.user.username) return res.status(403).send('Cannot remove video of another user') |
34ca3b52 | 81 | |
2df82d42 | 82 | next() |
34ca3b52 | 83 | }) |
9f10b292 C |
84 | }) |
85 | } | |
34ca3b52 | 86 | |
9f10b292 | 87 | function videosSearch (req, res, next) { |
46246b5f | 88 | const searchableColumns = constants.SEARCHABLE_COLUMNS.VIDEOS |
be587647 | 89 | req.checkParams('value', 'Should have a valid search').notEmpty() |
46246b5f | 90 | req.checkQuery('field', 'Should have correct searchable column').optional().isIn(searchableColumns) |
c45f7f84 | 91 | |
9f10b292 | 92 | logger.debug('Checking videosSearch parameters', { parameters: req.params }) |
c45f7f84 | 93 | |
9f10b292 C |
94 | checkErrors(req, res, next) |
95 | } | |
c45f7f84 | 96 | |
9f10b292 | 97 | // --------------------------------------------------------------------------- |
c45f7f84 | 98 | |
fc51fde0 | 99 | module.exports = validatorsVideos |