diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-05-16 19:49:10 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-05-16 19:49:10 +0200 |
commit | 67100f1f971dd10a466a321899b56c0813e08d31 (patch) | |
tree | 3253db23a9dca2273fd2c9702fb3b6f1801c0f1c | |
parent | dcc7b3fe470fe5f1546c0f3e7c28c07fc54633c5 (diff) | |
download | PeerTube-67100f1f971dd10a466a321899b56c0813e08d31.tar.gz PeerTube-67100f1f971dd10a466a321899b56c0813e08d31.tar.zst PeerTube-67100f1f971dd10a466a321899b56c0813e08d31.zip |
Add a check for the duration of videos
-rw-r--r-- | server/controllers/api/v1/videos.js | 26 | ||||
-rw-r--r-- | server/helpers/customValidators.js | 4 | ||||
-rw-r--r-- | server/initializers/constants.js | 5 | ||||
-rw-r--r-- | server/middlewares/reqValidators/videos.js | 18 | ||||
-rw-r--r-- | server/tests/api/checkParams.js | 30 | ||||
-rw-r--r-- | server/tests/api/fixtures/video_too_long.webm | bin | 0 -> 881903 bytes |
6 files changed, 56 insertions, 27 deletions
diff --git a/server/controllers/api/v1/videos.js b/server/controllers/api/v1/videos.js index 231309c5e..c6dbfbaf2 100644 --- a/server/controllers/api/v1/videos.js +++ b/server/controllers/api/v1/videos.js | |||
@@ -83,23 +83,11 @@ function addVideo (req, res, next) { | |||
83 | const videoInfos = req.body | 83 | const videoInfos = req.body |
84 | 84 | ||
85 | async.waterfall([ | 85 | async.waterfall([ |
86 | function (callback) { | 86 | function seedTheVideo (callback) { |
87 | videos.seed(videoFile.path, callback) | 87 | videos.seed(videoFile.path, callback) |
88 | }, | 88 | }, |
89 | 89 | ||
90 | function seed (torrent, callback) { | 90 | function createThumbnail (torrent, callback) { |
91 | videos.getVideoDuration(videoFile.path, function (err, duration) { | ||
92 | if (err) { | ||
93 | // TODO: unseed the video | ||
94 | logger.error('Cannot retrieve metadata of the file.') | ||
95 | return next(err) | ||
96 | } | ||
97 | |||
98 | callback(null, torrent, duration) | ||
99 | }) | ||
100 | }, | ||
101 | |||
102 | function createThumbnail (torrent, duration, callback) { | ||
103 | videos.createVideoThumbnail(videoFile.path, function (err, thumbnailName) { | 91 | videos.createVideoThumbnail(videoFile.path, function (err, thumbnailName) { |
104 | if (err) { | 92 | if (err) { |
105 | // TODO: unseed the video | 93 | // TODO: unseed the video |
@@ -107,18 +95,18 @@ function addVideo (req, res, next) { | |||
107 | return callback(err) | 95 | return callback(err) |
108 | } | 96 | } |
109 | 97 | ||
110 | callback(null, torrent, duration, thumbnailName) | 98 | callback(null, torrent, thumbnailName) |
111 | }) | 99 | }) |
112 | }, | 100 | }, |
113 | 101 | ||
114 | function insertIntoDB (torrent, duration, thumbnailName, callback) { | 102 | function insertIntoDB (torrent, thumbnailName, callback) { |
115 | const videoData = { | 103 | const videoData = { |
116 | name: videoInfos.name, | 104 | name: videoInfos.name, |
117 | namePath: videoFile.filename, | 105 | namePath: videoFile.filename, |
118 | description: videoInfos.description, | 106 | description: videoInfos.description, |
119 | magnetUri: torrent.magnetURI, | 107 | magnetUri: torrent.magnetURI, |
120 | author: res.locals.oauth.token.user.username, | 108 | author: res.locals.oauth.token.user.username, |
121 | duration: duration, | 109 | duration: videoFile.duration, |
122 | thumbnail: thumbnailName | 110 | thumbnail: thumbnailName |
123 | } | 111 | } |
124 | 112 | ||
@@ -130,11 +118,11 @@ function addVideo (req, res, next) { | |||
130 | return callback(err) | 118 | return callback(err) |
131 | } | 119 | } |
132 | 120 | ||
133 | return callback(null, torrent, duration, thumbnailName, videoData, insertedVideo) | 121 | return callback(null, torrent, thumbnailName, videoData, insertedVideo) |
134 | }) | 122 | }) |
135 | }, | 123 | }, |
136 | 124 | ||
137 | function getThumbnailBase64 (torrent, duration, thumbnailName, videoData, insertedVideo, callback) { | 125 | function getThumbnailBase64 (torrent, thumbnailName, videoData, insertedVideo, callback) { |
138 | videoData.createdDate = insertedVideo.createdDate | 126 | videoData.createdDate = insertedVideo.createdDate |
139 | 127 | ||
140 | fs.readFile(thumbnailsDir + thumbnailName, function (err, thumbnailData) { | 128 | fs.readFile(thumbnailsDir + thumbnailName, function (err, thumbnailData) { |
diff --git a/server/helpers/customValidators.js b/server/helpers/customValidators.js index a8fc6942d..9b982369e 100644 --- a/server/helpers/customValidators.js +++ b/server/helpers/customValidators.js | |||
@@ -2,6 +2,8 @@ | |||
2 | 2 | ||
3 | const validator = require('validator') | 3 | const validator = require('validator') |
4 | 4 | ||
5 | const constants = require('../initializers/constants') | ||
6 | |||
5 | const customValidators = { | 7 | const customValidators = { |
6 | eachIsRemoteVideosAddValid: eachIsRemoteVideosAddValid, | 8 | eachIsRemoteVideosAddValid: eachIsRemoteVideosAddValid, |
7 | eachIsRemoteVideosRemoveValid: eachIsRemoteVideosRemoveValid, | 9 | eachIsRemoteVideosRemoveValid: eachIsRemoteVideosRemoveValid, |
@@ -15,6 +17,8 @@ function eachIsRemoteVideosAddValid (values) { | |||
15 | validator.isLength(val.magnetUri, 10) && | 17 | validator.isLength(val.magnetUri, 10) && |
16 | validator.isURL(val.podUrl) && | 18 | validator.isURL(val.podUrl) && |
17 | !isNaN(val.duration) && | 19 | !isNaN(val.duration) && |
20 | val.duration >= 0 && | ||
21 | val.duration < constants.MAXIMUM_VIDEO_DURATION && | ||
18 | validator.isDate(val.createdDate) | 22 | validator.isDate(val.createdDate) |
19 | }) | 23 | }) |
20 | } | 24 | } |
diff --git a/server/initializers/constants.js b/server/initializers/constants.js index 48e660fe2..d87a376d3 100644 --- a/server/initializers/constants.js +++ b/server/initializers/constants.js | |||
@@ -9,6 +9,9 @@ let FRIEND_BASE_SCORE = 100 | |||
9 | // Time to wait between requests to the friends | 9 | // Time to wait between requests to the friends |
10 | let INTERVAL = 60000 | 10 | let INTERVAL = 60000 |
11 | 11 | ||
12 | // 2 hours maximum for the duration of a video (in seconds) | ||
13 | let MAXIMUM_VIDEO_DURATION = 7200 | ||
14 | |||
12 | // Number of results by default for the pagination | 15 | // Number of results by default for the pagination |
13 | const PAGINATION_COUNT_DEFAULT = 15 | 16 | const PAGINATION_COUNT_DEFAULT = 15 |
14 | 17 | ||
@@ -31,6 +34,7 @@ const THUMBNAILS_STATIC_PATH = '/static/thumbnails' | |||
31 | if (isTestInstance() === true) { | 34 | if (isTestInstance() === true) { |
32 | FRIEND_BASE_SCORE = 20 | 35 | FRIEND_BASE_SCORE = 20 |
33 | INTERVAL = 10000 | 36 | INTERVAL = 10000 |
37 | MAXIMUM_VIDEO_DURATION = 14 | ||
34 | REQUEST_RETRIES = 2 | 38 | REQUEST_RETRIES = 2 |
35 | } | 39 | } |
36 | 40 | ||
@@ -40,6 +44,7 @@ module.exports = { | |||
40 | API_VERSION: API_VERSION, | 44 | API_VERSION: API_VERSION, |
41 | FRIEND_BASE_SCORE: FRIEND_BASE_SCORE, | 45 | FRIEND_BASE_SCORE: FRIEND_BASE_SCORE, |
42 | INTERVAL: INTERVAL, | 46 | INTERVAL: INTERVAL, |
47 | MAXIMUM_VIDEO_DURATION: MAXIMUM_VIDEO_DURATION, | ||
43 | PAGINATION_COUNT_DEFAULT: PAGINATION_COUNT_DEFAULT, | 48 | PAGINATION_COUNT_DEFAULT: PAGINATION_COUNT_DEFAULT, |
44 | PODS_SCORE: PODS_SCORE, | 49 | PODS_SCORE: PODS_SCORE, |
45 | REQUEST_RETRIES: REQUEST_RETRIES, | 50 | REQUEST_RETRIES: REQUEST_RETRIES, |
diff --git a/server/middlewares/reqValidators/videos.js b/server/middlewares/reqValidators/videos.js index c20660452..6e6e75fb3 100644 --- a/server/middlewares/reqValidators/videos.js +++ b/server/middlewares/reqValidators/videos.js | |||
@@ -1,6 +1,7 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | const checkErrors = require('./utils').checkErrors | 3 | const checkErrors = require('./utils').checkErrors |
4 | const constants = require('../../initializers/constants') | ||
4 | const logger = require('../../helpers/logger') | 5 | const logger = require('../../helpers/logger') |
5 | const videos = require('../../lib/videos') | 6 | const videos = require('../../lib/videos') |
6 | const Videos = require('../../models/videos') | 7 | const Videos = require('../../models/videos') |
@@ -20,7 +21,22 @@ function videosAdd (req, res, next) { | |||
20 | 21 | ||
21 | logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files }) | 22 | logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files }) |
22 | 23 | ||
23 | checkErrors(req, res, next) | 24 | checkErrors(req, res, function () { |
25 | const videoFile = req.files.videofile[0] | ||
26 | |||
27 | videos.getVideoDuration(videoFile.path, function (err, duration) { | ||
28 | if (err) { | ||
29 | return res.status(400).send('Cannot retrieve metadata of the file.') | ||
30 | } | ||
31 | |||
32 | if (duration > constants.MAXIMUM_VIDEO_DURATION) { | ||
33 | return res.status(400).send('Duration of the video file is too big.') | ||
34 | } | ||
35 | |||
36 | videoFile.duration = duration | ||
37 | next() | ||
38 | }) | ||
39 | }) | ||
24 | } | 40 | } |
25 | 41 | ||
26 | function videosGet (req, res, next) { | 42 | function videosGet (req, res, next) { |
diff --git a/server/tests/api/checkParams.js b/server/tests/api/checkParams.js index 1f47e5ef4..b63091910 100644 --- a/server/tests/api/checkParams.js +++ b/server/tests/api/checkParams.js | |||
@@ -11,9 +11,9 @@ const utils = require('./utils') | |||
11 | describe('Test parameters validator', function () { | 11 | describe('Test parameters validator', function () { |
12 | let server = null | 12 | let server = null |
13 | 13 | ||
14 | function makePostRequest (path, token, fields, attach, done, fail) { | 14 | function makePostRequest (path, token, fields, attaches, done, fail) { |
15 | let statusCode = 400 | 15 | let statusCode = 400 |
16 | if (fail !== undefined && fail === false) statusCode = 200 | 16 | if (fail !== undefined && fail === false) statusCode = 204 |
17 | 17 | ||
18 | const req = request(server.url) | 18 | const req = request(server.url) |
19 | .post(path) | 19 | .post(path) |
@@ -26,6 +26,11 @@ describe('Test parameters validator', function () { | |||
26 | req.field(field, value) | 26 | req.field(field, value) |
27 | }) | 27 | }) |
28 | 28 | ||
29 | Object.keys(attaches).forEach(function (attach) { | ||
30 | const value = attaches[attach] | ||
31 | req.attach(attach, value) | ||
32 | }) | ||
33 | |||
29 | req.expect(statusCode, done) | 34 | req.expect(statusCode, done) |
30 | } | 35 | } |
31 | 36 | ||
@@ -200,7 +205,18 @@ describe('Test parameters validator', function () { | |||
200 | description: 'my super description' | 205 | description: 'my super description' |
201 | } | 206 | } |
202 | const attach = { | 207 | const attach = { |
203 | 'videofile': pathUtils.join(__dirname, '..', 'fixtures', 'video_short_fake.webm') | 208 | 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short_fake.webm') |
209 | } | ||
210 | makePostRequest(path, server.accessToken, data, attach, done) | ||
211 | }) | ||
212 | |||
213 | it('Should fail with a too big duration', function (done) { | ||
214 | const data = { | ||
215 | name: 'my super name', | ||
216 | description: 'my super description' | ||
217 | } | ||
218 | const attach = { | ||
219 | 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_too_long.webm') | ||
204 | } | 220 | } |
205 | makePostRequest(path, server.accessToken, data, attach, done) | 221 | makePostRequest(path, server.accessToken, data, attach, done) |
206 | }) | 222 | }) |
@@ -217,9 +233,9 @@ describe('Test parameters validator', function () { | |||
217 | attach.videofile = pathUtils.join(__dirname, 'fixtures', 'video_short.mp4') | 233 | attach.videofile = pathUtils.join(__dirname, 'fixtures', 'video_short.mp4') |
218 | makePostRequest(path, server.accessToken, data, attach, function () { | 234 | makePostRequest(path, server.accessToken, data, attach, function () { |
219 | attach.videofile = pathUtils.join(__dirname, 'fixtures', 'video_short.ogv') | 235 | attach.videofile = pathUtils.join(__dirname, 'fixtures', 'video_short.ogv') |
220 | makePostRequest(path, server.accessToken, data, attach, done, true) | 236 | makePostRequest(path, server.accessToken, data, attach, done, false) |
221 | }, true) | 237 | }, false) |
222 | }, true) | 238 | }, false) |
223 | }) | 239 | }) |
224 | }) | 240 | }) |
225 | 241 | ||
@@ -234,7 +250,7 @@ describe('Test parameters validator', function () { | |||
234 | if (err) throw err | 250 | if (err) throw err |
235 | 251 | ||
236 | expect(res.body).to.be.an('array') | 252 | expect(res.body).to.be.an('array') |
237 | expect(res.body.length).to.equal(0) | 253 | expect(res.body.length).to.equal(3) |
238 | 254 | ||
239 | done() | 255 | done() |
240 | }) | 256 | }) |
diff --git a/server/tests/api/fixtures/video_too_long.webm b/server/tests/api/fixtures/video_too_long.webm new file mode 100644 index 000000000..8286f74b0 --- /dev/null +++ b/server/tests/api/fixtures/video_too_long.webm | |||
Binary files differ | |||