Server: add video language attribute
[oweals/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   videoAbuseReport,
17
18   videoRate
19 }
20
21 function videosAdd (req, res, next) {
22   req.checkBody('videofile', 'Should have a valid file').isVideoFile(req.files)
23   req.checkBody('name', 'Should have a valid name').isVideoNameValid()
24   req.checkBody('category', 'Should have a valid category').isVideoCategoryValid()
25   req.checkBody('licence', 'Should have a valid licence').isVideoLicenceValid()
26   req.checkBody('language', 'Should have a valid language').optional().isVideoLanguageValid()
27   req.checkBody('nsfw', 'Should have a valid NSFW attribute').isVideoNSFWValid()
28   req.checkBody('description', 'Should have a valid description').isVideoDescriptionValid()
29   req.checkBody('tags', 'Should have correct tags').optional().isVideoTagsValid()
30
31   logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
32
33   checkErrors(req, res, function () {
34     const videoFile = req.files.videofile[0]
35
36     db.Video.getDurationFromFile(videoFile.path, function (err, duration) {
37       if (err) {
38         return res.status(400).send('Cannot retrieve metadata of the file.')
39       }
40
41       if (!customVideosValidators.isVideoDurationValid(duration)) {
42         return res.status(400).send('Duration of the video file is too big (max: ' + constants.CONSTRAINTS_FIELDS.VIDEOS.DURATION.max + 's).')
43       }
44
45       videoFile.duration = duration
46       next()
47     })
48   })
49 }
50
51 function videosUpdate (req, res, next) {
52   req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
53   req.checkBody('name', 'Should have a valid name').optional().isVideoNameValid()
54   req.checkBody('category', 'Should have a valid category').optional().isVideoCategoryValid()
55   req.checkBody('licence', 'Should have a valid licence').optional().isVideoLicenceValid()
56   req.checkBody('language', 'Should have a valid language').optional().isVideoLanguageValid()
57   req.checkBody('nsfw', 'Should have a valid NSFW attribute').optional().isVideoNSFWValid()
58   req.checkBody('description', 'Should have a valid description').optional().isVideoDescriptionValid()
59   req.checkBody('tags', 'Should have correct tags').optional().isVideoTagsValid()
60
61   logger.debug('Checking videosUpdate parameters', { parameters: req.body })
62
63   checkErrors(req, res, function () {
64     checkVideoExists(req.params.id, res, function () {
65       // We need to make additional checks
66       if (res.locals.video.isOwned() === false) {
67         return res.status(403).send('Cannot update video of another pod')
68       }
69
70       if (res.locals.video.Author.userId !== res.locals.oauth.token.User.id) {
71         return res.status(403).send('Cannot update video of another user')
72       }
73
74       next()
75     })
76   })
77 }
78
79 function videosGet (req, res, next) {
80   req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
81
82   logger.debug('Checking videosGet parameters', { parameters: req.params })
83
84   checkErrors(req, res, function () {
85     checkVideoExists(req.params.id, res, next)
86   })
87 }
88
89 function videosRemove (req, res, next) {
90   req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
91
92   logger.debug('Checking videosRemove parameters', { parameters: req.params })
93
94   checkErrors(req, res, function () {
95     checkVideoExists(req.params.id, res, function () {
96       // We need to make additional checks
97
98       if (res.locals.video.isOwned() === false) {
99         return res.status(403).send('Cannot remove video of another pod')
100       }
101
102       if (res.locals.video.Author.userId !== res.locals.oauth.token.User.id) {
103         return res.status(403).send('Cannot remove video of another user')
104       }
105
106       next()
107     })
108   })
109 }
110
111 function videosSearch (req, res, next) {
112   const searchableColumns = constants.SEARCHABLE_COLUMNS.VIDEOS
113   req.checkParams('value', 'Should have a valid search').notEmpty()
114   req.checkQuery('field', 'Should have correct searchable column').optional().isIn(searchableColumns)
115
116   logger.debug('Checking videosSearch parameters', { parameters: req.params })
117
118   checkErrors(req, res, next)
119 }
120
121 function videoAbuseReport (req, res, next) {
122   req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
123   req.checkBody('reason', 'Should have a valid reason').isVideoAbuseReasonValid()
124
125   logger.debug('Checking videoAbuseReport parameters', { parameters: req.body })
126
127   checkErrors(req, res, function () {
128     checkVideoExists(req.params.id, res, next)
129   })
130 }
131
132 function videoRate (req, res, next) {
133   req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
134   req.checkBody('rating', 'Should have a valid rate type').isVideoRatingTypeValid()
135
136   logger.debug('Checking videoRate parameters', { parameters: req.body })
137
138   checkErrors(req, res, function () {
139     checkVideoExists(req.params.id, res, next)
140   })
141 }
142
143 // ---------------------------------------------------------------------------
144
145 module.exports = validatorsVideos
146
147 // ---------------------------------------------------------------------------
148
149 function checkVideoExists (id, res, callback) {
150   db.Video.loadAndPopulateAuthorAndPodAndTags(id, function (err, video) {
151     if (err) {
152       logger.error('Error in video request validator.', { error: err })
153       return res.sendStatus(500)
154     }
155
156     if (!video) return res.status(404).send('Video not found')
157
158     res.locals.video = video
159     callback()
160   })
161 }