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/api/v1/remote.js | |
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/api/v1/remote.js')
-rw-r--r-- | server/controllers/api/v1/remote.js | 80 |
1 files changed, 80 insertions, 0 deletions
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 | } | ||