]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/video-imports.ts
8ec9373fb71e3a897b35741a299316e8745c321f
[github/Chocobozzz/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, isVideoMagnetUriValid, isVideoNameValid } from '../../helpers/custom-validators/videos'
10 import { CONFIG } from '../../initializers/constants'
11
12 const videoImportAddValidator = getCommonVideoAttributes().concat([
13 body('channelId')
14 .toInt()
15 .custom(isIdValid).withMessage('Should have correct video channel id'),
16 body('targetUrl')
17 .optional()
18 .custom(isVideoImportTargetUrlValid).withMessage('Should have a valid video import target URL'),
19 body('magnetUri')
20 .optional()
21 .custom(isVideoMagnetUriValid).withMessage('Should have a valid video magnet URI'),
22 body('name')
23 .optional()
24 .custom(isVideoNameValid).withMessage('Should have a valid name'),
25
26 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
27 logger.debug('Checking videoImportAddValidator parameters', { parameters: req.body })
28
29 const user = res.locals.oauth.token.User
30
31 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
32
33 if (CONFIG.IMPORT.VIDEOS.HTTP.ENABLED !== true) {
34 cleanUpReqFiles(req)
35 return res.status(409)
36 .json({ error: 'Import is not enabled on this instance.' })
37 .end()
38 }
39
40 if (!await isVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
41
42 // Check we have at least 1 required param
43 if (!req.body.targetUrl && !req.body.magnetUri) {
44 cleanUpReqFiles(req)
45
46 return res.status(400)
47 .json({ error: 'Should have a magnetUri or a targetUrl.' })
48 .end()
49 }
50
51 return next()
52 }
53 ])
54
55 // ---------------------------------------------------------------------------
56
57 export {
58 videoImportAddValidator
59 }
60
61 // ---------------------------------------------------------------------------