]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Add a check for the duration of videos
authorChocobozzz <florian.bigard@gmail.com>
Mon, 16 May 2016 17:49:10 +0000 (19:49 +0200)
committerChocobozzz <florian.bigard@gmail.com>
Mon, 16 May 2016 17:49:10 +0000 (19:49 +0200)
server/controllers/api/v1/videos.js
server/helpers/customValidators.js
server/initializers/constants.js
server/middlewares/reqValidators/videos.js
server/tests/api/checkParams.js
server/tests/api/fixtures/video_too_long.webm [new file with mode: 0644]

index 231309c5e2014adcef9e8d3066baa5295972ec0f..c6dbfbaf27d6305337f95d8501327cdca88544fa 100644 (file)
@@ -83,23 +83,11 @@ function addVideo (req, res, next) {
   const videoInfos = req.body
 
   async.waterfall([
-    function (callback) {
+    function seedTheVideo (callback) {
       videos.seed(videoFile.path, callback)
     },
 
-    function seed (torrent, callback) {
-      videos.getVideoDuration(videoFile.path, function (err, duration) {
-        if (err) {
-          // TODO: unseed the video
-          logger.error('Cannot retrieve metadata of the file.')
-          return next(err)
-        }
-
-        callback(null, torrent, duration)
-      })
-    },
-
-    function createThumbnail (torrent, duration, callback) {
+    function createThumbnail (torrent, callback) {
       videos.createVideoThumbnail(videoFile.path, function (err, thumbnailName) {
         if (err) {
           // TODO: unseed the video
@@ -107,18 +95,18 @@ function addVideo (req, res, next) {
           return callback(err)
         }
 
-        callback(null, torrent, duration, thumbnailName)
+        callback(null, torrent, thumbnailName)
       })
     },
 
-    function insertIntoDB (torrent, duration, thumbnailName, callback) {
+    function insertIntoDB (torrent, thumbnailName, callback) {
       const videoData = {
         name: videoInfos.name,
         namePath: videoFile.filename,
         description: videoInfos.description,
         magnetUri: torrent.magnetURI,
         author: res.locals.oauth.token.user.username,
-        duration: duration,
+        duration: videoFile.duration,
         thumbnail: thumbnailName
       }
 
@@ -130,11 +118,11 @@ function addVideo (req, res, next) {
           return callback(err)
         }
 
-        return callback(null, torrent, duration, thumbnailName, videoData, insertedVideo)
+        return callback(null, torrent, thumbnailName, videoData, insertedVideo)
       })
     },
 
-    function getThumbnailBase64 (torrent, duration, thumbnailName, videoData, insertedVideo, callback) {
+    function getThumbnailBase64 (torrent, thumbnailName, videoData, insertedVideo, callback) {
       videoData.createdDate = insertedVideo.createdDate
 
       fs.readFile(thumbnailsDir + thumbnailName, function (err, thumbnailData) {
index a8fc6942d6868555e6d5419f841c890c593960ce..9b982369ee9129dc4d4014f8eacd471fc2231fe0 100644 (file)
@@ -2,6 +2,8 @@
 
 const validator = require('validator')
 
+const constants = require('../initializers/constants')
+
 const customValidators = {
   eachIsRemoteVideosAddValid: eachIsRemoteVideosAddValid,
   eachIsRemoteVideosRemoveValid: eachIsRemoteVideosRemoveValid,
@@ -15,6 +17,8 @@ function eachIsRemoteVideosAddValid (values) {
       validator.isLength(val.magnetUri, 10) &&
       validator.isURL(val.podUrl) &&
       !isNaN(val.duration) &&
+      val.duration >= 0 &&
+      val.duration < constants.MAXIMUM_VIDEO_DURATION &&
       validator.isDate(val.createdDate)
   })
 }
index 48e660fe2667ca4926b05cf473d85fb2a9a68146..d87a376d321691ee4703fcfa678c47f94b6bba6e 100644 (file)
@@ -9,6 +9,9 @@ let FRIEND_BASE_SCORE = 100
 // Time to wait between requests to the friends
 let INTERVAL = 60000
 
+// 2 hours maximum for the duration of a video (in seconds)
+let MAXIMUM_VIDEO_DURATION = 7200
+
 // Number of results by default for the pagination
 const PAGINATION_COUNT_DEFAULT = 15
 
@@ -31,6 +34,7 @@ const THUMBNAILS_STATIC_PATH = '/static/thumbnails'
 if (isTestInstance() === true) {
   FRIEND_BASE_SCORE = 20
   INTERVAL = 10000
+  MAXIMUM_VIDEO_DURATION = 14
   REQUEST_RETRIES = 2
 }
 
@@ -40,6 +44,7 @@ module.exports = {
   API_VERSION: API_VERSION,
   FRIEND_BASE_SCORE: FRIEND_BASE_SCORE,
   INTERVAL: INTERVAL,
+  MAXIMUM_VIDEO_DURATION: MAXIMUM_VIDEO_DURATION,
   PAGINATION_COUNT_DEFAULT: PAGINATION_COUNT_DEFAULT,
   PODS_SCORE: PODS_SCORE,
   REQUEST_RETRIES: REQUEST_RETRIES,
index c20660452639645667c878603442cd6948de6ce0..6e6e75fb319ae42ade94f9e858df5d671cc9f850 100644 (file)
@@ -1,6 +1,7 @@
 'use strict'
 
 const checkErrors = require('./utils').checkErrors
+const constants = require('../../initializers/constants')
 const logger = require('../../helpers/logger')
 const videos = require('../../lib/videos')
 const Videos = require('../../models/videos')
@@ -20,7 +21,22 @@ function videosAdd (req, res, next) {
 
   logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
 
-  checkErrors(req, res, next)
+  checkErrors(req, res, function () {
+    const videoFile = req.files.videofile[0]
+
+    videos.getVideoDuration(videoFile.path, function (err, duration) {
+      if (err) {
+        return res.status(400).send('Cannot retrieve metadata of the file.')
+      }
+
+      if (duration > constants.MAXIMUM_VIDEO_DURATION) {
+        return res.status(400).send('Duration of the video file is too big.')
+      }
+
+      videoFile.duration = duration
+      next()
+    })
+  })
 }
 
 function videosGet (req, res, next) {
index 1f47e5ef46bfd62d4e6eed46de44e0a6d0aba603..b63091910e37a50e38801b28236c2e4fe4b2a010 100644 (file)
@@ -11,9 +11,9 @@ const utils = require('./utils')
 describe('Test parameters validator', function () {
   let server = null
 
-  function makePostRequest (path, token, fields, attach, done, fail) {
+  function makePostRequest (path, token, fields, attaches, done, fail) {
     let statusCode = 400
-    if (fail !== undefined && fail === false) statusCode = 200
+    if (fail !== undefined && fail === false) statusCode = 204
 
     const req = request(server.url)
       .post(path)
@@ -26,6 +26,11 @@ describe('Test parameters validator', function () {
       req.field(field, value)
     })
 
+    Object.keys(attaches).forEach(function (attach) {
+      const value = attaches[attach]
+      req.attach(attach, value)
+    })
+
     req.expect(statusCode, done)
   }
 
@@ -200,7 +205,18 @@ describe('Test parameters validator', function () {
           description: 'my super description'
         }
         const attach = {
-          'videofile': pathUtils.join(__dirname, '..', 'fixtures', 'video_short_fake.webm')
+          'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short_fake.webm')
+        }
+        makePostRequest(path, server.accessToken, data, attach, done)
+      })
+
+      it('Should fail with a too big duration', function (done) {
+        const data = {
+          name: 'my super name',
+          description: 'my super description'
+        }
+        const attach = {
+          'videofile': pathUtils.join(__dirname, 'fixtures', 'video_too_long.webm')
         }
         makePostRequest(path, server.accessToken, data, attach, done)
       })
@@ -217,9 +233,9 @@ describe('Test parameters validator', function () {
           attach.videofile = pathUtils.join(__dirname, 'fixtures', 'video_short.mp4')
           makePostRequest(path, server.accessToken, data, attach, function () {
             attach.videofile = pathUtils.join(__dirname, 'fixtures', 'video_short.ogv')
-            makePostRequest(path, server.accessToken, data, attach, done, true)
-          }, true)
-        }, true)
+            makePostRequest(path, server.accessToken, data, attach, done, false)
+          }, false)
+        }, false)
       })
     })
 
@@ -234,7 +250,7 @@ describe('Test parameters validator', function () {
             if (err) throw err
 
             expect(res.body).to.be.an('array')
-            expect(res.body.length).to.equal(0)
+            expect(res.body.length).to.equal(3)
 
             done()
           })
diff --git a/server/tests/api/fixtures/video_too_long.webm b/server/tests/api/fixtures/video_too_long.webm
new file mode 100644 (file)
index 0000000..8286f74
Binary files /dev/null and b/server/tests/api/fixtures/video_too_long.webm differ