]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/video-imports.ts
Import magnets with webtorrent
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / video-imports.ts
CommitLineData
fbad87b0 1import * as express from 'express'
516df59b 2import { body } from 'express-validator/check'
fbad87b0
C
3import { isIdValid } from '../../helpers/custom-validators/misc'
4import { logger } from '../../helpers/logger'
5import { areValidationErrors } from './utils'
6import { getCommonVideoAttributes } from './videos'
516df59b 7import { isVideoImportTargetUrlValid } from '../../helpers/custom-validators/video-imports'
fbad87b0 8import { cleanUpReqFiles } from '../../helpers/utils'
ce33919c 9import { isVideoChannelOfAccountExist, isVideoMagnetUriValid, isVideoNameValid } from '../../helpers/custom-validators/videos'
5d08a6a7 10import { CONFIG } from '../../initializers/constants'
fbad87b0
C
11
12const videoImportAddValidator = getCommonVideoAttributes().concat([
fbad87b0
C
13 body('channelId')
14 .toInt()
15 .custom(isIdValid).withMessage('Should have correct video channel id'),
ce33919c
C
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'),
fbad87b0
C
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)
5d08a6a7
C
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
fbad87b0
C
40 if (!await isVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
41
ce33919c
C
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
fbad87b0
C
51 return next()
52 }
53])
54
fbad87b0
C
55// ---------------------------------------------------------------------------
56
57export {
516df59b 58 videoImportAddValidator
fbad87b0
C
59}
60
61// ---------------------------------------------------------------------------