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