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