Add import http enabled configuration
[oweals/peertube.git] / server / middlewares / validators / video-imports.ts
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, isVideoNameValid } from '../../helpers/custom-validators/videos'
10 import { CONFIG } from '../../initializers/constants'
11
12 const videoImportAddValidator = getCommonVideoAttributes().concat([
13   body('targetUrl').custom(isVideoImportTargetUrlValid).withMessage('Should have a valid video import target URL'),
14   body('channelId')
15     .toInt()
16     .custom(isIdValid).withMessage('Should have correct video channel id'),
17   body('name')
18     .optional()
19     .custom(isVideoNameValid).withMessage('Should have a valid name'),
20
21   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
22     logger.debug('Checking videoImportAddValidator parameters', { parameters: req.body })
23
24     const user = res.locals.oauth.token.User
25
26     if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
27
28     if (CONFIG.IMPORT.VIDEOS.HTTP.ENABLED !== true) {
29       cleanUpReqFiles(req)
30       return res.status(409)
31         .json({ error: 'Import is not enabled on this instance.' })
32         .end()
33     }
34
35     if (!await isVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
36
37     return next()
38   }
39 ])
40
41 // ---------------------------------------------------------------------------
42
43 export {
44   videoImportAddValidator
45 }
46
47 // ---------------------------------------------------------------------------