diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-06-18 16:13:54 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-06-18 16:13:54 +0200 |
commit | 528a9efa8272532bbd0dafc35c3e05e57c50f61e (patch) | |
tree | 62d4417df4ab9b2e53c44dc7271be81b88e4e0e5 /server/controllers | |
parent | b2e4c0ba1a33b8a50491a1f8d111468a7da5640f (diff) | |
download | PeerTube-528a9efa8272532bbd0dafc35c3e05e57c50f61e.tar.gz PeerTube-528a9efa8272532bbd0dafc35c3e05e57c50f61e.tar.zst PeerTube-528a9efa8272532bbd0dafc35c3e05e57c50f61e.zip |
Try to make a better communication (between pods) module
Diffstat (limited to 'server/controllers')
-rw-r--r-- | server/controllers/api/v1/index.js | 4 | ||||
-rw-r--r-- | server/controllers/api/v1/pods.js | 43 | ||||
-rw-r--r-- | server/controllers/api/v1/remote.js | 80 | ||||
-rw-r--r-- | server/controllers/api/v1/remoteVideos.js | 66 | ||||
-rw-r--r-- | server/controllers/api/v1/videos.js | 30 |
5 files changed, 104 insertions, 119 deletions
diff --git a/server/controllers/api/v1/index.js b/server/controllers/api/v1/index.js index 7b3ec32c0..e0c29a8a2 100644 --- a/server/controllers/api/v1/index.js +++ b/server/controllers/api/v1/index.js | |||
@@ -5,12 +5,12 @@ const express = require('express') | |||
5 | const router = express.Router() | 5 | const router = express.Router() |
6 | 6 | ||
7 | const podsController = require('./pods') | 7 | const podsController = require('./pods') |
8 | const remoteVideosController = require('./remoteVideos') | 8 | const remoteController = require('./remote') |
9 | const usersController = require('./users') | 9 | const usersController = require('./users') |
10 | const videosController = require('./videos') | 10 | const videosController = require('./videos') |
11 | 11 | ||
12 | router.use('/pods', podsController) | 12 | router.use('/pods', podsController) |
13 | router.use('/remotevideos', remoteVideosController) | 13 | router.use('/remote', remoteController) |
14 | router.use('/users', usersController) | 14 | router.use('/users', usersController) |
15 | router.use('/videos', videosController) | 15 | router.use('/videos', videosController) |
16 | router.use('/*', badRequest) | 16 | router.use('/*', badRequest) |
diff --git a/server/controllers/api/v1/pods.js b/server/controllers/api/v1/pods.js index ecaeba666..881b2090d 100644 --- a/server/controllers/api/v1/pods.js +++ b/server/controllers/api/v1/pods.js | |||
@@ -9,19 +9,18 @@ const middlewares = require('../../../middlewares') | |||
9 | const Pods = require('../../../models/pods') | 9 | const Pods = require('../../../models/pods') |
10 | const oAuth2 = middlewares.oauth2 | 10 | const oAuth2 = middlewares.oauth2 |
11 | const reqValidator = middlewares.reqValidators.pods | 11 | const reqValidator = middlewares.reqValidators.pods |
12 | const secureMiddleware = middlewares.secure | 12 | const signatureValidator = middlewares.reqValidators.remote.signature |
13 | const secureRequest = middlewares.reqValidators.remote.secureRequest | ||
14 | const videos = require('../../../lib/videos') | 13 | const videos = require('../../../lib/videos') |
15 | const Videos = require('../../../models/videos') | 14 | const Videos = require('../../../models/videos') |
16 | 15 | ||
17 | const router = express.Router() | 16 | const router = express.Router() |
18 | 17 | ||
19 | router.get('/', listPods) | 18 | router.get('/', listPodsUrl) |
20 | router.post('/', reqValidator.podsAdd, addPods) | 19 | router.post('/', reqValidator.podsAdd, addPods) |
21 | router.get('/makefriends', oAuth2.authenticate, reqValidator.makeFriends, makeFriends) | 20 | router.get('/makefriends', oAuth2.authenticate, reqValidator.makeFriends, makeFriends) |
22 | router.get('/quitfriends', oAuth2.authenticate, quitFriends) | 21 | router.get('/quitfriends', oAuth2.authenticate, quitFriends) |
23 | // Post because this is a secured request | 22 | // Post because this is a secured request |
24 | router.post('/remove', secureRequest, secureMiddleware.decryptBody, removePods) | 23 | router.post('/remove', signatureValidator, removePods) |
25 | 24 | ||
26 | // --------------------------------------------------------------------------- | 25 | // --------------------------------------------------------------------------- |
27 | 26 | ||
@@ -30,22 +29,17 @@ module.exports = router | |||
30 | // --------------------------------------------------------------------------- | 29 | // --------------------------------------------------------------------------- |
31 | 30 | ||
32 | function addPods (req, res, next) { | 31 | function addPods (req, res, next) { |
33 | const informations = req.body.data | 32 | const informations = req.body |
34 | 33 | ||
35 | async.waterfall([ | 34 | async.waterfall([ |
36 | function addPod (callback) { | 35 | function addPod (callback) { |
37 | Pods.add(informations, function (err) { | 36 | Pods.add(informations, callback) |
38 | return callback(err) | ||
39 | }) | ||
40 | }, | 37 | }, |
41 | 38 | ||
42 | function createVideosOfThisPod (callback) { | 39 | function sendMyVideos (podCreated, callback) { |
43 | // Create the remote videos from the new pod | 40 | friends.sendOwnedVideosToPod(podCreated._id) |
44 | videos.createRemoteVideos(informations.videos, function (err) { | ||
45 | if (err) logger.error('Cannot create remote videos.', { error: err }) | ||
46 | 41 | ||
47 | return callback(err) | 42 | callback(null) |
48 | }) | ||
49 | }, | 43 | }, |
50 | 44 | ||
51 | function fetchMyCertificate (callback) { | 45 | function fetchMyCertificate (callback) { |
@@ -57,30 +51,19 @@ function addPods (req, res, next) { | |||
57 | 51 | ||
58 | return callback(null, cert) | 52 | return callback(null, cert) |
59 | }) | 53 | }) |
60 | }, | ||
61 | |||
62 | function getListOfMyVideos (cert, callback) { | ||
63 | Videos.listOwned(function (err, videosList) { | ||
64 | if (err) { | ||
65 | logger.error('Cannot get the list of owned videos.') | ||
66 | return callback(err) | ||
67 | } | ||
68 | |||
69 | return callback(null, cert, videosList) | ||
70 | }) | ||
71 | } | 54 | } |
72 | ], function (err, cert, videosList) { | 55 | ], function (err, cert) { |
73 | if (err) return next(err) | 56 | if (err) return next(err) |
74 | 57 | ||
75 | return res.json({ cert: cert, videos: videosList }) | 58 | return res.json({ cert: cert }) |
76 | }) | 59 | }) |
77 | } | 60 | } |
78 | 61 | ||
79 | function listPods (req, res, next) { | 62 | function listPodsUrl (req, res, next) { |
80 | Pods.list(function (err, podsList) { | 63 | Pods.listAllUrls(function (err, podsUrlList) { |
81 | if (err) return next(err) | 64 | if (err) return next(err) |
82 | 65 | ||
83 | res.json(podsList) | 66 | res.json(podsUrlList) |
84 | }) | 67 | }) |
85 | } | 68 | } |
86 | 69 | ||
diff --git a/server/controllers/api/v1/remote.js b/server/controllers/api/v1/remote.js new file mode 100644 index 000000000..ced8470d7 --- /dev/null +++ b/server/controllers/api/v1/remote.js | |||
@@ -0,0 +1,80 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const async = require('async') | ||
4 | const express = require('express') | ||
5 | |||
6 | const middlewares = require('../../../middlewares') | ||
7 | const secureMiddleware = middlewares.secure | ||
8 | const reqValidator = middlewares.reqValidators.remote | ||
9 | const logger = require('../../../helpers/logger') | ||
10 | const Videos = require('../../../models/videos') | ||
11 | const videos = require('../../../lib/videos') | ||
12 | |||
13 | const router = express.Router() | ||
14 | |||
15 | router.post('/videos', | ||
16 | reqValidator.signature, | ||
17 | reqValidator.dataToDecrypt, | ||
18 | secureMiddleware.decryptBody, | ||
19 | reqValidator.remoteVideos, | ||
20 | remoteVideos | ||
21 | ) | ||
22 | |||
23 | // --------------------------------------------------------------------------- | ||
24 | |||
25 | module.exports = router | ||
26 | |||
27 | // --------------------------------------------------------------------------- | ||
28 | |||
29 | function remoteVideos (req, res, next) { | ||
30 | const requests = req.body.data | ||
31 | const fromUrl = req.body.signature.url | ||
32 | |||
33 | // We need to process in the same order to keep consistency | ||
34 | // TODO: optimization | ||
35 | async.eachSeries(requests, function (request, callbackEach) { | ||
36 | const video = request.data | ||
37 | |||
38 | if (request.type === 'add') { | ||
39 | addRemoteVideo(video, callbackEach) | ||
40 | } else if (request.type === 'remove') { | ||
41 | removeRemoteVideo(video, fromUrl, callbackEach) | ||
42 | } | ||
43 | }) | ||
44 | |||
45 | // We don't need to keep the other pod waiting | ||
46 | return res.type('json').status(204).end() | ||
47 | } | ||
48 | |||
49 | function addRemoteVideo (videoToCreate, callback) { | ||
50 | videos.createRemoteVideos([ videoToCreate ], function (err, remoteVideos) { | ||
51 | if (err) { | ||
52 | logger.error('Cannot create remote videos.', { error: err }) | ||
53 | // Don't break the process | ||
54 | } | ||
55 | |||
56 | return callback() | ||
57 | }) | ||
58 | } | ||
59 | |||
60 | function removeRemoteVideo (videoToRemove, fromUrl, callback) { | ||
61 | const magnetUris = [ videoToRemove.magnetUri ] | ||
62 | |||
63 | // We need the list because we have to remove some other stuffs (thumbnail etc) | ||
64 | Videos.listFromUrlAndMagnets(fromUrl, magnetUris, function (err, videosList) { | ||
65 | if (err) { | ||
66 | logger.error('Cannot list videos from url and magnets.', { error: err }) | ||
67 | // Don't break the process | ||
68 | return callback() | ||
69 | } | ||
70 | |||
71 | videos.removeRemoteVideos(videosList, function (err) { | ||
72 | if (err) { | ||
73 | logger.error('Cannot remove remote videos.', { error: err }) | ||
74 | // Don't break the process | ||
75 | } | ||
76 | |||
77 | return callback() | ||
78 | }) | ||
79 | }) | ||
80 | } | ||
diff --git a/server/controllers/api/v1/remoteVideos.js b/server/controllers/api/v1/remoteVideos.js deleted file mode 100644 index 2f41c0411..000000000 --- a/server/controllers/api/v1/remoteVideos.js +++ /dev/null | |||
@@ -1,66 +0,0 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const express = require('express') | ||
4 | const map = require('lodash/map') | ||
5 | |||
6 | const middlewares = require('../../../middlewares') | ||
7 | const secureMiddleware = middlewares.secure | ||
8 | const reqValidator = middlewares.reqValidators.remote | ||
9 | const logger = require('../../../helpers/logger') | ||
10 | const Videos = require('../../../models/videos') | ||
11 | const videos = require('../../../lib/videos') | ||
12 | |||
13 | const router = express.Router() | ||
14 | |||
15 | router.post('/add', | ||
16 | reqValidator.secureRequest, | ||
17 | secureMiddleware.decryptBody, | ||
18 | reqValidator.remoteVideosAdd, | ||
19 | addRemoteVideos | ||
20 | ) | ||
21 | |||
22 | router.post('/remove', | ||
23 | reqValidator.secureRequest, | ||
24 | secureMiddleware.decryptBody, | ||
25 | reqValidator.remoteVideosRemove, | ||
26 | removeRemoteVideo | ||
27 | ) | ||
28 | |||
29 | // --------------------------------------------------------------------------- | ||
30 | |||
31 | module.exports = router | ||
32 | |||
33 | // --------------------------------------------------------------------------- | ||
34 | |||
35 | function addRemoteVideos (req, res, next) { | ||
36 | const videosToCreate = req.body.data | ||
37 | videos.createRemoteVideos(videosToCreate, function (err, remoteVideos) { | ||
38 | if (err) { | ||
39 | logger.error('Cannot create remote videos.', { error: err }) | ||
40 | return next(err) | ||
41 | } | ||
42 | |||
43 | res.type('json').status(201).end() | ||
44 | }) | ||
45 | } | ||
46 | |||
47 | function removeRemoteVideo (req, res, next) { | ||
48 | const fromUrl = req.body.signature.url | ||
49 | const magnetUris = map(req.body.data, 'magnetUri') | ||
50 | |||
51 | Videos.listFromUrlAndMagnets(fromUrl, magnetUris, function (err, videosList) { | ||
52 | if (err) { | ||
53 | logger.error('Cannot list videos from url and magnets.', { error: err }) | ||
54 | return next(err) | ||
55 | } | ||
56 | |||
57 | videos.removeRemoteVideos(videosList, function (err) { | ||
58 | if (err) { | ||
59 | logger.error('Cannot remove remote videos.', { error: err }) | ||
60 | return next(err) | ||
61 | } | ||
62 | |||
63 | res.type('json').status(204).end() | ||
64 | }) | ||
65 | }) | ||
66 | } | ||
diff --git a/server/controllers/api/v1/videos.js b/server/controllers/api/v1/videos.js index 5449cbcfa..2edb31122 100644 --- a/server/controllers/api/v1/videos.js +++ b/server/controllers/api/v1/videos.js | |||
@@ -3,8 +3,6 @@ | |||
3 | const async = require('async') | 3 | const async = require('async') |
4 | const config = require('config') | 4 | const config = require('config') |
5 | const express = require('express') | 5 | const express = require('express') |
6 | const fs = require('fs') | ||
7 | const path = require('path') | ||
8 | const multer = require('multer') | 6 | const multer = require('multer') |
9 | 7 | ||
10 | const constants = require('../../../initializers/constants') | 8 | const constants = require('../../../initializers/constants') |
@@ -46,7 +44,6 @@ const storage = multer.diskStorage({ | |||
46 | }) | 44 | }) |
47 | 45 | ||
48 | const reqFiles = multer({ storage: storage }).fields([{ name: 'videofile', maxCount: 1 }]) | 46 | const reqFiles = multer({ storage: storage }).fields([{ name: 'videofile', maxCount: 1 }]) |
49 | const thumbnailsDir = path.join(__dirname, '..', '..', '..', '..', config.get('storage.thumbnails')) | ||
50 | 47 | ||
51 | router.get('/', | 48 | router.get('/', |
52 | reqValidatorPagination.pagination, | 49 | reqValidatorPagination.pagination, |
@@ -127,34 +124,25 @@ function addVideo (req, res, next) { | |||
127 | return callback(err) | 124 | return callback(err) |
128 | } | 125 | } |
129 | 126 | ||
130 | return callback(null, torrent, thumbnailName, videoData, insertedVideo) | 127 | return callback(null, insertedVideo) |
131 | }) | 128 | }) |
132 | }, | 129 | }, |
133 | 130 | ||
134 | function getThumbnailBase64 (torrent, thumbnailName, videoData, insertedVideo, callback) { | 131 | function sendToFriends (insertedVideo, callback) { |
135 | videoData.createdDate = insertedVideo.createdDate | 132 | videos.convertVideoToRemote(insertedVideo, function (err, remoteVideo) { |
136 | |||
137 | fs.readFile(thumbnailsDir + thumbnailName, function (err, thumbnailData) { | ||
138 | if (err) { | 133 | if (err) { |
139 | // TODO unseed the video | 134 | // TODO unseed the video |
140 | // TODO remove thumbnail | 135 | // TODO remove thumbnail |
141 | // TODO: remove video | 136 | // TODO delete from DB |
142 | logger.error('Cannot read the thumbnail of the video') | 137 | logger.error('Cannot convert video to remote.') |
143 | return callback(err) | 138 | return callback(err) |
144 | } | 139 | } |
145 | 140 | ||
146 | return callback(null, videoData, thumbnailData) | 141 | // Now we'll add the video's meta data to our friends |
147 | }) | 142 | friends.addVideoToFriends(remoteVideo) |
148 | }, | ||
149 | |||
150 | function sendToFriends (videoData, thumbnailData, callback) { | ||
151 | // Set the image in base64 | ||
152 | videoData.thumbnailBase64 = new Buffer(thumbnailData).toString('base64') | ||
153 | 143 | ||
154 | // Now we'll add the video's meta data to our friends | 144 | return callback(null) |
155 | friends.addVideoToFriends(videoData) | 145 | }) |
156 | |||
157 | return callback(null) | ||
158 | } | 146 | } |
159 | 147 | ||
160 | ], function andFinally (err) { | 148 | ], function andFinally (err) { |