]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/middlewares/validators/videos.js
Server: fix video remoe validation
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos.js
... / ...
CommitLineData
1'use strict'
2
3const checkErrors = require('./utils').checkErrors
4const constants = require('../../initializers/constants')
5const customVideosValidators = require('../../helpers/custom-validators').videos
6const db = require('../../initializers/database')
7const logger = require('../../helpers/logger')
8
9const validatorsVideos = {
10 videosAdd,
11 videosUpdate,
12 videosGet,
13 videosRemove,
14 videosSearch
15}
16
17function 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
45function 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
58function 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
68function 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 checkVideoExists(req.params.id, res, function () {
75 // We need to make additional checks
76
77 if (res.locals.video.isOwned() === false) {
78 return res.status(403).send('Cannot remove video of another pod')
79 }
80
81 if (res.locals.video.Author.userId !== res.locals.oauth.token.User.id) {
82 return res.status(403).send('Cannot remove video of another user')
83 }
84
85 next()
86 })
87 })
88}
89
90function videosSearch (req, res, next) {
91 const searchableColumns = constants.SEARCHABLE_COLUMNS.VIDEOS
92 req.checkParams('value', 'Should have a valid search').notEmpty()
93 req.checkQuery('field', 'Should have correct searchable column').optional().isIn(searchableColumns)
94
95 logger.debug('Checking videosSearch parameters', { parameters: req.params })
96
97 checkErrors(req, res, next)
98}
99
100// ---------------------------------------------------------------------------
101
102module.exports = validatorsVideos
103
104// ---------------------------------------------------------------------------
105
106function checkVideoExists (id, res, callback) {
107 db.Video.loadAndPopulateAuthorAndPodAndTags(id, function (err, video) {
108 if (err) {
109 logger.error('Error in video request validator.', { error: err })
110 return res.sendStatus(500)
111 }
112
113 if (!video) return res.status(404).send('Video not found')
114
115 res.locals.video = video
116 callback()
117 })
118}