]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/reqValidators/videos.js
Extends the search feature by customizing the search field (name,
[github/Chocobozzz/PeerTube.git] / server / middlewares / reqValidators / videos.js
1 'use strict'
2
3 const checkErrors = require('./utils').checkErrors
4 const constants = require('../../initializers/constants')
5 const logger = require('../../helpers/logger')
6 const videos = require('../../lib/videos')
7 const Videos = require('../../models/videos')
8
9 const reqValidatorsVideos = {
10 videosAdd: videosAdd,
11 videosGet: videosGet,
12 videosRemove: videosRemove,
13 videosSearch: videosSearch
14 }
15
16 function videosAdd (req, res, next) {
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)
19 req.checkBody('name', 'Should have a name').isLength(1, 50)
20 req.checkBody('description', 'Should have a description').isLength(1, 250)
21
22 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
23
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 (' + constants.MAXIMUM_VIDEO_DURATION + ').')
34 }
35
36 videoFile.duration = duration
37 next()
38 })
39 })
40 }
41
42 function videosGet (req, res, next) {
43 req.checkParams('id', 'Should have a valid id').notEmpty().isMongoId()
44
45 logger.debug('Checking videosGet parameters', { parameters: req.params })
46
47 checkErrors(req, res, function () {
48 Videos.get(req.params.id, function (err, video) {
49 if (err) {
50 logger.error('Error in videosGet request validator.', { error: err })
51 res.sendStatus(500)
52 }
53
54 const state = videos.getVideoState(video)
55 if (state.exist === false) return res.status(404).send('Video not found')
56
57 next()
58 })
59 })
60 }
61
62 function videosRemove (req, res, next) {
63 req.checkParams('id', 'Should have a valid id').notEmpty().isMongoId()
64
65 logger.debug('Checking videosRemove parameters', { parameters: req.params })
66
67 checkErrors(req, res, function () {
68 Videos.get(req.params.id, function (err, video) {
69 if (err) {
70 logger.error('Error in videosRemove request validator.', { error: err })
71 res.sendStatus(500)
72 }
73
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')
77
78 next()
79 })
80 })
81 }
82
83 function videosSearch (req, res, next) {
84 const searchableColumns = constants.SEARCHABLE_COLUMNS.VIDEOS
85 req.checkParams('value', 'Should have a name').notEmpty()
86 req.checkQuery('field', 'Should have correct searchable column').optional().isIn(searchableColumns)
87
88 logger.debug('Checking videosSearch parameters', { parameters: req.params })
89
90 checkErrors(req, res, next)
91 }
92
93 // ---------------------------------------------------------------------------
94
95 module.exports = reqValidatorsVideos