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