]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/videos.js
09a188c765ae3dac558601e2e1b0b5582ea602f0
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos.js
1 'use strict'
2
3 const checkErrors = require('./utils').checkErrors
4 const constants = require('../../initializers/constants')
5 const customVideosValidators = require('../../helpers/custom-validators').videos
6 const db = require('../../initializers/database')
7 const logger = require('../../helpers/logger')
8
9 const validatorsVideos = {
10 videosAdd,
11 videosUpdate,
12 videosGet,
13 videosRemove,
14 videosSearch
15 }
16
17 function videosAdd (req, res, next) {
18 req.checkFiles('videofile[0].originalname', 'Should have an input video').notEmpty()
19 // TODO: move to constants and function
20 req.checkFiles('videofile[0].mimetype', 'Should have a correct mime type').matches(/video\/(webm)|(mp4)|(ogg)/i)
21 req.checkBody('name', 'Should have a valid name').isVideoNameValid()
22 req.checkBody('description', 'Should have a valid description').isVideoDescriptionValid()
23 req.checkBody('tags', 'Should have correct tags').isVideoTagsValid()
24
25 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
26
27 checkErrors(req, res, function () {
28 const videoFile = req.files.videofile[0]
29
30 db.Video.getDurationFromFile(videoFile.path, function (err, duration) {
31 if (err) {
32 return res.status(400).send('Cannot retrieve metadata of the file.')
33 }
34
35 if (!customVideosValidators.isVideoDurationValid(duration)) {
36 return res.status(400).send('Duration of the video file is too big (max: ' + constants.CONSTRAINTS_FIELDS.VIDEOS.DURATION.max + 's).')
37 }
38
39 videoFile.duration = duration
40 next()
41 })
42 })
43 }
44
45 function videosUpdate (req, res, next) {
46 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
47 req.checkBody('name', 'Should have a valid name').optional().isVideoNameValid()
48 req.checkBody('description', 'Should have a valid description').optional().isVideoDescriptionValid()
49 req.checkBody('tags', 'Should have correct tags').optional().isVideoTagsValid()
50
51 logger.debug('Checking videosUpdate parameters', { parameters: req.body })
52
53 checkErrors(req, res, function () {
54 checkVideoExists(req.params.id, res, next)
55 })
56 }
57
58 function videosGet (req, res, next) {
59 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
60
61 logger.debug('Checking videosGet parameters', { parameters: req.params })
62
63 checkErrors(req, res, function () {
64 checkVideoExists(req.params.id, res, next)
65 })
66 }
67
68 function videosRemove (req, res, next) {
69 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
70
71 logger.debug('Checking videosRemove parameters', { parameters: req.params })
72
73 checkErrors(req, res, function () {
74 db.Video.loadAndPopulateAuthor(req.params.id, function (err, video) {
75 if (err) {
76 logger.error('Error in videosRemove request validator.', { error: err })
77 return res.sendStatus(500)
78 }
79
80 if (!video) return res.status(404).send('Video not found')
81 else if (video.isOwned() === false) return res.status(403).send('Cannot remove video of another pod')
82 else if (video.Author.name !== res.locals.oauth.token.user.username) return res.status(403).send('Cannot remove video of another user')
83
84 next()
85 })
86 })
87 }
88
89 function videosSearch (req, res, next) {
90 const searchableColumns = constants.SEARCHABLE_COLUMNS.VIDEOS
91 req.checkParams('value', 'Should have a valid search').notEmpty()
92 req.checkQuery('field', 'Should have correct searchable column').optional().isIn(searchableColumns)
93
94 logger.debug('Checking videosSearch parameters', { parameters: req.params })
95
96 checkErrors(req, res, next)
97 }
98
99 // ---------------------------------------------------------------------------
100
101 module.exports = validatorsVideos
102
103 // ---------------------------------------------------------------------------
104
105 function checkVideoExists (id, res, callback) {
106 db.Video.loadAndPopulateAuthorAndPodAndTags(id, function (err, video) {
107 if (err) {
108 logger.error('Error in video request validator.', { error: err })
109 return res.sendStatus(500)
110 }
111
112 if (!video) return res.status(404).send('Video not found')
113
114 res.locals.video = video
115 callback()
116 })
117 }