X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmiddlewares%2Fvalidators%2Fvideo-imports.ts;h=b2063b8dac1a47561f543743b8ed5e38c27800c3;hb=d9eaee3939bf2e93e5d775d32bce77842201faba;hp=8ec9373fb71e3a897b35741a299316e8745c321f;hpb=ce33919c24e7402d92d81f3cd8e545df52d98240;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/middlewares/validators/video-imports.ts b/server/middlewares/validators/video-imports.ts index 8ec9373fb..b2063b8da 100644 --- a/server/middlewares/validators/video-imports.ts +++ b/server/middlewares/validators/video-imports.ts @@ -4,10 +4,11 @@ import { isIdValid } from '../../helpers/custom-validators/misc' import { logger } from '../../helpers/logger' import { areValidationErrors } from './utils' import { getCommonVideoAttributes } from './videos' -import { isVideoImportTargetUrlValid } from '../../helpers/custom-validators/video-imports' -import { cleanUpReqFiles } from '../../helpers/utils' +import { isVideoImportTargetUrlValid, isVideoImportTorrentFile } from '../../helpers/custom-validators/video-imports' +import { cleanUpReqFiles } from '../../helpers/express-utils' import { isVideoChannelOfAccountExist, isVideoMagnetUriValid, isVideoNameValid } from '../../helpers/custom-validators/videos' import { CONFIG } from '../../initializers/constants' +import { CONSTRAINTS_FIELDS } from '../../initializers' const videoImportAddValidator = getCommonVideoAttributes().concat([ body('channelId') @@ -19,6 +20,11 @@ const videoImportAddValidator = getCommonVideoAttributes().concat([ body('magnetUri') .optional() .custom(isVideoMagnetUriValid).withMessage('Should have a valid video magnet URI'), + body('torrentfile') + .custom((value, { req }) => isVideoImportTorrentFile(req.files)).withMessage( + 'This torrent file is not supported or too large. Please, make sure it is of the following type: ' + + CONSTRAINTS_FIELDS.VIDEO_IMPORTS.TORRENT_FILE.EXTNAME.join(', ') + ), body('name') .optional() .custom(isVideoNameValid).withMessage('Should have a valid name'), @@ -27,24 +33,32 @@ const videoImportAddValidator = getCommonVideoAttributes().concat([ logger.debug('Checking videoImportAddValidator parameters', { parameters: req.body }) const user = res.locals.oauth.token.User + const torrentFile = req.files && req.files['torrentfile'] ? req.files['torrentfile'][0] : undefined if (areValidationErrors(req, res)) return cleanUpReqFiles(req) - if (CONFIG.IMPORT.VIDEOS.HTTP.ENABLED !== true) { + if (req.body.targetUrl && CONFIG.IMPORT.VIDEOS.HTTP.ENABLED !== true) { cleanUpReqFiles(req) return res.status(409) - .json({ error: 'Import is not enabled on this instance.' }) + .json({ error: 'HTTP import is not enabled on this instance.' }) .end() } + if (CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED !== true && (req.body.magnetUri || torrentFile)) { + cleanUpReqFiles(req) + return res.status(409) + .json({ error: 'Torrent/magnet URI import is not enabled on this instance.' }) + .end() + } + if (!await isVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req) // Check we have at least 1 required param - if (!req.body.targetUrl && !req.body.magnetUri) { + if (!req.body.targetUrl && !req.body.magnetUri && !torrentFile) { cleanUpReqFiles(req) return res.status(400) - .json({ error: 'Should have a magnetUri or a targetUrl.' }) + .json({ error: 'Should have a magnetUri or a targetUrl or a torrent file.' }) .end() }