1 import * as express from 'express'
2 import { body, param } from 'express-validator/check'
3 import { isIdValid } from '../../helpers/custom-validators/misc'
4 import { logger } from '../../helpers/logger'
5 import { areValidationErrors } from './utils'
6 import { getCommonVideoAttributes } from './videos'
7 import { isVideoImportTargetUrlValid, isVideoImportExist } from '../../helpers/custom-validators/video-imports'
8 import { cleanUpReqFiles } from '../../helpers/utils'
9 import { isVideoChannelOfAccountExist, isVideoNameValid, checkUserCanManageVideo } from '../../helpers/custom-validators/videos'
10 import { VideoImportModel } from '../../models/video/video-import'
11 import { UserRight } from '../../../shared'
13 const videoImportAddValidator = getCommonVideoAttributes().concat([
14 body('targetUrl').custom(isVideoImportTargetUrlValid).withMessage('Should have a valid video import target URL'),
17 .custom(isIdValid).withMessage('Should have correct video channel id'),
20 .custom(isVideoNameValid).withMessage('Should have a valid name'),
22 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
23 logger.debug('Checking videoImportAddValidator parameters', { parameters: req.body })
25 const user = res.locals.oauth.token.User
27 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
28 if (!await isVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
34 const videoImportDeleteValidator = [
35 param('id').custom(isIdValid).not().isEmpty().withMessage('Should have a valid id'),
37 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
38 logger.debug('Checking videoImportDeleteValidator parameters', { parameters: req.body })
40 if (areValidationErrors(req, res)) return
41 if (!await isVideoImportExist(req.params.id, res)) return
43 const user = res.locals.oauth.token.User
44 const videoImport: VideoImportModel = res.locals.videoImport
46 if (!await checkUserCanManageVideo(user, videoImport.Video, UserRight.UPDATE_ANY_VIDEO, res)) return
52 // ---------------------------------------------------------------------------
55 videoImportAddValidator,
56 videoImportDeleteValidator
59 // ---------------------------------------------------------------------------