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 // FIXME: Don't write an error message, it seems there is a bug with express-validator
12 // 'Should have a valid file'
13 req.checkBody('videofile').isVideoFile(req.files)
14 req.checkBody('name', 'Should have a valid name').isVideoNameValid()
15 req.checkBody('category', 'Should have a valid category').isVideoCategoryValid()
16 req.checkBody('licence', 'Should have a valid licence').isVideoLicenceValid()
17 req.checkBody('language', 'Should have a valid language').optional().isVideoLanguageValid()
18 req.checkBody('nsfw', 'Should have a valid NSFW attribute').isVideoNSFWValid()
19 req.checkBody('description', 'Should have a valid description').isVideoDescriptionValid()
20 req.checkBody('tags', 'Should have correct tags').optional().isVideoTagsValid()
22 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
24 checkErrors(req, res, function () {
25 const videoFile = req.files.videofile[0]
27 db.Video.getDurationFromFile(videoFile.path, function (err, duration) {
29 return res.status(400).send('Cannot retrieve metadata of the file.')
32 if (!isVideoDurationValid(duration)) {
33 return res.status(400).send('Duration of the video file is too big (max: ' + CONSTRAINTS_FIELDS.VIDEOS.DURATION.max + 's).')
36 videoFile['duration'] = duration
42 function videosUpdateValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
43 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
44 req.checkBody('name', 'Should have a valid name').optional().isVideoNameValid()
45 req.checkBody('category', 'Should have a valid category').optional().isVideoCategoryValid()
46 req.checkBody('licence', 'Should have a valid licence').optional().isVideoLicenceValid()
47 req.checkBody('language', 'Should have a valid language').optional().isVideoLanguageValid()
48 req.checkBody('nsfw', 'Should have a valid NSFW attribute').optional().isVideoNSFWValid()
49 req.checkBody('description', 'Should have a valid description').optional().isVideoDescriptionValid()
50 req.checkBody('tags', 'Should have correct tags').optional().isVideoTagsValid()
52 logger.debug('Checking videosUpdate parameters', { parameters: req.body })
54 checkErrors(req, res, function () {
55 checkVideoExists(req.params.id, res, function () {
56 // We need to make additional checks
57 if (res.locals.video.isOwned() === false) {
58 return res.status(403).send('Cannot update video of another pod')
61 if (res.locals.video.Author.userId !== res.locals.oauth.token.User.id) {
62 return res.status(403).send('Cannot update video of another user')
70 function videosGetValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
71 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
73 logger.debug('Checking videosGet parameters', { parameters: req.params })
75 checkErrors(req, res, function () {
76 checkVideoExists(req.params.id, res, next)
80 function videosRemoveValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
81 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
83 logger.debug('Checking videosRemove parameters', { parameters: req.params })
85 checkErrors(req, res, function () {
86 checkVideoExists(req.params.id, res, function () {
87 // We need to make additional checks
89 // Check if the user who did the request is able to delete the video
90 checkUserCanDeleteVideo(res.locals.oauth.token.User.id, res, function () {
97 function videosSearchValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
98 const searchableColumns = SEARCHABLE_COLUMNS.VIDEOS
99 req.checkParams('value', 'Should have a valid search').notEmpty()
100 req.checkQuery('field', 'Should have correct searchable column').optional().isIn(searchableColumns)
102 logger.debug('Checking videosSearch parameters', { parameters: req.params })
104 checkErrors(req, res, next)
107 function videoAbuseReportValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
108 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
109 req.checkBody('reason', 'Should have a valid reason').isVideoAbuseReasonValid()
111 logger.debug('Checking videoAbuseReport parameters', { parameters: req.body })
113 checkErrors(req, res, function () {
114 checkVideoExists(req.params.id, res, next)
118 function videoRateValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
119 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
120 req.checkBody('rating', 'Should have a valid rate type').isVideoRatingTypeValid()
122 logger.debug('Checking videoRate parameters', { parameters: req.body })
124 checkErrors(req, res, function () {
125 checkVideoExists(req.params.id, res, next)
129 function videosBlacklistValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
130 req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4)
132 logger.debug('Checking videosBlacklist parameters', { parameters: req.params })
134 checkErrors(req, res, function () {
135 checkVideoExists(req.params.id, res, function () {
136 checkVideoIsBlacklistable(req, res, next)
141 // ---------------------------------------------------------------------------
145 videosUpdateValidator,
147 videosRemoveValidator,
148 videosSearchValidator,
150 videoAbuseReportValidator,
154 videosBlacklistValidator
157 // ---------------------------------------------------------------------------
159 function checkVideoExists (id: string, res: express.Response, callback: () => void) {
160 db.Video.loadAndPopulateAuthorAndPodAndTags(id, function (err, video) {
162 logger.error('Error in video request validator.', { error: err })
163 return res.sendStatus(500)
166 if (!video) return res.status(404).send('Video not found')
168 res.locals.video = video
173 function checkUserCanDeleteVideo (userId: number, res: express.Response, callback: () => void) {
174 // Retrieve the user who did the request
175 db.User.loadById(userId, function (err, user) {
177 logger.error('Error in video request validator.', { error: err })
178 return res.sendStatus(500)
181 // Check if the user can delete the video
182 // The user can delete it if s/he is an admin
183 // Or if s/he is the video's author
184 if (user.isAdmin() === false) {
185 if (res.locals.video.isOwned() === false) {
186 return res.status(403).send('Cannot remove video of another pod')
189 if (res.locals.video.Author.userId !== res.locals.oauth.token.User.id) {
190 return res.status(403).send('Cannot remove video of another user')
194 // If we reach this comment, we can delete the video
199 function checkVideoIsBlacklistable (req: express.Request, res: express.Response, callback: () => void) {
200 if (res.locals.video.isOwned() === true) {
201 return res.status(403).send('Cannot blacklist a local video')