]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/videos.js
24e2299dcdc1d297395f588df6b8c2dcdd5ff551
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos.js
1 'use strict'
2
3 const mongoose = require('mongoose')
4
5 const checkErrors = require('./utils').checkErrors
6 const constants = require('../../initializers/constants')
7 const customValidators = require('../../helpers/customValidators')
8 const logger = require('../../helpers/logger')
9
10 const Video = mongoose.model('Video')
11
12 const validatorsVideos = {
13 videosAdd: videosAdd,
14 videosGet: videosGet,
15 videosRemove: videosRemove,
16 videosSearch: videosSearch
17 }
18
19 function videosAdd (req, res, next) {
20 req.checkFiles('videofile[0].originalname', 'Should have an input video').notEmpty()
21 req.checkFiles('videofile[0].mimetype', 'Should have a correct mime type').matches(/video\/(webm)|(mp4)|(ogg)/i)
22 req.checkBody('name', 'Should have a valid name').isVideoNameValid()
23 req.checkBody('description', 'Should have a valid description').isVideoDescriptionValid()
24 req.checkBody('tags', 'Should have correct tags').isVideoTagsValid()
25
26 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
27
28 checkErrors(req, res, function () {
29 const videoFile = req.files.videofile[0]
30
31 Video.getDurationFromFile(videoFile.path, function (err, duration) {
32 if (err) {
33 return res.status(400).send('Cannot retrieve metadata of the file.')
34 }
35
36 if (!customValidators.isVideoDurationValid(duration)) {
37 return res.status(400).send('Duration of the video file is too big (max: ' + constants.VIDEOS_CONSTRAINTS_FIELDS.DURATION.max + 's).')
38 }
39
40 videoFile.duration = duration
41 next()
42 })
43 })
44 }
45
46 function videosGet (req, res, next) {
47 req.checkParams('id', 'Should have a valid id').notEmpty().isMongoId()
48
49 logger.debug('Checking videosGet parameters', { parameters: req.params })
50
51 checkErrors(req, res, function () {
52 Video.load(req.params.id, function (err, video) {
53 if (err) {
54 logger.error('Error in videosGet request validator.', { error: err })
55 return res.sendStatus(500)
56 }
57
58 if (!video) return res.status(404).send('Video not found')
59
60 next()
61 })
62 })
63 }
64
65 function videosRemove (req, res, next) {
66 req.checkParams('id', 'Should have a valid id').notEmpty().isMongoId()
67
68 logger.debug('Checking videosRemove parameters', { parameters: req.params })
69
70 checkErrors(req, res, function () {
71 Video.load(req.params.id, function (err, video) {
72 if (err) {
73 logger.error('Error in videosRemove request validator.', { error: err })
74 return res.sendStatus(500)
75 }
76
77 if (!video) return res.status(404).send('Video not found')
78 else if (video.isOwned() === 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 = validatorsVideos