]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/video-imports.ts
Add quota used in users list
[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'
990b6a0b 7import { isVideoImportTargetUrlValid, isVideoImportTorrentFile } from '../../helpers/custom-validators/video-imports'
06215f15 8import { cleanUpReqFiles } from '../../helpers/express-utils'
ce33919c 9import { isVideoChannelOfAccountExist, isVideoMagnetUriValid, isVideoNameValid } from '../../helpers/custom-validators/videos'
5d08a6a7 10import { CONFIG } from '../../initializers/constants'
990b6a0b 11import { CONSTRAINTS_FIELDS } from '../../initializers'
fbad87b0
C
12
13const videoImportAddValidator = getCommonVideoAttributes().concat([
fbad87b0
C
14 body('channelId')
15 .toInt()
16 .custom(isIdValid).withMessage('Should have correct video channel id'),
ce33919c
C
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'),
990b6a0b
C
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 ),
fbad87b0
C
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
a84b8fa5 36 const torrentFile = req.files && req.files['torrentfile'] ? req.files['torrentfile'][0] : undefined
fbad87b0
C
37
38 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
5d08a6a7 39
a84b8fa5 40 if (req.body.targetUrl && CONFIG.IMPORT.VIDEOS.HTTP.ENABLED !== true) {
5d08a6a7
C
41 cleanUpReqFiles(req)
42 return res.status(409)
a84b8fa5 43 .json({ error: 'HTTP import is not enabled on this instance.' })
5d08a6a7
C
44 .end()
45 }
46
a84b8fa5
C
47 if (CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED !== true && (req.body.magnetUri || torrentFile)) {
48 cleanUpReqFiles(req)
49 return res.status(409)
50 .json({ error: 'Torrent/magnet URI import is not enabled on this instance.' })
51 .end()
52 }
53
fbad87b0
C
54 if (!await isVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
55
ce33919c 56 // Check we have at least 1 required param
a84b8fa5 57 if (!req.body.targetUrl && !req.body.magnetUri && !torrentFile) {
ce33919c
C
58 cleanUpReqFiles(req)
59
60 return res.status(400)
990b6a0b 61 .json({ error: 'Should have a magnetUri or a targetUrl or a torrent file.' })
ce33919c
C
62 .end()
63 }
64
fbad87b0
C
65 return next()
66 }
67])
68
fbad87b0
C
69// ---------------------------------------------------------------------------
70
71export {
516df59b 72 videoImportAddValidator
fbad87b0
C
73}
74
75// ---------------------------------------------------------------------------