1 import * as express from 'express'
2 import { body } from 'express-validator'
3 import { isIdValid, toIntOrNull } from '../../../helpers/custom-validators/misc'
4 import { logger } from '../../../helpers/logger'
5 import { areValidationErrors } from '../utils'
6 import { getCommonVideoEditAttributes } from './videos'
7 import { isVideoImportTargetUrlValid, isVideoImportTorrentFile } from '../../../helpers/custom-validators/video-imports'
8 import { cleanUpReqFiles } from '../../../helpers/express-utils'
9 import { isVideoMagnetUriValid, isVideoNameValid } from '../../../helpers/custom-validators/videos'
10 import { CONFIG } from '../../../initializers/config'
11 import { CONSTRAINTS_FIELDS } from '../../../initializers/constants'
12 import { doesVideoChannelOfAccountExist } from '../../../helpers/middlewares'
14 const videoImportAddValidator = getCommonVideoEditAttributes().concat([
16 .customSanitizer(toIntOrNull)
17 .custom(isIdValid).withMessage('Should have correct video channel id'),
20 .custom(isVideoImportTargetUrlValid).withMessage('Should have a valid video import target URL'),
23 .custom(isVideoMagnetUriValid).withMessage('Should have a valid video magnet URI'),
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(', ')
31 .custom(isVideoNameValid).withMessage('Should have a valid name'),
33 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
34 logger.debug('Checking videoImportAddValidator parameters', { parameters: req.body })
36 const user = res.locals.oauth.token.User
37 const torrentFile = req.files && req.files['torrentfile'] ? req.files['torrentfile'][0] : undefined
39 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
41 if (req.body.targetUrl && CONFIG.IMPORT.VIDEOS.HTTP.ENABLED !== true) {
43 return res.status(409)
44 .json({ error: 'HTTP import is not enabled on this instance.' })
48 if (CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED !== true && (req.body.magnetUri || torrentFile)) {
50 return res.status(409)
51 .json({ error: 'Torrent/magnet URI import is not enabled on this instance.' })
55 if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
57 // Check we have at least 1 required param
58 if (!req.body.targetUrl && !req.body.magnetUri && !torrentFile) {
61 return res.status(400)
62 .json({ error: 'Should have a magnetUri or a targetUrl or a torrent file.' })
70 // ---------------------------------------------------------------------------
73 videoImportAddValidator
76 // ---------------------------------------------------------------------------