Add ability to import video with youtube-dl
[oweals/peertube.git] / server / middlewares / validators / video-imports.ts
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 } from '../../helpers/custom-validators/video-imports'
8 import { cleanUpReqFiles } from '../../helpers/utils'
9 import { isVideoChannelOfAccountExist, isVideoNameValid } from '../../helpers/custom-validators/videos'
10
11 const videoImportAddValidator = getCommonVideoAttributes().concat([
12   body('targetUrl').custom(isVideoImportTargetUrlValid).withMessage('Should have a valid video import target URL'),
13   body('channelId')
14     .toInt()
15     .custom(isIdValid).withMessage('Should have correct video channel id'),
16   body('name')
17     .optional()
18     .custom(isVideoNameValid).withMessage('Should have a valid name'),
19
20   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
21     logger.debug('Checking videoImportAddValidator parameters', { parameters: req.body })
22
23     const user = res.locals.oauth.token.User
24
25     if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
26     if (!await isVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
27
28     return next()
29   }
30 ])
31
32 const videoImportDeleteValidator = [
33   param('id').custom(isIdValid).not().isEmpty().withMessage('Should have a valid id'),
34
35   (req: express.Request, res: express.Response, next: express.NextFunction) => {
36     logger.debug('Checking videoImportDeleteValidator parameters', { parameters: req.body })
37
38     if (areValidationErrors(req, res)) return
39
40     return next()
41   }
42 ]
43
44 // ---------------------------------------------------------------------------
45
46 export {
47   videoImportAddValidator,
48   videoImportDeleteValidator
49 }
50
51 // ---------------------------------------------------------------------------