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