]>
Commit | Line | Data |
---|---|---|
1 | 'use strict' | |
2 | ||
3 | var checkErrors = require('./utils').checkErrors | |
4 | var logger = require('../../helpers/logger') | |
5 | var Videos = require('../../models/videos') | |
6 | ||
7 | var reqValidatorsVideos = { | |
8 | videosAdd: videosAdd, | |
9 | videosGet: videosGet, | |
10 | videosRemove: videosRemove, | |
11 | videosSearch: videosSearch | |
12 | } | |
13 | ||
14 | function videosAdd (req, res, next) { | |
15 | req.checkFiles('input_video[0].originalname', 'Should have an input video').notEmpty() | |
16 | req.checkFiles('input_video[0].mimetype', 'Should have a correct mime type').matches(/video\/(webm)|(mp4)|(ogg)/i) | |
17 | req.checkBody('name', 'Should have a name').isLength(1, 50) | |
18 | req.checkBody('description', 'Should have a description').isLength(1, 250) | |
19 | ||
20 | logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files }) | |
21 | ||
22 | checkErrors(req, res, next) | |
23 | } | |
24 | ||
25 | function videosGet (req, res, next) { | |
26 | req.checkParams('id', 'Should have a valid id').notEmpty().isMongoId() | |
27 | ||
28 | logger.debug('Checking videosGet parameters', { parameters: req.params }) | |
29 | ||
30 | checkErrors(req, res, function () { | |
31 | Videos.getVideoState(req.params.id, function (err, state) { | |
32 | if (err) { | |
33 | logger.error('Error in videosGet request validator.', { error: err }) | |
34 | res.sendStatus(500) | |
35 | } | |
36 | ||
37 | if (state.exist === false) return res.status(404).send('Video not found') | |
38 | ||
39 | next() | |
40 | }) | |
41 | }) | |
42 | } | |
43 | ||
44 | function videosRemove (req, res, next) { | |
45 | req.checkParams('id', 'Should have a valid id').notEmpty().isMongoId() | |
46 | ||
47 | logger.debug('Checking videosRemove parameters', { parameters: req.params }) | |
48 | ||
49 | checkErrors(req, res, function () { | |
50 | Videos.getVideoState(req.params.id, function (err, state) { | |
51 | if (err) { | |
52 | logger.error('Error in videosRemove request validator.', { error: err }) | |
53 | res.sendStatus(500) | |
54 | } | |
55 | ||
56 | if (state.exist === false) return res.status(404).send('Video not found') | |
57 | else if (state.owned === false) return res.status(403).send('Cannot remove video of another pod') | |
58 | ||
59 | next() | |
60 | }) | |
61 | }) | |
62 | } | |
63 | ||
64 | function videosSearch (req, res, next) { | |
65 | req.checkParams('name', 'Should have a name').notEmpty() | |
66 | ||
67 | logger.debug('Checking videosSearch parameters', { parameters: req.params }) | |
68 | ||
69 | checkErrors(req, res, next) | |
70 | } | |
71 | ||
72 | // --------------------------------------------------------------------------- | |
73 | ||
74 | module.exports = reqValidatorsVideos |