1 import * as express from 'express'
2 import { body } 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 } from '../../helpers/custom-validators/video-imports'
8 import { cleanUpReqFiles } from '../../helpers/utils'
9 import { isVideoChannelOfAccountExist, isVideoMagnetUriValid, isVideoNameValid } from '../../helpers/custom-validators/videos'
10 import { CONFIG } from '../../initializers/constants'
12 const videoImportAddValidator = getCommonVideoAttributes().concat([
15 .custom(isIdValid).withMessage('Should have correct video channel id'),
18 .custom(isVideoImportTargetUrlValid).withMessage('Should have a valid video import target URL'),
21 .custom(isVideoMagnetUriValid).withMessage('Should have a valid video magnet URI'),
24 .custom(isVideoNameValid).withMessage('Should have a valid name'),
26 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
27 logger.debug('Checking videoImportAddValidator parameters', { parameters: req.body })
29 const user = res.locals.oauth.token.User
31 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
33 if (CONFIG.IMPORT.VIDEOS.HTTP.ENABLED !== true) {
35 return res.status(409)
36 .json({ error: 'Import is not enabled on this instance.' })
40 if (!await isVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
42 // Check we have at least 1 required param
43 if (!req.body.targetUrl && !req.body.magnetUri) {
46 return res.status(400)
47 .json({ error: 'Should have a magnetUri or a targetUrl.' })
55 // ---------------------------------------------------------------------------
58 videoImportAddValidator
61 // ---------------------------------------------------------------------------