ActivityPub-federated video streaming platform using P2P directly in your web browser
Add a check for the duration of videos
Chocobozzz67100f1f971d
diff --git a/server/controllers/api/v1/videos.js b/server/controllers/api/v1/videos.jsindex 231309c5e2..c6dbfbaf27 100644--- a/server/controllers/api/v1/videos.js+++ b/server/controllers/api/v1/videos.js@@ -83,23 +83,11 @@ function addVideo (req, res, next) {const videoInfos = req.bodyasync.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.createdDatefs.readFile(thumbnailsDir + thumbnailName, function (err, thumbnailData) {diff --git a/server/helpers/customValidators.js b/server/helpers/customValidators.jsindex a8fc6942d6..9b982369ee 100644--- a/server/helpers/customValidators.js+++ b/server/helpers/customValidators.js@@ -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)})}diff --git a/server/initializers/constants.js b/server/initializers/constants.jsindex 48e660fe26..d87a376d32 100644--- a/server/initializers/constants.js+++ b/server/initializers/constants.js@@ -9,6 +9,9 @@ let FRIEND_BASE_SCORE = 100// Time to wait between requests to the friendslet 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 paginationconst PAGINATION_COUNT_DEFAULT = 15@@ -31,6 +34,7 @@ const THUMBNAILS_STATIC_PATH = '/static/thumbnails'if (isTestInstance() === true) {FRIEND_BASE_SCORE = 20INTERVAL = 10000+ MAXIMUM_VIDEO_DURATION = 14REQUEST_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,diff --git a/server/middlewares/reqValidators/videos.js b/server/middlewares/reqValidators/videos.jsindex c206604526..6e6e75fb31 100644--- a/server/middlewares/reqValidators/videos.js+++ b/server/middlewares/reqValidators/videos.js@@ -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) {diff --git a/server/tests/api/checkParams.js b/server/tests/api/checkParams.jsindex 1f47e5ef46..b63091910e 100644--- a/server/tests/api/checkParams.js+++ b/server/tests/api/checkParams.js@@ -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 = 204const 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 errexpect(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.webmnew file mode 100644index 0000000000..8286f74b0aBinary files /dev/null and b/server/tests/api/fixtures/video_too_long.webm differ