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