2017-09-15 06:17:08 -04:00
|
|
|
import { body, param, query } from 'express-validator/check'
|
2017-06-10 16:15:25 -04:00
|
|
|
import * as express from 'express'
|
|
|
|
|
2017-05-22 14:58:25 -04:00
|
|
|
import { database as db } from '../../initializers/database'
|
2017-05-15 16:22:03 -04:00
|
|
|
import { checkErrors } from './utils'
|
|
|
|
import { CONSTRAINTS_FIELDS, SEARCHABLE_COLUMNS } from '../../initializers'
|
2017-09-15 06:17:08 -04:00
|
|
|
import {
|
|
|
|
logger,
|
|
|
|
isVideoDurationValid,
|
|
|
|
isVideoFile,
|
|
|
|
isVideoNameValid,
|
|
|
|
isVideoCategoryValid,
|
|
|
|
isVideoLicenceValid,
|
|
|
|
isVideoDescriptionValid,
|
|
|
|
isVideoLanguageValid,
|
|
|
|
isVideoTagsValid,
|
|
|
|
isVideoNSFWValid,
|
2017-10-24 13:41:09 -04:00
|
|
|
isIdOrUUIDValid,
|
2017-09-15 06:17:08 -04:00
|
|
|
isVideoAbuseReasonValid,
|
2017-10-09 05:06:13 -04:00
|
|
|
isVideoRatingTypeValid,
|
2017-10-10 04:02:18 -04:00
|
|
|
getDurationFromVideoFile,
|
2017-10-24 13:41:09 -04:00
|
|
|
checkVideoExists,
|
2017-10-31 06:52:52 -04:00
|
|
|
isIdValid,
|
|
|
|
isVideoPrivacyValid
|
2017-09-15 06:17:08 -04:00
|
|
|
} from '../../helpers'
|
2017-10-31 06:52:52 -04:00
|
|
|
import { UserRight, VideoPrivacy } from '../../../shared'
|
2017-10-31 10:20:35 -04:00
|
|
|
import { authenticate } from '../oauth'
|
2015-11-07 08:16:26 -05:00
|
|
|
|
2017-09-15 06:17:08 -04:00
|
|
|
const videosAddValidator = [
|
2017-10-16 05:43:40 -04:00
|
|
|
body('videofile').custom((value, { req }) => isVideoFile(req.files)).withMessage(
|
2017-10-19 04:22:07 -04:00
|
|
|
'This file is not supported. Please, make sure it is of the following type : '
|
|
|
|
+ CONSTRAINTS_FIELDS.VIDEOS.EXTNAME.join(', ')
|
2017-10-16 05:43:40 -04:00
|
|
|
),
|
2017-09-15 06:17:08 -04: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 13:41:09 -04:00
|
|
|
body('channelId').custom(isIdValid).withMessage('Should have correct video channel id'),
|
2017-10-31 06:52:52 -04:00
|
|
|
body('privacy').custom(isVideoPrivacyValid).withMessage('Should have correct video privacy'),
|
2017-09-15 06:17:08 -04: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-11-10 08:48:08 -05:00
|
|
|
return db.VideoChannel.loadByIdAndAccount(req.body.channelId, user.Account.id)
|
2017-10-24 13:41:09 -04:00
|
|
|
.then(videoChannel => {
|
|
|
|
if (!videoChannel) {
|
|
|
|
res.status(400)
|
2017-11-10 08:48:08 -05:00
|
|
|
.json({ error: 'Unknown video video channel for this account.' })
|
2017-10-24 13:41:09 -04:00
|
|
|
.end()
|
|
|
|
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
res.locals.videoChannel = videoChannel
|
|
|
|
|
|
|
|
return user.isAbleToUploadVideo(videoFile)
|
|
|
|
})
|
2017-09-15 06:17:08 -04: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 05:06:13 -04:00
|
|
|
return getDurationFromVideoFile(videoFile.path)
|
2017-09-15 06:17:08 -04: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 10:52:01 -04:00
|
|
|
if (duration === undefined) return undefined
|
2017-09-15 06:17:08 -04: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 14:07:54 -04:00
|
|
|
|
|
|
|
return undefined
|
2017-09-15 06:17:08 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
const videosUpdateValidator = [
|
2017-10-24 13:41:09 -04:00
|
|
|
param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
|
2017-09-15 06:17:08 -04: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'),
|
2017-10-31 10:20:35 -04:00
|
|
|
body('privacy').optional().custom(isVideoPrivacyValid).withMessage('Should have correct video privacy'),
|
2017-09-15 06:17:08 -04:00
|
|
|
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, () => {
|
2017-10-31 06:52:52 -04:00
|
|
|
const video = res.locals.video
|
|
|
|
|
2017-09-15 06:17:08 -04:00
|
|
|
// We need to make additional checks
|
2017-10-31 06:52:52 -04:00
|
|
|
if (video.isOwned() === false) {
|
2017-09-15 06:17:08 -04:00
|
|
|
return res.status(403)
|
2017-11-15 05:00:25 -05:00
|
|
|
.json({ error: 'Cannot update video of another server' })
|
2017-09-15 06:17:08 -04:00
|
|
|
.end()
|
2017-09-04 14:07:54 -04:00
|
|
|
}
|
|
|
|
|
2017-11-10 08:48:08 -05:00
|
|
|
if (video.VideoChannel.Account.userId !== res.locals.oauth.token.User.id) {
|
2017-09-15 06:17:08 -04:00
|
|
|
return res.status(403)
|
|
|
|
.json({ error: 'Cannot update video of another user' })
|
2017-09-14 11:06:31 -04:00
|
|
|
.end()
|
2017-07-05 07:26:25 -04:00
|
|
|
}
|
2016-05-16 13:49:10 -04:00
|
|
|
|
2017-10-31 06:52:52 -04:00
|
|
|
if (video.privacy !== VideoPrivacy.PRIVATE && req.body.privacy === VideoPrivacy.PRIVATE) {
|
|
|
|
return res.status(409)
|
|
|
|
.json({ error: 'Cannot set "private" a video that was not private anymore.' })
|
|
|
|
.end()
|
|
|
|
}
|
|
|
|
|
2017-07-05 07:26:25 -04:00
|
|
|
next()
|
|
|
|
})
|
2017-01-11 13:15:23 -05:00
|
|
|
})
|
2017-09-15 06:17:08 -04:00
|
|
|
}
|
|
|
|
]
|
2016-02-04 15:10:33 -05:00
|
|
|
|
2017-09-15 06:17:08 -04:00
|
|
|
const videosGetValidator = [
|
2017-10-24 13:41:09 -04:00
|
|
|
param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
|
2015-11-07 08:16:26 -05:00
|
|
|
|
2017-09-15 06:17:08 -04:00
|
|
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
logger.debug('Checking videosGet parameters', { parameters: req.params })
|
2016-12-29 13:07:05 -05:00
|
|
|
|
2017-09-15 06:17:08 -04:00
|
|
|
checkErrors(req, res, () => {
|
2017-10-31 10:20:35 -04:00
|
|
|
checkVideoExists(req.params.id, res, () => {
|
|
|
|
const video = res.locals.video
|
|
|
|
|
|
|
|
// Video is not private, anyone can access it
|
|
|
|
if (video.privacy !== VideoPrivacy.PRIVATE) return next()
|
|
|
|
|
|
|
|
authenticate(req, res, () => {
|
2017-11-10 08:48:08 -05:00
|
|
|
if (video.VideoChannel.Account.userId !== res.locals.oauth.token.User.id) {
|
2017-10-31 10:20:35 -04:00
|
|
|
return res.status(403)
|
|
|
|
.json({ error: 'Cannot get this private video of another user' })
|
|
|
|
.end()
|
|
|
|
}
|
|
|
|
|
|
|
|
next()
|
|
|
|
})
|
|
|
|
})
|
2017-09-15 06:17:08 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
]
|
2015-11-07 08:16:26 -05:00
|
|
|
|
2017-09-15 06:17:08 -04:00
|
|
|
const videosRemoveValidator = [
|
2017-10-24 13:41:09 -04:00
|
|
|
param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
|
2015-11-07 08:16:26 -05:00
|
|
|
|
2017-09-15 06:17:08 -04:00
|
|
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
logger.debug('Checking videosRemove parameters', { parameters: req.params })
|
2015-11-07 08:16:26 -05:00
|
|
|
|
2017-09-15 06:17:08 -04: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 15:22:10 -04:00
|
|
|
})
|
2015-11-07 08:16:26 -05:00
|
|
|
})
|
2017-09-15 06:17:08 -04:00
|
|
|
}
|
|
|
|
]
|
2015-11-07 08:16:26 -05:00
|
|
|
|
2017-09-15 06:17:08 -04: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 05:23:52 -05:00
|
|
|
|
2017-09-15 06:17:08 -04:00
|
|
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
logger.debug('Checking videosSearch parameters', { parameters: req.params })
|
2016-01-31 05:23:52 -05:00
|
|
|
|
2017-09-15 06:17:08 -04:00
|
|
|
checkErrors(req, res, next)
|
|
|
|
}
|
|
|
|
]
|
2016-01-31 05:23:52 -05:00
|
|
|
|
2017-09-15 06:17:08 -04:00
|
|
|
const videoAbuseReportValidator = [
|
2017-10-24 13:41:09 -04:00
|
|
|
param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
|
2017-09-15 06:17:08 -04:00
|
|
|
body('reason').custom(isVideoAbuseReasonValid).withMessage('Should have a valid reason'),
|
2017-01-04 14:59:23 -05:00
|
|
|
|
2017-09-15 06:17:08 -04:00
|
|
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
logger.debug('Checking videoAbuseReport parameters', { parameters: req.body })
|
2017-01-04 14:59:23 -05:00
|
|
|
|
2017-09-15 06:17:08 -04:00
|
|
|
checkErrors(req, res, () => {
|
|
|
|
checkVideoExists(req.params.id, res, next)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
]
|
2017-01-04 14:59:23 -05:00
|
|
|
|
2017-09-15 06:17:08 -04:00
|
|
|
const videoRateValidator = [
|
2017-10-24 13:41:09 -04:00
|
|
|
param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
|
2017-09-15 06:17:08 -04:00
|
|
|
body('rating').custom(isVideoRatingTypeValid).withMessage('Should have a valid rate type'),
|
2017-03-08 15:35:43 -05:00
|
|
|
|
2017-09-15 06:17:08 -04:00
|
|
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
logger.debug('Checking videoRate parameters', { parameters: req.body })
|
2017-03-08 15:35:43 -05:00
|
|
|
|
2017-09-15 06:17:08 -04:00
|
|
|
checkErrors(req, res, () => {
|
|
|
|
checkVideoExists(req.params.id, res, next)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
]
|
2017-03-08 15:35:43 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
2016-01-31 05:23:52 -05:00
|
|
|
|
2017-05-15 16:22:03 -04:00
|
|
|
export {
|
|
|
|
videosAddValidator,
|
|
|
|
videosUpdateValidator,
|
|
|
|
videosGetValidator,
|
|
|
|
videosRemoveValidator,
|
|
|
|
videosSearchValidator,
|
|
|
|
|
|
|
|
videoAbuseReportValidator,
|
|
|
|
|
2017-10-10 04:02:18 -04:00
|
|
|
videoRateValidator
|
2017-05-15 16:22:03 -04:00
|
|
|
}
|
2016-12-29 13:07:05 -05:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
function checkUserCanDeleteVideo (userId: number, res: express.Response, callback: () => void) {
|
2017-04-26 15:22:10 -04:00
|
|
|
// Retrieve the user who did the request
|
2017-10-31 10:20:35 -04:00
|
|
|
if (res.locals.video.isOwned() === false) {
|
|
|
|
return res.status(403)
|
2017-11-15 05:00:25 -05:00
|
|
|
.json({ error: 'Cannot remove video of another server, blacklist it' })
|
2017-10-31 10:20:35 -04:00
|
|
|
.end()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the user can delete the video
|
|
|
|
// The user can delete it if s/he is an admin
|
2017-11-10 08:48:08 -05:00
|
|
|
// Or if s/he is the video's account
|
|
|
|
const account = res.locals.video.VideoChannel.Account
|
2017-10-31 10:20:35 -04:00
|
|
|
const user = res.locals.oauth.token.User
|
2017-11-10 08:48:08 -05:00
|
|
|
if (user.hasRight(UserRight.REMOVE_ANY_VIDEO) === false && account.userId !== user.id) {
|
2017-10-31 10:20:35 -04:00
|
|
|
return res.status(403)
|
|
|
|
.json({ error: 'Cannot remove video of another user' })
|
|
|
|
.end()
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we reach this comment, we can delete the video
|
|
|
|
callback()
|
2017-04-26 15:22:10 -04:00
|
|
|
}
|