2017-06-10 15:15:25 -05:00
|
|
|
import * as express from 'express'
|
2017-12-12 10:53:50 -06:00
|
|
|
import 'express-validator'
|
2017-11-23 10:53:38 -06:00
|
|
|
import { body, param, query } from 'express-validator/check'
|
|
|
|
import { UserRight, VideoPrivacy } from '../../../shared'
|
|
|
|
import { isIdOrUUIDValid, isIdValid } from '../../helpers/custom-validators/misc'
|
2017-09-15 05:17:08 -05:00
|
|
|
import {
|
2017-12-28 04:16:08 -06:00
|
|
|
isVideoAbuseReasonValid, isVideoCategoryValid, isVideoDescriptionValid, isVideoExist, isVideoFile, isVideoLanguageValid,
|
|
|
|
isVideoLicenceValid, isVideoNameValid, isVideoNSFWValid, isVideoPrivacyValid, isVideoRatingTypeValid, isVideoTagsValid
|
2017-11-23 10:53:38 -06:00
|
|
|
} from '../../helpers/custom-validators/videos'
|
2017-12-28 04:16:08 -06:00
|
|
|
import { getDurationFromVideoFile } from '../../helpers/ffmpeg-utils'
|
|
|
|
import { logger } from '../../helpers/logger'
|
2017-12-05 10:46:33 -06:00
|
|
|
import { CONSTRAINTS_FIELDS } from '../../initializers'
|
2017-12-12 10:53:50 -06:00
|
|
|
import { UserModel } from '../../models/account/user'
|
|
|
|
import { VideoModel } from '../../models/video/video'
|
|
|
|
import { VideoChannelModel } from '../../models/video/video-channel'
|
|
|
|
import { VideoShareModel } from '../../models/video/video-share'
|
2017-10-31 09:20:35 -05:00
|
|
|
import { authenticate } from '../oauth'
|
2017-11-27 10:30:46 -06:00
|
|
|
import { areValidationErrors } from './utils'
|
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'),
|
2017-12-07 10:03:56 -06:00
|
|
|
body('category').optional().custom(isVideoCategoryValid).withMessage('Should have a valid category'),
|
|
|
|
body('licence').optional().custom(isVideoLicenceValid).withMessage('Should have a valid licence'),
|
2017-09-15 05:17:08 -05:00
|
|
|
body('language').optional().custom(isVideoLanguageValid).withMessage('Should have a valid language'),
|
|
|
|
body('nsfw').custom(isVideoNSFWValid).withMessage('Should have a valid NSFW attribute'),
|
2017-12-07 10:03:56 -06:00
|
|
|
body('description').optional().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-10-31 05:52:52 -05:00
|
|
|
body('privacy').custom(isVideoPrivacyValid).withMessage('Should have correct video privacy'),
|
2017-09-15 05:17:08 -05:00
|
|
|
body('tags').optional().custom(isVideoTagsValid).withMessage('Should have correct tags'),
|
|
|
|
|
2017-11-27 10:30:46 -06:00
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
2017-09-15 05:17:08 -05:00
|
|
|
logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
|
|
|
|
|
2017-11-27 10:30:46 -06:00
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
|
|
|
|
const videoFile: Express.Multer.File = req.files['videofile'][0]
|
|
|
|
const user = res.locals.oauth.token.User
|
2017-09-15 05:17:08 -05:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
const videoChannel = await VideoChannelModel.loadByIdAndAccount(req.body.channelId, user.Account.id)
|
2017-11-27 10:30:46 -06:00
|
|
|
if (!videoChannel) {
|
|
|
|
res.status(400)
|
|
|
|
.json({ error: 'Unknown video video channel for this account.' })
|
|
|
|
.end()
|
2017-10-24 12:41:09 -05:00
|
|
|
|
2017-11-27 10:30:46 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res.locals.videoChannel = videoChannel
|
|
|
|
|
|
|
|
const isAble = await user.isAbleToUploadVideo(videoFile)
|
|
|
|
if (isAble === false) {
|
|
|
|
res.status(403)
|
|
|
|
.json({ error: 'The user video quota is exceeded with this video.' })
|
|
|
|
.end()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let duration: number
|
|
|
|
|
|
|
|
try {
|
|
|
|
duration = await getDurationFromVideoFile(videoFile.path)
|
|
|
|
} catch (err) {
|
|
|
|
logger.error('Invalid input file in videosAddValidator.', err)
|
|
|
|
res.status(400)
|
|
|
|
.json({ error: 'Invalid input file.' })
|
|
|
|
.end()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
videoFile['duration'] = duration
|
|
|
|
|
|
|
|
return next()
|
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'),
|
2017-10-31 09:20:35 -05:00
|
|
|
body('privacy').optional().custom(isVideoPrivacyValid).withMessage('Should have correct video privacy'),
|
2017-09-15 05:17:08 -05:00
|
|
|
body('description').optional().custom(isVideoDescriptionValid).withMessage('Should have a valid description'),
|
|
|
|
body('tags').optional().custom(isVideoTagsValid).withMessage('Should have correct tags'),
|
|
|
|
|
2017-11-27 10:30:46 -06:00
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
2017-09-15 05:17:08 -05:00
|
|
|
logger.debug('Checking videosUpdate parameters', { parameters: req.body })
|
|
|
|
|
2017-11-27 10:30:46 -06:00
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
if (!await isVideoExist(req.params.id, res)) return
|
|
|
|
|
|
|
|
const video = res.locals.video
|
|
|
|
|
|
|
|
// We need to make additional checks
|
|
|
|
if (video.isOwned() === false) {
|
|
|
|
return res.status(403)
|
|
|
|
.json({ error: 'Cannot update video of another server' })
|
|
|
|
.end()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (video.VideoChannel.Account.userId !== res.locals.oauth.token.User.id) {
|
|
|
|
return res.status(403)
|
|
|
|
.json({ error: 'Cannot update video of another user' })
|
|
|
|
.end()
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
return next()
|
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-11-27 10:30:46 -06:00
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
2017-09-15 05:17:08 -05:00
|
|
|
logger.debug('Checking videosGet parameters', { parameters: req.params })
|
2016-12-29 12:07:05 -06:00
|
|
|
|
2017-11-27 10:30:46 -06:00
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
if (!await isVideoExist(req.params.id, res)) return
|
2017-10-31 09:20:35 -05:00
|
|
|
|
2017-11-27 10:30:46 -06:00
|
|
|
const video = res.locals.video
|
2017-10-31 09:20:35 -05:00
|
|
|
|
2017-11-27 10:30:46 -06:00
|
|
|
// Video is not private, anyone can access it
|
|
|
|
if (video.privacy !== VideoPrivacy.PRIVATE) return next()
|
2017-10-31 09:20:35 -05:00
|
|
|
|
2017-11-27 10:30:46 -06:00
|
|
|
authenticate(req, res, () => {
|
|
|
|
if (video.VideoChannel.Account.userId !== res.locals.oauth.token.User.id) {
|
|
|
|
return res.status(403)
|
|
|
|
.json({ error: 'Cannot get this private video of another user' })
|
|
|
|
.end()
|
|
|
|
}
|
|
|
|
|
|
|
|
return next()
|
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 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-11-27 10:30:46 -06:00
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
2017-09-15 05:17:08 -05:00
|
|
|
logger.debug('Checking videosRemove parameters', { parameters: req.params })
|
2015-11-07 07:16:26 -06:00
|
|
|
|
2017-11-27 10:30:46 -06:00
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
if (!await isVideoExist(req.params.id, res)) return
|
|
|
|
|
|
|
|
// Check if the user who did the request is able to delete the video
|
|
|
|
if (!checkUserCanDeleteVideo(res.locals.oauth.token.User, res.locals.video, res)) return
|
|
|
|
|
|
|
|
return next()
|
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 = [
|
2017-12-05 10:46:33 -06:00
|
|
|
query('search').not().isEmpty().withMessage('Should have a valid search'),
|
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-11-27 10:30:46 -06:00
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
|
|
|
|
return next()
|
2017-09-15 05:17:08 -05:00
|
|
|
}
|
|
|
|
]
|
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-11-27 10:30:46 -06:00
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
2017-09-15 05:17:08 -05:00
|
|
|
logger.debug('Checking videoAbuseReport parameters', { parameters: req.body })
|
2017-01-04 13:59:23 -06:00
|
|
|
|
2017-11-27 10:30:46 -06:00
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
if (!await isVideoExist(req.params.id, res)) return
|
|
|
|
|
|
|
|
return next()
|
2017-09-15 05:17:08 -05:00
|
|
|
}
|
|
|
|
]
|
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-11-27 10:30:46 -06:00
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
2017-09-15 05:17:08 -05:00
|
|
|
logger.debug('Checking videoRate parameters', { parameters: req.body })
|
2017-03-08 14:35:43 -06:00
|
|
|
|
2017-11-27 10:30:46 -06:00
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
if (!await isVideoExist(req.params.id, res)) return
|
|
|
|
|
|
|
|
return next()
|
2017-09-15 05:17:08 -05:00
|
|
|
}
|
|
|
|
]
|
2017-03-08 14:35:43 -06:00
|
|
|
|
2017-11-27 07:44:51 -06:00
|
|
|
const videosShareValidator = [
|
|
|
|
param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
|
|
|
|
param('accountId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid account id'),
|
|
|
|
|
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
logger.debug('Checking videoShare parameters', { parameters: req.params })
|
|
|
|
|
|
|
|
if (areValidationErrors(req, res)) return
|
2017-11-27 10:30:46 -06:00
|
|
|
if (!await isVideoExist(req.params.id, res)) return
|
2017-11-27 07:44:51 -06:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
const share = await VideoShareModel.load(req.params.accountId, res.locals.video.id, undefined)
|
2017-11-27 07:44:51 -06:00
|
|
|
if (!share) {
|
|
|
|
return res.status(404)
|
|
|
|
.end()
|
|
|
|
}
|
|
|
|
|
|
|
|
res.locals.videoShare = share
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
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,
|
2017-11-27 07:44:51 -06:00
|
|
|
videosShareValidator,
|
2017-05-15 15:22:03 -05:00
|
|
|
|
|
|
|
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-12-12 10:53:50 -06:00
|
|
|
function checkUserCanDeleteVideo (user: UserModel, video: VideoModel, res: express.Response) {
|
2017-04-26 14:22:10 -05:00
|
|
|
// Retrieve the user who did the request
|
2017-11-27 10:30:46 -06:00
|
|
|
if (video.isOwned() === false) {
|
|
|
|
res.status(403)
|
2017-11-15 04:00:25 -06:00
|
|
|
.json({ error: 'Cannot remove video of another server, blacklist it' })
|
2017-10-31 09:20:35 -05:00
|
|
|
.end()
|
2017-11-27 10:30:46 -06:00
|
|
|
return false
|
2017-10-31 09:20:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the user can delete the video
|
|
|
|
// The user can delete it if s/he is an admin
|
2017-11-10 07:48:08 -06:00
|
|
|
// Or if s/he is the video's account
|
2017-11-27 10:30:46 -06:00
|
|
|
const account = video.VideoChannel.Account
|
2017-11-10 07:48:08 -06:00
|
|
|
if (user.hasRight(UserRight.REMOVE_ANY_VIDEO) === false && account.userId !== user.id) {
|
2017-11-27 10:30:46 -06:00
|
|
|
res.status(403)
|
2017-10-31 09:20:35 -05:00
|
|
|
.json({ error: 'Cannot remove video of another user' })
|
|
|
|
.end()
|
2017-11-27 10:30:46 -06:00
|
|
|
return false
|
2017-10-31 09:20:35 -05:00
|
|
|
}
|
|
|
|
|
2017-11-27 10:30:46 -06:00
|
|
|
return true
|
2017-04-26 14:22:10 -05:00
|
|
|
}
|