X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmiddlewares%2Fvalidators%2Fvideos%2Fvideo-imports.ts;h=d0643ff26e8935648913643ef5c0b5a9330a25f7;hb=8f608a4cb22ab232cfab20665050764b38bac9c7;hp=48d20f904c8228d5462f8a502811f621c286e54c;hpb=be0f59b4eec3c2c4dcd151e2b174be39dff1568e;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/middlewares/validators/videos/video-imports.ts b/server/middlewares/validators/videos/video-imports.ts index 48d20f904..d0643ff26 100644 --- a/server/middlewares/validators/videos/video-imports.ts +++ b/server/middlewares/validators/videos/video-imports.ts @@ -1,18 +1,23 @@ import * as express from 'express' -import { body } from 'express-validator/check' -import { isIdValid } from '../../../helpers/custom-validators/misc' -import { logger } from '../../../helpers/logger' -import { areValidationErrors } from '../utils' -import { getCommonVideoAttributes } from './videos' +import { body } from 'express-validator' +import { isPreImportVideoAccepted } from '@server/lib/moderation' +import { Hooks } from '@server/lib/plugins/hooks' +import { VideoImportCreate } from '@shared/models/videos/import/video-import-create.model' +import { isIdValid, toIntOrNull } from '../../../helpers/custom-validators/misc' import { isVideoImportTargetUrlValid, isVideoImportTorrentFile } from '../../../helpers/custom-validators/video-imports' +import { isVideoMagnetUriValid, isVideoNameValid } from '../../../helpers/custom-validators/videos' 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' +import { logger } from '../../../helpers/logger' +import { doesVideoChannelOfAccountExist } from '../../../helpers/middlewares' +import { CONFIG } from '../../../initializers/config' +import { CONSTRAINTS_FIELDS } from '../../../initializers/constants' +import { areValidationErrors } from '../utils' +import { getCommonVideoEditAttributes } from './videos' +import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' -const videoImportAddValidator = getCommonVideoAttributes().concat([ +const videoImportAddValidator = getCommonVideoEditAttributes().concat([ body('channelId') - .toInt() + .customSanitizer(toIntOrNull) .custom(isIdValid).withMessage('Should have correct video channel id'), body('targetUrl') .optional() @@ -21,10 +26,11 @@ const videoImportAddValidator = getCommonVideoAttributes().concat([ .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(', ') - ), + .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'), @@ -33,35 +39,34 @@ 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 + const torrentFile = req.files?.['torrentfile'] ? req.files['torrentfile'][0] : undefined if (areValidationErrors(req, res)) return cleanUpReqFiles(req) - if (req.body.targetUrl && CONFIG.IMPORT.VIDEOS.HTTP.ENABLED !== true) { + if (CONFIG.IMPORT.VIDEOS.HTTP.ENABLED !== true && req.body.targetUrl) { cleanUpReqFiles(req) - return res.status(409) + return res.status(HttpStatusCode.CONFLICT_409) .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) + return res.status(HttpStatusCode.CONFLICT_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) + if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req) // Check we have at least 1 required param if (!req.body.targetUrl && !req.body.magnetUri && !torrentFile) { cleanUpReqFiles(req) - return res.status(400) + return res.status(HttpStatusCode.BAD_REQUEST_400) .json({ error: 'Should have a magnetUri or a targetUrl or a torrent file.' }) - .end() } + if (!await isImportAccepted(req, res)) return cleanUpReqFiles(req) + return next() } ]) @@ -73,3 +78,31 @@ export { } // --------------------------------------------------------------------------- + +async function isImportAccepted (req: express.Request, res: express.Response) { + const body: VideoImportCreate = req.body + const hookName = body.targetUrl + ? 'filter:api.video.pre-import-url.accept.result' + : 'filter:api.video.pre-import-torrent.accept.result' + + // Check we accept this video + const acceptParameters = { + videoImportBody: body, + user: res.locals.oauth.token.User + } + const acceptedResult = await Hooks.wrapFun( + isPreImportVideoAccepted, + acceptParameters, + hookName + ) + + if (!acceptedResult || acceptedResult.accepted !== true) { + logger.info('Refused to import video.', { acceptedResult, acceptParameters }) + res.status(HttpStatusCode.FORBIDDEN_403) + .json({ error: acceptedResult.errorMessage || 'Refused to import video' }) + + return false + } + + return true +}