diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-10-21 12:16:28 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-10-21 12:16:28 +0200 |
commit | f253b1c1f19d9cb056ab95b2cb6208952e073894 (patch) | |
tree | ce75f6a96d9ceebd09d33cedfa008c4efb32477b /server/controllers/api/remote.js | |
parent | 844e39c2f843c6e2db81699176f13972c29c42fa (diff) | |
download | PeerTube-f253b1c1f19d9cb056ab95b2cb6208952e073894.tar.gz PeerTube-f253b1c1f19d9cb056ab95b2cb6208952e073894.tar.zst PeerTube-f253b1c1f19d9cb056ab95b2cb6208952e073894.zip |
Server: remove v1 directory, we don't really need it
Diffstat (limited to 'server/controllers/api/remote.js')
-rw-r--r-- | server/controllers/api/remote.js | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/server/controllers/api/remote.js b/server/controllers/api/remote.js new file mode 100644 index 000000000..17b1d07c4 --- /dev/null +++ b/server/controllers/api/remote.js | |||
@@ -0,0 +1,83 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const each = require('async/each') | ||
4 | const eachSeries = require('async/eachSeries') | ||
5 | const express = require('express') | ||
6 | const mongoose = require('mongoose') | ||
7 | |||
8 | const middlewares = require('../../middlewares') | ||
9 | const secureMiddleware = middlewares.secure | ||
10 | const validators = middlewares.validators.remote | ||
11 | const logger = require('../../helpers/logger') | ||
12 | |||
13 | const router = express.Router() | ||
14 | const Video = mongoose.model('Video') | ||
15 | |||
16 | router.post('/videos', | ||
17 | validators.signature, | ||
18 | validators.dataToDecrypt, | ||
19 | secureMiddleware.checkSignature, | ||
20 | secureMiddleware.decryptBody, | ||
21 | validators.remoteVideos, | ||
22 | remoteVideos | ||
23 | ) | ||
24 | |||
25 | // --------------------------------------------------------------------------- | ||
26 | |||
27 | module.exports = router | ||
28 | |||
29 | // --------------------------------------------------------------------------- | ||
30 | |||
31 | function remoteVideos (req, res, next) { | ||
32 | const requests = req.body.data | ||
33 | const fromUrl = req.body.signature.url | ||
34 | |||
35 | // We need to process in the same order to keep consistency | ||
36 | // TODO: optimization | ||
37 | eachSeries(requests, function (request, callbackEach) { | ||
38 | const videoData = request.data | ||
39 | |||
40 | if (request.type === 'add') { | ||
41 | addRemoteVideo(videoData, callbackEach) | ||
42 | } else if (request.type === 'remove') { | ||
43 | removeRemoteVideo(videoData, fromUrl, callbackEach) | ||
44 | } else { | ||
45 | logger.error('Unkown remote request type %s.', request.type) | ||
46 | } | ||
47 | }, function (err) { | ||
48 | if (err) logger.error('Error managing remote videos.', { error: err }) | ||
49 | }) | ||
50 | |||
51 | // We don't need to keep the other pod waiting | ||
52 | return res.type('json').status(204).end() | ||
53 | } | ||
54 | |||
55 | function addRemoteVideo (videoToCreateData, callback) { | ||
56 | logger.debug('Adding remote video %s.', videoToCreateData.magnetUri) | ||
57 | |||
58 | // Mongoose pre hook will automatically create the thumbnail on disk | ||
59 | videoToCreateData.thumbnail = videoToCreateData.thumbnailBase64 | ||
60 | |||
61 | const video = new Video(videoToCreateData) | ||
62 | video.save(callback) | ||
63 | } | ||
64 | |||
65 | function removeRemoteVideo (videoToRemoveData, fromUrl, callback) { | ||
66 | // We need the list because we have to remove some other stuffs (thumbnail etc) | ||
67 | Video.listByUrlAndMagnet(fromUrl, videoToRemoveData.magnetUri, function (err, videosList) { | ||
68 | if (err) { | ||
69 | logger.error('Cannot list videos from url and magnets.', { error: err }) | ||
70 | return callback(err) | ||
71 | } | ||
72 | |||
73 | if (videosList.length === 0) { | ||
74 | logger.error('No remote video was found for this pod.', { magnetUri: videoToRemoveData.magnetUri, podUrl: fromUrl }) | ||
75 | } | ||
76 | |||
77 | each(videosList, function (video, callbackEach) { | ||
78 | logger.debug('Removing remote video %s.', video.magnetUri) | ||
79 | |||
80 | video.remove(callbackEach) | ||
81 | }, callback) | ||
82 | }) | ||
83 | } | ||