1 import 'express-validator'
2 import * as multer from 'multer'
3 import * as express from 'express'
5 import { database as db } from '../../initializers/database'
6 import { checkErrors } from './utils'
7 import { CONSTRAINTS_FIELDS, SEARCHABLE_COLUMNS } from '../../initializers'
8 import { logger, isVideoDurationValid } from '../../helpers'
10 function videosAddValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
11 req.checkBody('videofile', 'Should have a valid file').isVideoFile(req.files)
12 req.checkBody('name', 'Should have a valid name').isVideoNameValid()
13 req.checkBody('category', 'Should have a valid category').isVideoCategoryValid()
14 req.checkBody('licence', 'Should have a valid licence').isVideoLicenceValid()
15 req.checkBody('language', 'Should have a valid language').optional().isVideoLanguageValid()
16 req.checkBody('nsfw', 'Should have a valid NSFW attribute').isVideoNSFWValid()
17 req.checkBody('description', 'Should have a valid description').isVideoDescriptionValid()
18 req.checkBody('tags', 'Should have correct tags').optional().isVideoTagsValid()
20 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
22 checkErrors(req, res, function () {
23 const videoFile = req.files.videofile[0]
25 db.Video.getDurationFromFile(videoFile.path, function (err, duration) {
27 return res.status(400).send('Cannot retrieve metadata of the file.')
30 if (!isVideoDurationValid(duration)) {
31 return res.status(400).send('Duration of the video file is too big (max: ' + CONSTRAINTS_FIELDS.VIDEOS.DURATION.max + 's).')
34 videoFile['duration'] = duration
40 function videosUpdateValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
41 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
42 req.checkBody('name', 'Should have a valid name').optional().isVideoNameValid()
43 req.checkBody('category', 'Should have a valid category').optional().isVideoCategoryValid()
44 req.checkBody('licence', 'Should have a valid licence').optional().isVideoLicenceValid()
45 req.checkBody('language', 'Should have a valid language').optional().isVideoLanguageValid()
46 req.checkBody('nsfw', 'Should have a valid NSFW attribute').optional().isVideoNSFWValid()
47 req.checkBody('description', 'Should have a valid description').optional().isVideoDescriptionValid()
48 req.checkBody('tags', 'Should have correct tags').optional().isVideoTagsValid()
50 logger.debug('Checking videosUpdate parameters', { parameters: req.body })
52 checkErrors(req, res, function () {
53 checkVideoExists(req.params.id, res, function () {
54 // We need to make additional checks
55 if (res.locals.video.isOwned() === false) {
56 return res.status(403).send('Cannot update video of another pod')
59 if (res.locals.video.Author.userId !== res.locals.oauth.token.User.id) {
60 return res.status(403).send('Cannot update video of another user')
68 function videosGetValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
69 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
71 logger.debug('Checking videosGet parameters', { parameters: req.params })
73 checkErrors(req, res, function () {
74 checkVideoExists(req.params.id, res, next)
78 function videosRemoveValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
79 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
81 logger.debug('Checking videosRemove parameters', { parameters: req.params })
83 checkErrors(req, res, function () {
84 checkVideoExists(req.params.id, res, function () {
85 // We need to make additional checks
87 // Check if the user who did the request is able to delete the video
88 checkUserCanDeleteVideo(res.locals.oauth.token.User.id, res, function () {
95 function videosSearchValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
96 const searchableColumns = SEARCHABLE_COLUMNS.VIDEOS
97 req.checkParams('value', 'Should have a valid search').notEmpty()
98 req.checkQuery('field', 'Should have correct searchable column').optional().isIn(searchableColumns)
100 logger.debug('Checking videosSearch parameters', { parameters: req.params })
102 checkErrors(req, res, next)
105 function videoAbuseReportValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
106 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
107 req.checkBody('reason', 'Should have a valid reason').isVideoAbuseReasonValid()
109 logger.debug('Checking videoAbuseReport parameters', { parameters: req.body })
111 checkErrors(req, res, function () {
112 checkVideoExists(req.params.id, res, next)
116 function videoRateValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
117 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
118 req.checkBody('rating', 'Should have a valid rate type').isVideoRatingTypeValid()
120 logger.debug('Checking videoRate parameters', { parameters: req.body })
122 checkErrors(req, res, function () {
123 checkVideoExists(req.params.id, res, next)
127 function videosBlacklistValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
128 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
130 logger.debug('Checking videosBlacklist parameters', { parameters: req.params })
132 checkErrors(req, res, function () {
133 checkVideoExists(req.params.id, res, function () {
134 checkVideoIsBlacklistable(req, res, next)
139 // ---------------------------------------------------------------------------
143 videosUpdateValidator,
145 videosRemoveValidator,
146 videosSearchValidator,
148 videoAbuseReportValidator,
152 videosBlacklistValidator
155 // ---------------------------------------------------------------------------
157 function checkVideoExists (id: string, res: express.Response, callback: () => void) {
158 db.Video.loadAndPopulateAuthorAndPodAndTags(id, function (err, video) {
160 logger.error('Error in video request validator.', { error: err })
161 return res.sendStatus(500)
164 if (!video) return res.status(404).send('Video not found')
166 res.locals.video = video
171 function checkUserCanDeleteVideo (userId: number, res: express.Response, callback: () => void) {
172 // Retrieve the user who did the request
173 db.User.loadById(userId, function (err, user) {
175 logger.error('Error in video request validator.', { error: err })
176 return res.sendStatus(500)
179 // Check if the user can delete the video
180 // The user can delete it if s/he is an admin
181 // Or if s/he is the video's author
182 if (user.isAdmin() === false) {
183 if (res.locals.video.isOwned() === false) {
184 return res.status(403).send('Cannot remove video of another pod')
187 if (res.locals.video.Author.userId !== res.locals.oauth.token.User.id) {
188 return res.status(403).send('Cannot remove video of another user')
192 // If we reach this comment, we can delete the video
197 function checkVideoIsBlacklistable (req: express.Request, res: express.Response, callback: () => void) {
198 if (res.locals.video.isOwned() === true) {
199 return res.status(403).send('Cannot blacklist a local video')