1 import { body, param, query } from 'express-validator/check'
2 import * as express from 'express'
4 import { database as db } from '../../initializers/database'
5 import { checkErrors } from './utils'
6 import { CONSTRAINTS_FIELDS, SEARCHABLE_COLUMNS } from '../../initializers'
14 isVideoDescriptionValid,
19 isVideoAbuseReasonValid,
20 isVideoRatingTypeValid,
21 getDurationFromVideoFile,
23 } from '../../helpers'
25 const videosAddValidator = [
26 body('videofile').custom((value, { req }) => isVideoFile(req.files)).withMessage('Should have a valid file'),
27 body('name').custom(isVideoNameValid).withMessage('Should have a valid name'),
28 body('category').custom(isVideoCategoryValid).withMessage('Should have a valid category'),
29 body('licence').custom(isVideoLicenceValid).withMessage('Should have a valid licence'),
30 body('language').optional().custom(isVideoLanguageValid).withMessage('Should have a valid language'),
31 body('nsfw').custom(isVideoNSFWValid).withMessage('Should have a valid NSFW attribute'),
32 body('description').custom(isVideoDescriptionValid).withMessage('Should have a valid description'),
33 body('tags').optional().custom(isVideoTagsValid).withMessage('Should have correct tags'),
35 (req: express.Request, res: express.Response, next: express.NextFunction) => {
36 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
38 checkErrors(req, res, () => {
39 const videoFile: Express.Multer.File = req.files['videofile'][0]
40 const user = res.locals.oauth.token.User
42 user.isAbleToUploadVideo(videoFile)
44 if (isAble === false) {
46 .json({ error: 'The user video quota is exceeded with this video.' })
52 return getDurationFromVideoFile(videoFile.path)
54 logger.error('Invalid input file in videosAddValidator.', err)
56 .json({ error: 'Invalid input file.' })
63 // Previous test failed, abort
64 if (duration === undefined) return
66 if (!isVideoDurationValid('' + duration)) {
67 return res.status(400)
69 error: 'Duration of the video file is too big (max: ' + CONSTRAINTS_FIELDS.VIDEOS.DURATION.max + 's).'
74 videoFile['duration'] = duration
78 logger.error('Error in video add validator', err)
87 const videosUpdateValidator = [
88 param('id').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
89 body('name').optional().custom(isVideoNameValid).withMessage('Should have a valid name'),
90 body('category').optional().custom(isVideoCategoryValid).withMessage('Should have a valid category'),
91 body('licence').optional().custom(isVideoLicenceValid).withMessage('Should have a valid licence'),
92 body('language').optional().custom(isVideoLanguageValid).withMessage('Should have a valid language'),
93 body('nsfw').optional().custom(isVideoNSFWValid).withMessage('Should have a valid NSFW attribute'),
94 body('description').optional().custom(isVideoDescriptionValid).withMessage('Should have a valid description'),
95 body('tags').optional().custom(isVideoTagsValid).withMessage('Should have correct tags'),
97 (req: express.Request, res: express.Response, next: express.NextFunction) => {
98 logger.debug('Checking videosUpdate parameters', { parameters: req.body })
100 checkErrors(req, res, () => {
101 checkVideoExists(req.params.id, res, () => {
102 // We need to make additional checks
103 if (res.locals.video.isOwned() === false) {
104 return res.status(403)
105 .json({ error: 'Cannot update video of another pod' })
109 if (res.locals.video.Author.userId !== res.locals.oauth.token.User.id) {
110 return res.status(403)
111 .json({ error: 'Cannot update video of another user' })
121 const videosGetValidator = [
122 param('id').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
124 (req: express.Request, res: express.Response, next: express.NextFunction) => {
125 logger.debug('Checking videosGet parameters', { parameters: req.params })
127 checkErrors(req, res, () => {
128 checkVideoExists(req.params.id, res, next)
133 const videosRemoveValidator = [
134 param('id').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
136 (req: express.Request, res: express.Response, next: express.NextFunction) => {
137 logger.debug('Checking videosRemove parameters', { parameters: req.params })
139 checkErrors(req, res, () => {
140 checkVideoExists(req.params.id, res, () => {
141 // Check if the user who did the request is able to delete the video
142 checkUserCanDeleteVideo(res.locals.oauth.token.User.id, res, () => {
150 const videosSearchValidator = [
151 param('value').not().isEmpty().withMessage('Should have a valid search'),
152 query('field').optional().isIn(SEARCHABLE_COLUMNS.VIDEOS).withMessage('Should have correct searchable column'),
154 (req: express.Request, res: express.Response, next: express.NextFunction) => {
155 logger.debug('Checking videosSearch parameters', { parameters: req.params })
157 checkErrors(req, res, next)
161 const videoAbuseReportValidator = [
162 param('id').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
163 body('reason').custom(isVideoAbuseReasonValid).withMessage('Should have a valid reason'),
165 (req: express.Request, res: express.Response, next: express.NextFunction) => {
166 logger.debug('Checking videoAbuseReport parameters', { parameters: req.body })
168 checkErrors(req, res, () => {
169 checkVideoExists(req.params.id, res, next)
174 const videoRateValidator = [
175 param('id').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
176 body('rating').custom(isVideoRatingTypeValid).withMessage('Should have a valid rate type'),
178 (req: express.Request, res: express.Response, next: express.NextFunction) => {
179 logger.debug('Checking videoRate parameters', { parameters: req.body })
181 checkErrors(req, res, () => {
182 checkVideoExists(req.params.id, res, next)
187 // ---------------------------------------------------------------------------
191 videosUpdateValidator,
193 videosRemoveValidator,
194 videosSearchValidator,
196 videoAbuseReportValidator,
201 // ---------------------------------------------------------------------------
203 function checkUserCanDeleteVideo (userId: number, res: express.Response, callback: () => void) {
204 // Retrieve the user who did the request
205 db.User.loadById(userId)
207 if (res.locals.video.isOwned() === false) {
208 return res.status(403)
209 .json({ error: 'Cannot remove video of another pod, blacklist it' })
213 // Check if the user can delete the video
214 // The user can delete it if s/he is an admin
215 // Or if s/he is the video's author
216 if (user.isAdmin() === false && res.locals.video.Author.userId !== res.locals.oauth.token.User.id) {
217 return res.status(403)
218 .json({ error: 'Cannot remove video of another user' })
222 // If we reach this comment, we can delete the video
226 logger.error('Error in video request validator.', err)
227 return res.sendStatus(500)