diff options
Diffstat (limited to 'server')
-rw-r--r-- | server/controllers/api/v1/videos.js | 36 | ||||
-rw-r--r-- | server/lib/videos.js | 4 | ||||
-rw-r--r-- | server/middlewares/reqValidators/videos.js | 16 | ||||
-rw-r--r-- | server/tests/api/multiplePods.js | 14 | ||||
-rw-r--r-- | server/tests/api/singlePod.js | 26 | ||||
-rw-r--r-- | server/tests/api/utils.js | 12 |
6 files changed, 82 insertions, 26 deletions
diff --git a/server/controllers/api/v1/videos.js b/server/controllers/api/v1/videos.js index 1eea417d4..4384724c1 100644 --- a/server/controllers/api/v1/videos.js +++ b/server/controllers/api/v1/videos.js | |||
@@ -83,14 +83,15 @@ function addVideo (req, res, next) { | |||
83 | } | 83 | } |
84 | 84 | ||
85 | function getVideos (req, res, next) { | 85 | function getVideos (req, res, next) { |
86 | Videos.get(req.params.id, function (err, video) { | 86 | Videos.get(req.params.id, function (err, video_obj) { |
87 | if (err) return next(err) | 87 | if (err) return next(err) |
88 | 88 | ||
89 | if (video === null) { | 89 | const state = videos.getVideoState(video_obj) |
90 | res.type('json').status(204).end() | 90 | if (state.exist === false) { |
91 | return res.type('json').status(204).end() | ||
91 | } | 92 | } |
92 | 93 | ||
93 | res.json(video) | 94 | res.json(getFormatedVideo(video_obj)) |
94 | }) | 95 | }) |
95 | } | 96 | } |
96 | 97 | ||
@@ -98,7 +99,7 @@ function listVideos (req, res, next) { | |||
98 | Videos.list(function (err, videos_list) { | 99 | Videos.list(function (err, videos_list) { |
99 | if (err) return next(err) | 100 | if (err) return next(err) |
100 | 101 | ||
101 | res.json(videos_list) | 102 | res.json(getFormatedVideos(videos_list)) |
102 | }) | 103 | }) |
103 | } | 104 | } |
104 | 105 | ||
@@ -127,12 +128,35 @@ function searchVideos (req, res, next) { | |||
127 | Videos.search(req.params.name, function (err, videos_list) { | 128 | Videos.search(req.params.name, function (err, videos_list) { |
128 | if (err) return next(err) | 129 | if (err) return next(err) |
129 | 130 | ||
130 | res.json(videos_list) | 131 | res.json(getFormatedVideos(videos_list)) |
131 | }) | 132 | }) |
132 | } | 133 | } |
133 | 134 | ||
134 | // --------------------------------------------------------------------------- | 135 | // --------------------------------------------------------------------------- |
135 | 136 | ||
137 | function getFormatedVideo (video_obj) { | ||
138 | const formated_video = { | ||
139 | id: video_obj._id, | ||
140 | name: video_obj.name, | ||
141 | description: video_obj.description, | ||
142 | podUrl: video_obj.podUrl, | ||
143 | isLocal: videos.getVideoState(video_obj).owned, | ||
144 | magnetUri: video_obj.magnetUri | ||
145 | } | ||
146 | |||
147 | return formated_video | ||
148 | } | ||
149 | |||
150 | function getFormatedVideos (videos_obj) { | ||
151 | const formated_videos = [] | ||
152 | |||
153 | videos_obj.forEach(function (video_obj) { | ||
154 | formated_videos.push(getFormatedVideo(video_obj)) | ||
155 | }) | ||
156 | |||
157 | return formated_videos | ||
158 | } | ||
159 | |||
136 | // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process | 160 | // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process |
137 | function removeTorrent (magnetUri, callback) { | 161 | function removeTorrent (magnetUri, callback) { |
138 | try { | 162 | try { |
diff --git a/server/lib/videos.js b/server/lib/videos.js index eb3a0125a..24178561d 100644 --- a/server/lib/videos.js +++ b/server/lib/videos.js | |||
@@ -16,14 +16,14 @@ const videos = { | |||
16 | seedAllExisting: seedAllExisting | 16 | seedAllExisting: seedAllExisting |
17 | } | 17 | } |
18 | 18 | ||
19 | function getVideoState (video, callback) { | 19 | function getVideoState (video) { |
20 | const exist = (video !== null) | 20 | const exist = (video !== null) |
21 | let owned = false | 21 | let owned = false |
22 | if (exist === true) { | 22 | if (exist === true) { |
23 | owned = (video.namePath !== null) | 23 | owned = (video.namePath !== null) |
24 | } | 24 | } |
25 | 25 | ||
26 | return callback({ exist: exist, owned: owned }) | 26 | return { exist: exist, owned: owned } |
27 | } | 27 | } |
28 | 28 | ||
29 | function seed (path, callback) { | 29 | function seed (path, callback) { |
diff --git a/server/middlewares/reqValidators/videos.js b/server/middlewares/reqValidators/videos.js index 4057e72cd..12e878e81 100644 --- a/server/middlewares/reqValidators/videos.js +++ b/server/middlewares/reqValidators/videos.js | |||
@@ -35,11 +35,10 @@ function videosGet (req, res, next) { | |||
35 | res.sendStatus(500) | 35 | res.sendStatus(500) |
36 | } | 36 | } |
37 | 37 | ||
38 | videos.getVideoState(video, function (state) { | 38 | const state = videos.getVideoState(video) |
39 | if (state.exist === false) return res.status(404).send('Video not found') | 39 | if (state.exist === false) return res.status(404).send('Video not found') |
40 | 40 | ||
41 | next() | 41 | next() |
42 | }) | ||
43 | }) | 42 | }) |
44 | }) | 43 | }) |
45 | } | 44 | } |
@@ -56,12 +55,11 @@ function videosRemove (req, res, next) { | |||
56 | res.sendStatus(500) | 55 | res.sendStatus(500) |
57 | } | 56 | } |
58 | 57 | ||
59 | videos.getVideoState(video, function (state) { | 58 | const state = videos.getVideoState(video) |
60 | if (state.exist === false) return res.status(404).send('Video not found') | 59 | if (state.exist === false) return res.status(404).send('Video not found') |
61 | else if (state.owned === false) return res.status(403).send('Cannot remove video of another pod') | 60 | else if (state.owned === false) return res.status(403).send('Cannot remove video of another pod') |
62 | 61 | ||
63 | next() | 62 | next() |
64 | }) | ||
65 | }) | 63 | }) |
66 | }) | 64 | }) |
67 | } | 65 | } |
diff --git a/server/tests/api/multiplePods.js b/server/tests/api/multiplePods.js index e8b182622..0e2355a55 100644 --- a/server/tests/api/multiplePods.js +++ b/server/tests/api/multiplePods.js | |||
@@ -205,8 +205,8 @@ describe('Test multiple pods', function () { | |||
205 | if (err) throw err | 205 | if (err) throw err |
206 | 206 | ||
207 | const video = res.body[0] | 207 | const video = res.body[0] |
208 | to_remove.push(res.body[2]._id) | 208 | to_remove.push(res.body[2].id) |
209 | to_remove.push(res.body[3]._id) | 209 | to_remove.push(res.body[3].id) |
210 | 210 | ||
211 | webtorrent.add(video.magnetUri, function (torrent) { | 211 | webtorrent.add(video.magnetUri, function (torrent) { |
212 | expect(torrent.files).to.exist | 212 | expect(torrent.files).to.exist |
@@ -300,11 +300,11 @@ describe('Test multiple pods', function () { | |||
300 | const videos = res.body | 300 | const videos = res.body |
301 | expect(videos).to.be.an('array') | 301 | expect(videos).to.be.an('array') |
302 | expect(videos.length).to.equal(2) | 302 | expect(videos.length).to.equal(2) |
303 | expect(videos[0]._id).not.to.equal(videos[1]._id) | 303 | expect(videos[0].id).not.to.equal(videos[1].id) |
304 | expect(videos[0]._id).not.to.equal(to_remove[0]) | 304 | expect(videos[0].id).not.to.equal(to_remove[0]) |
305 | expect(videos[1]._id).not.to.equal(to_remove[0]) | 305 | expect(videos[1].id).not.to.equal(to_remove[0]) |
306 | expect(videos[0]._id).not.to.equal(to_remove[1]) | 306 | expect(videos[0].id).not.to.equal(to_remove[1]) |
307 | expect(videos[1]._id).not.to.equal(to_remove[1]) | 307 | expect(videos[1].id).not.to.equal(to_remove[1]) |
308 | 308 | ||
309 | callback() | 309 | callback() |
310 | }) | 310 | }) |
diff --git a/server/tests/api/singlePod.js b/server/tests/api/singlePod.js index 14f893f13..0b96f221a 100644 --- a/server/tests/api/singlePod.js +++ b/server/tests/api/singlePod.js | |||
@@ -68,7 +68,30 @@ describe('Test a single pod', function () { | |||
68 | expect(video.podUrl).to.equal('http://localhost:9001') | 68 | expect(video.podUrl).to.equal('http://localhost:9001') |
69 | expect(video.magnetUri).to.exist | 69 | expect(video.magnetUri).to.exist |
70 | 70 | ||
71 | video_id = video._id | 71 | video_id = video.id |
72 | |||
73 | webtorrent.add(video.magnetUri, function (torrent) { | ||
74 | expect(torrent.files).to.exist | ||
75 | expect(torrent.files.length).to.equal(1) | ||
76 | expect(torrent.files[0].path).to.exist.and.to.not.equal('') | ||
77 | |||
78 | done() | ||
79 | }) | ||
80 | }) | ||
81 | }) | ||
82 | |||
83 | it('Should get the video', function (done) { | ||
84 | // Yes, this could be long | ||
85 | this.timeout(60000) | ||
86 | |||
87 | utils.getVideo(url, video_id, function (err, res) { | ||
88 | if (err) throw err | ||
89 | |||
90 | const video = res.body | ||
91 | expect(video.name).to.equal('my super name') | ||
92 | expect(video.description).to.equal('my super description') | ||
93 | expect(video.podUrl).to.equal('http://localhost:9001') | ||
94 | expect(video.magnetUri).to.exist | ||
72 | 95 | ||
73 | webtorrent.add(video.magnetUri, function (torrent) { | 96 | webtorrent.add(video.magnetUri, function (torrent) { |
74 | expect(torrent.files).to.exist | 97 | expect(torrent.files).to.exist |
@@ -91,7 +114,6 @@ describe('Test a single pod', function () { | |||
91 | expect(video.name).to.equal('my super name') | 114 | expect(video.name).to.equal('my super name') |
92 | expect(video.description).to.equal('my super description') | 115 | expect(video.description).to.equal('my super description') |
93 | expect(video.podUrl).to.equal('http://localhost:9001') | 116 | expect(video.podUrl).to.equal('http://localhost:9001') |
94 | expect(video.magnetUri).to.exist | ||
95 | 117 | ||
96 | done() | 118 | done() |
97 | }) | 119 | }) |
diff --git a/server/tests/api/utils.js b/server/tests/api/utils.js index 05142085f..ea0982e81 100644 --- a/server/tests/api/utils.js +++ b/server/tests/api/utils.js | |||
@@ -9,6 +9,7 @@ const request = require('supertest') | |||
9 | const testUtils = { | 9 | const testUtils = { |
10 | flushTests: flushTests, | 10 | flushTests: flushTests, |
11 | getFriendsList: getFriendsList, | 11 | getFriendsList: getFriendsList, |
12 | getVideo: getVideo, | ||
12 | getVideosList: getVideosList, | 13 | getVideosList: getVideosList, |
13 | makeFriends: makeFriends, | 14 | makeFriends: makeFriends, |
14 | quitFriends: quitFriends, | 15 | quitFriends: quitFriends, |
@@ -36,6 +37,17 @@ function getFriendsList (url, end) { | |||
36 | .end(end) | 37 | .end(end) |
37 | } | 38 | } |
38 | 39 | ||
40 | function getVideo (url, id, end) { | ||
41 | const path = '/api/v1/videos/' + id | ||
42 | |||
43 | request(url) | ||
44 | .get(path) | ||
45 | .set('Accept', 'application/json') | ||
46 | .expect(200) | ||
47 | .expect('Content-Type', /json/) | ||
48 | .end(end) | ||
49 | } | ||
50 | |||
39 | function getVideosList (url, end) { | 51 | function getVideosList (url, end) { |
40 | const path = '/api/v1/videos' | 52 | const path = '/api/v1/videos' |
41 | 53 | ||