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