]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - middlewares/reqValidators/videos.js
Infile code reorganization
[github/Chocobozzz/PeerTube.git] / middlewares / reqValidators / videos.js
1 ;(function () {
2 'use strict'
3
4 var checkErrors = require('./utils').checkErrors
5 var logger = require('../../helpers/logger')
6 var VideosDB = require('../../initializers/database').VideosDB
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 findVideoById(req.params.id, function (video) {
33 if (!video) return res.status(404).send('Video not found')
34
35 next()
36 })
37 })
38 }
39
40 function videosRemove (req, res, next) {
41 req.checkParams('id', 'Should have a valid id').notEmpty().isMongoId()
42
43 logger.debug('Checking videosRemove parameters', { parameters: req.params })
44
45 checkErrors(req, res, function () {
46 findVideoById(req.params.id, function (video) {
47 if (!video) return res.status(404).send('Video not found')
48 else if (video.namePath === null) return res.status(403).send('Cannot remove video of another pod')
49
50 next()
51 })
52 })
53 }
54
55 function videosSearch (req, res, next) {
56 req.checkParams('name', 'Should have a name').notEmpty()
57
58 logger.debug('Checking videosSearch parameters', { parameters: req.params })
59
60 checkErrors(req, res, next)
61 }
62
63 // ---------------------------------------------------------------------------
64
65 module.exports = reqValidatorsVideos
66
67 // ---------------------------------------------------------------------------
68
69 function findVideoById (id, callback) {
70 VideosDB.findById(id, { _id: 1, namePath: 1 }).limit(1).exec(function (err, video) {
71 if (err) throw err
72
73 callback(video)
74 })
75 }
76 })()