]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/reqValidators/videos.js
Change api output for videos
[github/Chocobozzz/PeerTube.git] / server / middlewares / reqValidators / videos.js
CommitLineData
9f10b292 1'use strict'
34ca3b52 2
f0f5567b
C
3const checkErrors = require('./utils').checkErrors
4const logger = require('../../helpers/logger')
5const videos = require('../../lib/videos')
6const Videos = require('../../models/videos')
34ca3b52 7
f0f5567b 8const reqValidatorsVideos = {
9f10b292
C
9 videosAdd: videosAdd,
10 videosGet: videosGet,
11 videosRemove: videosRemove,
12 videosSearch: videosSearch
13}
34ca3b52 14
9f10b292
C
15function 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)
34ca3b52 20
9f10b292 21 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
34ca3b52 22
9f10b292
C
23 checkErrors(req, res, next)
24}
34ca3b52 25
9f10b292
C
26function videosGet (req, res, next) {
27 req.checkParams('id', 'Should have a valid id').notEmpty().isMongoId()
34ca3b52 28
9f10b292 29 logger.debug('Checking videosGet parameters', { parameters: req.params })
34ca3b52 30
9f10b292 31 checkErrors(req, res, function () {
5101105e 32 Videos.get(req.params.id, function (err, video) {
9f10b292
C
33 if (err) {
34 logger.error('Error in videosGet request validator.', { error: err })
35 res.sendStatus(500)
36 }
c173e565 37
2df82d42
C
38 const state = videos.getVideoState(video)
39 if (state.exist === false) return res.status(404).send('Video not found')
34ca3b52 40
2df82d42 41 next()
34ca3b52 42 })
9f10b292
C
43 })
44}
34ca3b52 45
9f10b292
C
46function videosRemove (req, res, next) {
47 req.checkParams('id', 'Should have a valid id').notEmpty().isMongoId()
34ca3b52 48
9f10b292 49 logger.debug('Checking videosRemove parameters', { parameters: req.params })
34ca3b52 50
9f10b292 51 checkErrors(req, res, function () {
5101105e 52 Videos.get(req.params.id, function (err, video) {
9f10b292
C
53 if (err) {
54 logger.error('Error in videosRemove request validator.', { error: err })
55 res.sendStatus(500)
56 }
c173e565 57
2df82d42
C
58 const state = videos.getVideoState(video)
59 if (state.exist === false) return res.status(404).send('Video not found')
60 else if (state.owned === false) return res.status(403).send('Cannot remove video of another pod')
34ca3b52 61
2df82d42 62 next()
34ca3b52 63 })
9f10b292
C
64 })
65}
34ca3b52 66
9f10b292
C
67function videosSearch (req, res, next) {
68 req.checkParams('name', 'Should have a name').notEmpty()
c45f7f84 69
9f10b292 70 logger.debug('Checking videosSearch parameters', { parameters: req.params })
c45f7f84 71
9f10b292
C
72 checkErrors(req, res, next)
73}
c45f7f84 74
9f10b292 75// ---------------------------------------------------------------------------
c45f7f84 76
9f10b292 77module.exports = reqValidatorsVideos