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