]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/videos/video-imports.ts
Refactor middleware helpers
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-imports.ts
CommitLineData
fbad87b0 1import * as express from 'express'
516df59b 2import { body } from 'express-validator/check'
6e46de09
C
3import { isIdValid } from '../../../helpers/custom-validators/misc'
4import { logger } from '../../../helpers/logger'
5import { areValidationErrors } from '../utils'
418d092a 6import { getCommonVideoEditAttributes } from './videos'
6e46de09
C
7import { isVideoImportTargetUrlValid, isVideoImportTorrentFile } from '../../../helpers/custom-validators/video-imports'
8import { cleanUpReqFiles } from '../../../helpers/express-utils'
3e753302 9import { isVideoMagnetUriValid, isVideoNameValid } from '../../../helpers/custom-validators/videos'
6dd9de95 10import { CONFIG } from '../../../initializers/config'
74dc3bca 11import { CONSTRAINTS_FIELDS } from '../../../initializers/constants'
3e753302 12import { doesVideoChannelOfAccountExist } from '../../../helpers/middlewares'
fbad87b0 13
418d092a 14const videoImportAddValidator = getCommonVideoEditAttributes().concat([
fbad87b0
C
15 body('channelId')
16 .toInt()
17 .custom(isIdValid).withMessage('Should have correct video channel id'),
ce33919c
C
18 body('targetUrl')
19 .optional()
20 .custom(isVideoImportTargetUrlValid).withMessage('Should have a valid video import target URL'),
21 body('magnetUri')
22 .optional()
23 .custom(isVideoMagnetUriValid).withMessage('Should have a valid video magnet URI'),
990b6a0b
C
24 body('torrentfile')
25 .custom((value, { req }) => isVideoImportTorrentFile(req.files)).withMessage(
26 'This torrent file is not supported or too large. Please, make sure it is of the following type: '
27 + CONSTRAINTS_FIELDS.VIDEO_IMPORTS.TORRENT_FILE.EXTNAME.join(', ')
28 ),
fbad87b0
C
29 body('name')
30 .optional()
31 .custom(isVideoNameValid).withMessage('Should have a valid name'),
32
33 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
34 logger.debug('Checking videoImportAddValidator parameters', { parameters: req.body })
35
36 const user = res.locals.oauth.token.User
a84b8fa5 37 const torrentFile = req.files && req.files['torrentfile'] ? req.files['torrentfile'][0] : undefined
fbad87b0
C
38
39 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
5d08a6a7 40
a84b8fa5 41 if (req.body.targetUrl && CONFIG.IMPORT.VIDEOS.HTTP.ENABLED !== true) {
5d08a6a7
C
42 cleanUpReqFiles(req)
43 return res.status(409)
a84b8fa5 44 .json({ error: 'HTTP import is not enabled on this instance.' })
5d08a6a7
C
45 .end()
46 }
47
a84b8fa5
C
48 if (CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED !== true && (req.body.magnetUri || torrentFile)) {
49 cleanUpReqFiles(req)
50 return res.status(409)
51 .json({ error: 'Torrent/magnet URI import is not enabled on this instance.' })
52 .end()
53 }
54
0f6acda1 55 if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
fbad87b0 56
ce33919c 57 // Check we have at least 1 required param
a84b8fa5 58 if (!req.body.targetUrl && !req.body.magnetUri && !torrentFile) {
ce33919c
C
59 cleanUpReqFiles(req)
60
61 return res.status(400)
990b6a0b 62 .json({ error: 'Should have a magnetUri or a targetUrl or a torrent file.' })
ce33919c
C
63 .end()
64 }
65
fbad87b0
C
66 return next()
67 }
68])
69
fbad87b0
C
70// ---------------------------------------------------------------------------
71
72export {
516df59b 73 videoImportAddValidator
fbad87b0
C
74}
75
76// ---------------------------------------------------------------------------