2017-06-10 15:15:25 -05:00
|
|
|
import * as express from 'express'
|
2017-12-12 10:53:50 -06:00
|
|
|
import 'express-validator'
|
2018-07-19 09:17:54 -05:00
|
|
|
import { body, param, ValidationChain } from 'express-validator/check'
|
2017-11-23 10:53:38 -06:00
|
|
|
import { UserRight, VideoPrivacy } from '../../../shared'
|
2017-09-15 05:17:08 -05:00
|
|
|
import {
|
2018-06-14 11:06:56 -05:00
|
|
|
isBooleanValid,
|
|
|
|
isDateValid,
|
|
|
|
isIdOrUUIDValid,
|
|
|
|
isIdValid,
|
|
|
|
isUUIDValid,
|
|
|
|
toIntOrNull,
|
|
|
|
toValueOrNull
|
|
|
|
} from '../../helpers/custom-validators/misc'
|
|
|
|
import {
|
2018-07-12 12:02:00 -05:00
|
|
|
checkUserCanManageVideo,
|
2018-06-14 11:06:56 -05:00
|
|
|
isScheduleVideoUpdatePrivacyValid,
|
2018-02-13 11:17:05 -06:00
|
|
|
isVideoCategoryValid,
|
2018-05-11 08:10:13 -05:00
|
|
|
isVideoChannelOfAccountExist,
|
2018-02-13 11:17:05 -06:00
|
|
|
isVideoDescriptionValid,
|
|
|
|
isVideoExist,
|
|
|
|
isVideoFile,
|
|
|
|
isVideoImage,
|
|
|
|
isVideoLanguageValid,
|
|
|
|
isVideoLicenceValid,
|
|
|
|
isVideoNameValid,
|
|
|
|
isVideoPrivacyValid,
|
2018-05-09 04:23:14 -05:00
|
|
|
isVideoRatingTypeValid,
|
|
|
|
isVideoSupportValid,
|
2018-02-13 11:17:05 -06:00
|
|
|
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 { 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'
|
2018-08-14 08:28:30 -05:00
|
|
|
import { cleanUpReqFiles } from '../../helpers/express-utils'
|
2018-08-14 02:08:47 -05:00
|
|
|
import { VideoModel } from '../../models/video/video'
|
|
|
|
import { UserModel } from '../../models/account/user'
|
2015-11-07 07:16:26 -06:00
|
|
|
|
2018-07-16 07:58:22 -05:00
|
|
|
const videosAddValidator = getCommonVideoAttributes().concat([
|
2018-06-22 08:42:55 -05:00
|
|
|
body('videofile')
|
|
|
|
.custom((value, { req }) => isVideoFile(req.files)).withMessage(
|
2018-07-12 12:02:00 -05:00
|
|
|
'This file is not supported or too large. Please, make sure it is of the following type: '
|
2018-06-22 08:42:55 -05:00
|
|
|
+ CONSTRAINTS_FIELDS.VIDEOS.EXTNAME.join(', ')
|
|
|
|
),
|
2017-09-15 05:17:08 -05:00
|
|
|
body('name').custom(isVideoNameValid).withMessage('Should have a valid name'),
|
2018-05-11 08:10:13 -05:00
|
|
|
body('channelId')
|
|
|
|
.toInt()
|
2018-06-14 11:06:56 -05:00
|
|
|
.custom(isIdValid).withMessage('Should have correct video channel id'),
|
2017-09-15 05:17:08 -05: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 videosAdd parameters', { parameters: req.body, files: req.files })
|
|
|
|
|
2018-07-31 08:09:34 -05:00
|
|
|
if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
|
|
|
|
if (areErrorsInScheduleUpdate(req, res)) return cleanUpReqFiles(req)
|
2017-11-27 10:30:46 -06:00
|
|
|
|
|
|
|
const videoFile: Express.Multer.File = req.files['videofile'][0]
|
|
|
|
const user = res.locals.oauth.token.User
|
2017-09-15 05:17:08 -05:00
|
|
|
|
2018-07-31 08:09:34 -05:00
|
|
|
if (!await isVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
|
2017-11-27 10:30:46 -06:00
|
|
|
|
|
|
|
const isAble = await user.isAbleToUploadVideo(videoFile)
|
|
|
|
if (isAble === false) {
|
|
|
|
res.status(403)
|
|
|
|
.json({ error: 'The user video quota is exceeded with this video.' })
|
|
|
|
.end()
|
|
|
|
|
2018-07-31 08:09:34 -05:00
|
|
|
return cleanUpReqFiles(req)
|
2017-11-27 10:30:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
let duration: number
|
|
|
|
|
|
|
|
try {
|
|
|
|
duration = await getDurationFromVideoFile(videoFile.path)
|
|
|
|
} catch (err) {
|
2018-03-26 08:54:13 -05:00
|
|
|
logger.error('Invalid input file in videosAddValidator.', { err })
|
2017-11-27 10:30:46 -06:00
|
|
|
res.status(400)
|
|
|
|
.json({ error: 'Invalid input file.' })
|
|
|
|
.end()
|
|
|
|
|
2018-07-31 08:09:34 -05:00
|
|
|
return cleanUpReqFiles(req)
|
2017-11-27 10:30:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
videoFile['duration'] = duration
|
|
|
|
|
|
|
|
return next()
|
2017-09-15 05:17:08 -05:00
|
|
|
}
|
2018-07-16 07:58:22 -05:00
|
|
|
])
|
2017-09-15 05:17:08 -05:00
|
|
|
|
2018-07-16 07:58:22 -05:00
|
|
|
const videosUpdateValidator = getCommonVideoAttributes().concat([
|
2017-10-24 12:41:09 -05:00
|
|
|
param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
|
2018-05-09 04:23:14 -05:00
|
|
|
body('name')
|
|
|
|
.optional()
|
|
|
|
.custom(isVideoNameValid).withMessage('Should have a valid name'),
|
2018-05-11 08:10:13 -05:00
|
|
|
body('channelId')
|
|
|
|
.optional()
|
|
|
|
.toInt()
|
|
|
|
.custom(isIdValid).withMessage('Should have correct video channel id'),
|
2017-09-15 05:17:08 -05: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 videosUpdate parameters', { parameters: req.body })
|
|
|
|
|
2018-07-31 08:09:34 -05:00
|
|
|
if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
|
|
|
|
if (areErrorsInScheduleUpdate(req, res)) return cleanUpReqFiles(req)
|
|
|
|
if (!await isVideoExist(req.params.id, res)) return cleanUpReqFiles(req)
|
2017-11-27 10:30:46 -06:00
|
|
|
|
|
|
|
const video = res.locals.video
|
|
|
|
|
2018-02-22 02:03:45 -06:00
|
|
|
// Check if the user who did the request is able to update the video
|
2018-05-11 08:10:13 -05:00
|
|
|
const user = res.locals.oauth.token.User
|
2018-07-31 08:09:34 -05:00
|
|
|
if (!checkUserCanManageVideo(user, res.locals.video, UserRight.UPDATE_ANY_VIDEO, res)) return cleanUpReqFiles(req)
|
2017-11-27 10:30:46 -06:00
|
|
|
|
|
|
|
if (video.privacy !== VideoPrivacy.PRIVATE && req.body.privacy === VideoPrivacy.PRIVATE) {
|
2018-07-31 08:09:34 -05:00
|
|
|
cleanUpReqFiles(req)
|
2017-11-27 10:30:46 -06:00
|
|
|
return res.status(409)
|
2018-06-15 09:52:15 -05:00
|
|
|
.json({ error: 'Cannot set "private" a video that was not private.' })
|
2017-11-27 10:30:46 -06:00
|
|
|
.end()
|
|
|
|
}
|
|
|
|
|
2018-07-31 08:09:34 -05:00
|
|
|
if (req.body.channelId && !await isVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
|
2018-05-11 08:10:13 -05:00
|
|
|
|
2017-11-27 10:30:46 -06:00
|
|
|
return next()
|
2017-09-15 05:17:08 -05:00
|
|
|
}
|
2018-07-16 07:58:22 -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
|
|
|
|
2018-08-14 02:08:47 -05:00
|
|
|
const video: VideoModel = res.locals.video
|
|
|
|
|
|
|
|
// Video private or blacklisted
|
|
|
|
if (video.privacy === VideoPrivacy.PRIVATE || video.VideoBlacklist) {
|
|
|
|
authenticate(req, res, () => {
|
|
|
|
const user: UserModel = res.locals.oauth.token.User
|
|
|
|
|
|
|
|
// Only the owner or a user that have blacklist rights can see the video
|
|
|
|
if (video.VideoChannel.Account.userId !== user.id && !user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST)) {
|
|
|
|
return res.status(403)
|
|
|
|
.json({ error: 'Cannot get this private or blacklisted video.' })
|
|
|
|
.end()
|
|
|
|
}
|
|
|
|
|
|
|
|
return next()
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2017-10-31 09:20:35 -05:00
|
|
|
|
2018-01-31 07:40:42 -06:00
|
|
|
// Video is public, anyone can access it
|
|
|
|
if (video.privacy === VideoPrivacy.PUBLIC) return next()
|
2017-10-31 09:20:35 -05:00
|
|
|
|
2018-01-31 07:40:42 -06:00
|
|
|
// Video is unlisted, check we used the uuid to fetch it
|
|
|
|
if (video.privacy === VideoPrivacy.UNLISTED) {
|
|
|
|
if (isUUIDValid(req.params.id)) return next()
|
|
|
|
|
|
|
|
// Don't leak this unlisted video
|
|
|
|
return res.status(404).end()
|
|
|
|
}
|
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
|
2018-02-22 02:03:45 -06:00
|
|
|
if (!checkUserCanManageVideo(res.locals.oauth.token.User, res.locals.video, UserRight.REMOVE_ANY_VIDEO, res)) return
|
2017-11-27 10:30:46 -06:00
|
|
|
|
|
|
|
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 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()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2018-07-16 07:58:22 -05:00
|
|
|
function getCommonVideoAttributes () {
|
|
|
|
return [
|
|
|
|
body('thumbnailfile')
|
|
|
|
.custom((value, { req }) => isVideoImage(req.files, 'thumbnailfile')).withMessage(
|
|
|
|
'This thumbnail file is not supported or too large. Please, make sure it is of the following type: '
|
|
|
|
+ CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME.join(', ')
|
|
|
|
),
|
|
|
|
body('previewfile')
|
|
|
|
.custom((value, { req }) => isVideoImage(req.files, 'previewfile')).withMessage(
|
|
|
|
'This preview file is not supported or too large. Please, make sure it is of the following type: '
|
|
|
|
+ CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME.join(', ')
|
|
|
|
),
|
|
|
|
|
|
|
|
body('category')
|
|
|
|
.optional()
|
|
|
|
.customSanitizer(toIntOrNull)
|
|
|
|
.custom(isVideoCategoryValid).withMessage('Should have a valid category'),
|
|
|
|
body('licence')
|
|
|
|
.optional()
|
|
|
|
.customSanitizer(toIntOrNull)
|
|
|
|
.custom(isVideoLicenceValid).withMessage('Should have a valid licence'),
|
|
|
|
body('language')
|
|
|
|
.optional()
|
|
|
|
.customSanitizer(toValueOrNull)
|
|
|
|
.custom(isVideoLanguageValid).withMessage('Should have a valid language'),
|
|
|
|
body('nsfw')
|
|
|
|
.optional()
|
|
|
|
.toBoolean()
|
|
|
|
.custom(isBooleanValid).withMessage('Should have a valid NSFW attribute'),
|
|
|
|
body('waitTranscoding')
|
|
|
|
.optional()
|
|
|
|
.toBoolean()
|
|
|
|
.custom(isBooleanValid).withMessage('Should have a valid wait transcoding attribute'),
|
|
|
|
body('privacy')
|
|
|
|
.optional()
|
|
|
|
.toInt()
|
|
|
|
.custom(isVideoPrivacyValid).withMessage('Should have correct video privacy'),
|
|
|
|
body('description')
|
|
|
|
.optional()
|
|
|
|
.customSanitizer(toValueOrNull)
|
|
|
|
.custom(isVideoDescriptionValid).withMessage('Should have a valid description'),
|
|
|
|
body('support')
|
|
|
|
.optional()
|
|
|
|
.customSanitizer(toValueOrNull)
|
|
|
|
.custom(isVideoSupportValid).withMessage('Should have a valid support text'),
|
|
|
|
body('tags')
|
|
|
|
.optional()
|
|
|
|
.customSanitizer(toValueOrNull)
|
|
|
|
.custom(isVideoTagsValid).withMessage('Should have correct tags'),
|
|
|
|
body('commentsEnabled')
|
|
|
|
.optional()
|
|
|
|
.toBoolean()
|
|
|
|
.custom(isBooleanValid).withMessage('Should have comments enabled boolean'),
|
|
|
|
|
|
|
|
body('scheduleUpdate')
|
|
|
|
.optional()
|
|
|
|
.customSanitizer(toValueOrNull),
|
|
|
|
body('scheduleUpdate.updateAt')
|
|
|
|
.optional()
|
|
|
|
.custom(isDateValid).withMessage('Should have a valid schedule update date'),
|
|
|
|
body('scheduleUpdate.privacy')
|
|
|
|
.optional()
|
|
|
|
.toInt()
|
|
|
|
.custom(isScheduleVideoUpdatePrivacyValid).withMessage('Should have correct schedule update privacy')
|
|
|
|
] as (ValidationChain | express.Handler)[]
|
|
|
|
}
|
2018-08-02 08:34:09 -05:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
videosAddValidator,
|
|
|
|
videosUpdateValidator,
|
|
|
|
videosGetValidator,
|
|
|
|
videosRemoveValidator,
|
|
|
|
videosShareValidator,
|
|
|
|
|
|
|
|
videoRateValidator,
|
|
|
|
|
|
|
|
getCommonVideoAttributes
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
function areErrorsInScheduleUpdate (req: express.Request, res: express.Response) {
|
|
|
|
if (req.body.scheduleUpdate) {
|
|
|
|
if (!req.body.scheduleUpdate.updateAt) {
|
|
|
|
res.status(400)
|
|
|
|
.json({ error: 'Schedule update at is mandatory.' })
|
|
|
|
.end()
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|