2017-09-15 05:17:08 -05:00
|
|
|
import { body, param, query } from 'express-validator/check'
|
2017-06-10 15:15:25 -05:00
|
|
|
import * as express from 'express'
|
|
|
|
|
2017-05-22 13:58:25 -05:00
|
|
|
import { database as db } from '../../initializers/database'
|
2017-05-15 15:22:03 -05:00
|
|
|
import { checkErrors } from './utils'
|
|
|
|
import { CONSTRAINTS_FIELDS, SEARCHABLE_COLUMNS } from '../../initializers'
|
2017-09-15 05:17:08 -05:00
|
|
|
import {
|
|
|
|
logger,
|
|
|
|
isVideoDurationValid,
|
|
|
|
isVideoFile,
|
|
|
|
isVideoNameValid,
|
|
|
|
isVideoCategoryValid,
|
|
|
|
isVideoLicenceValid,
|
|
|
|
isVideoDescriptionValid,
|
|
|
|
isVideoLanguageValid,
|
|
|
|
isVideoTagsValid,
|
|
|
|
isVideoNSFWValid,
|
2017-10-24 12:41:09 -05:00
|
|
|
isIdOrUUIDValid,
|
2017-09-15 05:17:08 -05:00
|
|
|
isVideoAbuseReasonValid,
|
2017-10-09 04:06:13 -05:00
|
|
|
isVideoRatingTypeValid,
|
2017-10-10 03:02:18 -05:00
|
|
|
getDurationFromVideoFile,
|
2017-10-24 12:41:09 -05:00
|
|
|
checkVideoExists,
|
|
|
|
isIdValid
|
2017-09-15 05:17:08 -05:00
|
|
|
} from '../../helpers'
|
2017-10-27 09:55:03 -05:00
|
|
|
import { UserRight } from '../../../shared'
|
2015-11-07 07:16:26 -06:00
|
|
|
|
2017-09-15 05:17:08 -05:00
|
|
|
const videosAddValidator = [
|
2017-10-16 04:43:40 -05:00
|
|
|
body('videofile').custom((value, { req }) => isVideoFile(req.files)).withMessage(
|
2017-10-19 03:22:07 -05:00
|
|
|
'This file is not supported. Please, make sure it is of the following type : '
|
|
|
|
+ CONSTRAINTS_FIELDS.VIDEOS.EXTNAME.join(', ')
|
2017-10-16 04:43:40 -05:00
|
|
|
),
|
2017-09-15 05:17:08 -05:00
|
|
|
body('name').custom(isVideoNameValid).withMessage('Should have a valid name'),
|
|
|
|
body('category').custom(isVideoCategoryValid).withMessage('Should have a valid category'),
|
|
|
|
body('licence').custom(isVideoLicenceValid).withMessage('Should have a valid licence'),
|
|
|
|
body('language').optional().custom(isVideoLanguageValid).withMessage('Should have a valid language'),
|
|
|
|
body('nsfw').custom(isVideoNSFWValid).withMessage('Should have a valid NSFW attribute'),
|
|
|
|
body('description').custom(isVideoDescriptionValid).withMessage('Should have a valid description'),
|
2017-10-24 12:41:09 -05:00
|
|
|
body('channelId').custom(isIdValid).withMessage('Should have correct video channel id'),
|
2017-09-15 05:17:08 -05:00
|
|
|
body('tags').optional().custom(isVideoTagsValid).withMessage('Should have correct tags'),
|
|
|
|
|
|
|
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
|
|
|
|
|
|
|
|
checkErrors(req, res, () => {
|
|
|
|
const videoFile: Express.Multer.File = req.files['videofile'][0]
|
|
|
|
const user = res.locals.oauth.token.User
|
|
|
|
|
2017-10-24 12:41:09 -05:00
|
|
|
return db.VideoChannel.loadByIdAndAuthor(req.body.channelId, user.Author.id)
|
|
|
|
.then(videoChannel => {
|
|
|
|
if (!videoChannel) {
|
|
|
|
res.status(400)
|
|
|
|
.json({ error: 'Unknown video video channel for this author.' })
|
|
|
|
.end()
|
|
|
|
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
res.locals.videoChannel = videoChannel
|
|
|
|
|
|
|
|
return user.isAbleToUploadVideo(videoFile)
|
|
|
|
})
|
2017-09-15 05:17:08 -05:00
|
|
|
.then(isAble => {
|
|
|
|
if (isAble === false) {
|
|
|
|
res.status(403)
|
|
|
|
.json({ error: 'The user video quota is exceeded with this video.' })
|
|
|
|
.end()
|
|
|
|
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
2017-10-09 04:06:13 -05:00
|
|
|
return getDurationFromVideoFile(videoFile.path)
|
2017-09-15 05:17:08 -05:00
|
|
|
.catch(err => {
|
|
|
|
logger.error('Invalid input file in videosAddValidator.', err)
|
|
|
|
res.status(400)
|
|
|
|
.json({ error: 'Invalid input file.' })
|
|
|
|
.end()
|
|
|
|
|
|
|
|
return undefined
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.then(duration => {
|
|
|
|
// Previous test failed, abort
|
2017-10-25 09:52:01 -05:00
|
|
|
if (duration === undefined) return undefined
|
2017-09-15 05:17:08 -05:00
|
|
|
|
|
|
|
if (!isVideoDurationValid('' + duration)) {
|
|
|
|
return res.status(400)
|
|
|
|
.json({
|
|
|
|
error: 'Duration of the video file is too big (max: ' + CONSTRAINTS_FIELDS.VIDEOS.DURATION.max + 's).'
|
|
|
|
})
|
|
|
|
.end()
|
|
|
|
}
|
|
|
|
|
|
|
|
videoFile['duration'] = duration
|
|
|
|
next()
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
logger.error('Error in video add validator', err)
|
|
|
|
res.sendStatus(500)
|
2017-09-04 13:07:54 -05:00
|
|
|
|
|
|
|
return undefined
|
2017-09-15 05:17:08 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
const videosUpdateValidator = [
|
2017-10-24 12:41:09 -05:00
|
|
|
param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
|
2017-09-15 05:17:08 -05:00
|
|
|
body('name').optional().custom(isVideoNameValid).withMessage('Should have a valid name'),
|
|
|
|
body('category').optional().custom(isVideoCategoryValid).withMessage('Should have a valid category'),
|
|
|
|
body('licence').optional().custom(isVideoLicenceValid).withMessage('Should have a valid licence'),
|
|
|
|
body('language').optional().custom(isVideoLanguageValid).withMessage('Should have a valid language'),
|
|
|
|
body('nsfw').optional().custom(isVideoNSFWValid).withMessage('Should have a valid NSFW attribute'),
|
|
|
|
body('description').optional().custom(isVideoDescriptionValid).withMessage('Should have a valid description'),
|
|
|
|
body('tags').optional().custom(isVideoTagsValid).withMessage('Should have correct tags'),
|
|
|
|
|
|
|
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
logger.debug('Checking videosUpdate parameters', { parameters: req.body })
|
|
|
|
|
|
|
|
checkErrors(req, res, () => {
|
|
|
|
checkVideoExists(req.params.id, res, () => {
|
|
|
|
// We need to make additional checks
|
|
|
|
if (res.locals.video.isOwned() === false) {
|
|
|
|
return res.status(403)
|
|
|
|
.json({ error: 'Cannot update video of another pod' })
|
|
|
|
.end()
|
2017-09-04 13:07:54 -05:00
|
|
|
}
|
|
|
|
|
2017-10-24 12:41:09 -05:00
|
|
|
if (res.locals.video.VideoChannel.Author.userId !== res.locals.oauth.token.User.id) {
|
2017-09-15 05:17:08 -05:00
|
|
|
return res.status(403)
|
|
|
|
.json({ error: 'Cannot update video of another user' })
|
2017-09-14 10:06:31 -05:00
|
|
|
.end()
|
2017-07-05 06:26:25 -05:00
|
|
|
}
|
2016-05-16 12:49:10 -05:00
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
next()
|
|
|
|
})
|
2017-01-11 12:15:23 -06:00
|
|
|
})
|
2017-09-15 05:17:08 -05:00
|
|
|
}
|
|
|
|
]
|
2016-02-04 14:10:33 -06:00
|
|
|
|
2017-09-15 05:17:08 -05:00
|
|
|
const videosGetValidator = [
|
2017-10-24 12:41:09 -05:00
|
|
|
param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
|
2015-11-07 07:16:26 -06:00
|
|
|
|
2017-09-15 05:17:08 -05:00
|
|
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
logger.debug('Checking videosGet parameters', { parameters: req.params })
|
2016-12-29 12:07:05 -06:00
|
|
|
|
2017-09-15 05:17:08 -05:00
|
|
|
checkErrors(req, res, () => {
|
|
|
|
checkVideoExists(req.params.id, res, next)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
]
|
2015-11-07 07:16:26 -06:00
|
|
|
|
2017-09-15 05:17:08 -05:00
|
|
|
const videosRemoveValidator = [
|
2017-10-24 12:41:09 -05:00
|
|
|
param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
|
2015-11-07 07:16:26 -06:00
|
|
|
|
2017-09-15 05:17:08 -05:00
|
|
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
logger.debug('Checking videosRemove parameters', { parameters: req.params })
|
2015-11-07 07:16:26 -06:00
|
|
|
|
2017-09-15 05:17:08 -05:00
|
|
|
checkErrors(req, res, () => {
|
|
|
|
checkVideoExists(req.params.id, res, () => {
|
|
|
|
// Check if the user who did the request is able to delete the video
|
|
|
|
checkUserCanDeleteVideo(res.locals.oauth.token.User.id, res, () => {
|
|
|
|
next()
|
|
|
|
})
|
2017-04-26 14:22:10 -05:00
|
|
|
})
|
2015-11-07 07:16:26 -06:00
|
|
|
})
|
2017-09-15 05:17:08 -05:00
|
|
|
}
|
|
|
|
]
|
2015-11-07 07:16:26 -06:00
|
|
|
|
2017-09-15 05:17:08 -05:00
|
|
|
const videosSearchValidator = [
|
|
|
|
param('value').not().isEmpty().withMessage('Should have a valid search'),
|
|
|
|
query('field').optional().isIn(SEARCHABLE_COLUMNS.VIDEOS).withMessage('Should have correct searchable column'),
|
2016-01-31 04:23:52 -06:00
|
|
|
|
2017-09-15 05:17:08 -05:00
|
|
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
logger.debug('Checking videosSearch parameters', { parameters: req.params })
|
2016-01-31 04:23:52 -06:00
|
|
|
|
2017-09-15 05:17:08 -05:00
|
|
|
checkErrors(req, res, next)
|
|
|
|
}
|
|
|
|
]
|
2016-01-31 04:23:52 -06:00
|
|
|
|
2017-09-15 05:17:08 -05:00
|
|
|
const videoAbuseReportValidator = [
|
2017-10-24 12:41:09 -05:00
|
|
|
param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
|
2017-09-15 05:17:08 -05:00
|
|
|
body('reason').custom(isVideoAbuseReasonValid).withMessage('Should have a valid reason'),
|
2017-01-04 13:59:23 -06:00
|
|
|
|
2017-09-15 05:17:08 -05:00
|
|
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
logger.debug('Checking videoAbuseReport parameters', { parameters: req.body })
|
2017-01-04 13:59:23 -06:00
|
|
|
|
2017-09-15 05:17:08 -05:00
|
|
|
checkErrors(req, res, () => {
|
|
|
|
checkVideoExists(req.params.id, res, next)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
]
|
2017-01-04 13:59:23 -06:00
|
|
|
|
2017-09-15 05:17:08 -05:00
|
|
|
const videoRateValidator = [
|
2017-10-24 12:41:09 -05:00
|
|
|
param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
|
2017-09-15 05:17:08 -05:00
|
|
|
body('rating').custom(isVideoRatingTypeValid).withMessage('Should have a valid rate type'),
|
2017-03-08 14:35:43 -06:00
|
|
|
|
2017-09-15 05:17:08 -05:00
|
|
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
logger.debug('Checking videoRate parameters', { parameters: req.body })
|
2017-03-08 14:35:43 -06:00
|
|
|
|
2017-09-15 05:17:08 -05:00
|
|
|
checkErrors(req, res, () => {
|
|
|
|
checkVideoExists(req.params.id, res, next)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
]
|
2017-03-08 14:35:43 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
// ---------------------------------------------------------------------------
|
2016-01-31 04:23:52 -06:00
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
export {
|
|
|
|
videosAddValidator,
|
|
|
|
videosUpdateValidator,
|
|
|
|
videosGetValidator,
|
|
|
|
videosRemoveValidator,
|
|
|
|
videosSearchValidator,
|
|
|
|
|
|
|
|
videoAbuseReportValidator,
|
|
|
|
|
2017-10-10 03:02:18 -05:00
|
|
|
videoRateValidator
|
2017-05-15 15:22:03 -05:00
|
|
|
}
|
2016-12-29 12:07:05 -06:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-06-10 15:15:25 -05:00
|
|
|
function checkUserCanDeleteVideo (userId: number, res: express.Response, callback: () => void) {
|
2017-04-26 14:22:10 -05:00
|
|
|
// Retrieve the user who did the request
|
2017-07-05 06:26:25 -05:00
|
|
|
db.User.loadById(userId)
|
|
|
|
.then(user => {
|
2017-09-12 05:53:55 -05:00
|
|
|
if (res.locals.video.isOwned() === false) {
|
2017-09-14 10:06:31 -05:00
|
|
|
return res.status(403)
|
|
|
|
.json({ error: 'Cannot remove video of another pod, blacklist it' })
|
|
|
|
.end()
|
2017-09-12 05:53:55 -05:00
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
// Check if the user can delete the video
|
|
|
|
// The user can delete it if s/he is an admin
|
|
|
|
// Or if s/he is the video's author
|
2017-10-27 09:55:03 -05:00
|
|
|
if (user.hasRight(UserRight.REMOVE_ANY_VIDEO) === false && res.locals.video.Author.userId !== res.locals.oauth.token.User.id) {
|
2017-09-14 10:06:31 -05:00
|
|
|
return res.status(403)
|
|
|
|
.json({ error: 'Cannot remove video of another user' })
|
|
|
|
.end()
|
2017-04-26 14:22:10 -05:00
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
// If we reach this comment, we can delete the video
|
|
|
|
callback()
|
|
|
|
})
|
|
|
|
.catch(err => {
|
2017-07-07 11:26:12 -05:00
|
|
|
logger.error('Error in video request validator.', err)
|
2017-07-05 06:26:25 -05:00
|
|
|
return res.sendStatus(500)
|
|
|
|
})
|
2017-04-26 14:22:10 -05:00
|
|
|
}
|