]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/middlewares/validators/video-imports.ts
add user account email verificiation (#977)
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / video-imports.ts
index 0dedcf803cdf4fa7c0e342ab60c7b88a163e6e4c..b2063b8dac1a47561f543743b8ed5e38c27800c3 100644 (file)
@@ -1,20 +1,30 @@
 import * as express from 'express'
-import { body, param } from 'express-validator/check'
+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 { isVideoImportTargetUrlValid, isVideoImportExist } from '../../helpers/custom-validators/video-imports'
-import { cleanUpReqFiles } from '../../helpers/utils'
-import { isVideoChannelOfAccountExist, isVideoNameValid, checkUserCanManageVideo } from '../../helpers/custom-validators/videos'
-import { VideoImportModel } from '../../models/video/video-import'
-import { UserRight } from '../../../shared'
+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('targetUrl').custom(isVideoImportTargetUrlValid).withMessage('Should have a valid video import target URL'),
   body('channelId')
     .toInt()
     .custom(isIdValid).withMessage('Should have correct video channel id'),
+  body('targetUrl')
+    .optional()
+    .custom(isVideoImportTargetUrlValid).withMessage('Should have a valid video import target URL'),
+  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'),
@@ -23,37 +33,43 @@ 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 (!await isVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
 
-    return next()
-  }
-])
+    if (req.body.targetUrl && CONFIG.IMPORT.VIDEOS.HTTP.ENABLED !== true) {
+      cleanUpReqFiles(req)
+      return res.status(409)
+        .json({ error: 'HTTP import is not enabled on this instance.' })
+        .end()
+    }
 
-const videoImportDeleteValidator = [
-  param('id').custom(isIdValid).not().isEmpty().withMessage('Should have a valid id'),
+    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()
+    }
 
-  async (req: express.Request, res: express.Response, next: express.NextFunction) => {
-    logger.debug('Checking videoImportDeleteValidator parameters', { parameters: req.body })
+    if (!await isVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
 
-    if (areValidationErrors(req, res)) return
-    if (!await isVideoImportExist(req.params.id, res)) return
+    // Check we have at least 1 required param
+    if (!req.body.targetUrl && !req.body.magnetUri && !torrentFile) {
+      cleanUpReqFiles(req)
 
-    const user = res.locals.oauth.token.User
-    const videoImport: VideoImportModel = res.locals.videoImport
-
-    if (!await checkUserCanManageVideo(user, videoImport.Video, UserRight.UPDATE_ANY_VIDEO, res)) return
+      return res.status(400)
+        .json({ error: 'Should have a magnetUri or a targetUrl or a torrent file.' })
+        .end()
+    }
 
     return next()
   }
-]
+])
 
 // ---------------------------------------------------------------------------
 
 export {
-  videoImportAddValidator,
-  videoImportDeleteValidator
+  videoImportAddValidator
 }
 
 // ---------------------------------------------------------------------------