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-07-11 09:01:56 -05:00
|
|
|
import * as Promise from 'bluebird'
|
|
|
|
import * as validator from 'validator'
|
2017-06-10 15:15:25 -05:00
|
|
|
|
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,
|
|
|
|
isVideoIdOrUUIDValid,
|
|
|
|
isVideoAbuseReasonValid,
|
|
|
|
isVideoRatingTypeValid
|
|
|
|
} from '../../helpers'
|
2017-07-11 09:01:56 -05:00
|
|
|
import { VideoInstance } from '../../models'
|
2015-11-07 07:16:26 -06:00
|
|
|
|
2017-09-15 05:17:08 -05:00
|
|
|
const videosAddValidator = [
|
|
|
|
body('videofile').custom((value, { req }) => isVideoFile(req.files)).withMessage('Should have a valid file'),
|
|
|
|
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'),
|
|
|
|
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
|
|
|
|
|
|
|
|
user.isAbleToUploadVideo(videoFile)
|
|
|
|
.then(isAble => {
|
|
|
|
if (isAble === false) {
|
|
|
|
res.status(403)
|
|
|
|
.json({ error: 'The user video quota is exceeded with this video.' })
|
|
|
|
.end()
|
|
|
|
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
return db.Video.getDurationFromFile(videoFile.path)
|
|
|
|
.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
|
|
|
|
if (duration === undefined) return
|
|
|
|
|
|
|
|
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 = [
|
|
|
|
param('id').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
|
|
|
|
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-09-15 05:17:08 -05:00
|
|
|
if (res.locals.video.Author.userId !== res.locals.oauth.token.User.id) {
|
|
|
|
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 = [
|
|
|
|
param('id').custom(isVideoIdOrUUIDValid).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 = [
|
|
|
|
param('id').custom(isVideoIdOrUUIDValid).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 = [
|
|
|
|
param('id').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
|
|
|
|
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 = [
|
|
|
|
param('id').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
|
|
|
|
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
|
|
|
|
2017-09-15 05:17:08 -05:00
|
|
|
const videosBlacklistValidator = [
|
|
|
|
param('id').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
|
2017-04-26 14:42:36 -05:00
|
|
|
|
2017-09-15 05:17:08 -05:00
|
|
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
logger.debug('Checking videosBlacklist parameters', { parameters: req.params })
|
2017-04-26 14:42:36 -05:00
|
|
|
|
2017-09-15 05:17:08 -05:00
|
|
|
checkErrors(req, res, () => {
|
|
|
|
checkVideoExists(req.params.id, res, () => {
|
|
|
|
checkVideoIsBlacklistable(req, res, next)
|
|
|
|
})
|
2017-04-26 14:42:36 -05:00
|
|
|
})
|
2017-09-15 05:17:08 -05:00
|
|
|
}
|
|
|
|
]
|
2017-04-26 14:42:36 -05: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,
|
|
|
|
|
|
|
|
videoRateValidator,
|
|
|
|
|
|
|
|
videosBlacklistValidator
|
|
|
|
}
|
2016-12-29 12:07:05 -06:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-06-10 15:15:25 -05:00
|
|
|
function checkVideoExists (id: string, res: express.Response, callback: () => void) {
|
2017-07-11 09:01:56 -05:00
|
|
|
let promise: Promise<VideoInstance>
|
|
|
|
if (validator.isInt(id)) {
|
|
|
|
promise = db.Video.loadAndPopulateAuthorAndPodAndTags(+id)
|
|
|
|
} else { // UUID
|
|
|
|
promise = db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
promise.then(video => {
|
2017-09-14 10:06:31 -05:00
|
|
|
if (!video) {
|
|
|
|
return res.status(404)
|
|
|
|
.json({ error: 'Video not found' })
|
|
|
|
.end()
|
|
|
|
}
|
2016-12-29 12:07:05 -06:00
|
|
|
|
|
|
|
res.locals.video = video
|
|
|
|
callback()
|
|
|
|
})
|
2017-07-05 06:26:25 -05:00
|
|
|
.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)
|
|
|
|
})
|
2016-12-29 12:07:05 -06:00
|
|
|
}
|
2017-04-26 14:22:10 -05: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-09-12 05:53:55 -05:00
|
|
|
if (user.isAdmin() === 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
|
|
|
}
|
|
|
|
|
2017-06-10 15:15:25 -05:00
|
|
|
function checkVideoIsBlacklistable (req: express.Request, res: express.Response, callback: () => void) {
|
2017-04-26 14:22:10 -05:00
|
|
|
if (res.locals.video.isOwned() === true) {
|
2017-09-14 10:06:31 -05:00
|
|
|
return res.status(403)
|
|
|
|
.json({ error: 'Cannot blacklist a local video' })
|
|
|
|
.end()
|
2017-04-26 14:22:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
callback()
|
|
|
|
}
|