aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-06-18 16:13:54 +0200
committerChocobozzz <florian.bigard@gmail.com>2016-06-18 16:13:54 +0200
commit528a9efa8272532bbd0dafc35c3e05e57c50f61e (patch)
tree62d4417df4ab9b2e53c44dc7271be81b88e4e0e5 /server/controllers
parentb2e4c0ba1a33b8a50491a1f8d111468a7da5640f (diff)
downloadPeerTube-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.js4
-rw-r--r--server/controllers/api/v1/pods.js43
-rw-r--r--server/controllers/api/v1/remote.js80
-rw-r--r--server/controllers/api/v1/remoteVideos.js66
-rw-r--r--server/controllers/api/v1/videos.js30
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')
5const router = express.Router() 5const router = express.Router()
6 6
7const podsController = require('./pods') 7const podsController = require('./pods')
8const remoteVideosController = require('./remoteVideos') 8const remoteController = require('./remote')
9const usersController = require('./users') 9const usersController = require('./users')
10const videosController = require('./videos') 10const videosController = require('./videos')
11 11
12router.use('/pods', podsController) 12router.use('/pods', podsController)
13router.use('/remotevideos', remoteVideosController) 13router.use('/remote', remoteController)
14router.use('/users', usersController) 14router.use('/users', usersController)
15router.use('/videos', videosController) 15router.use('/videos', videosController)
16router.use('/*', badRequest) 16router.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')
9const Pods = require('../../../models/pods') 9const Pods = require('../../../models/pods')
10const oAuth2 = middlewares.oauth2 10const oAuth2 = middlewares.oauth2
11const reqValidator = middlewares.reqValidators.pods 11const reqValidator = middlewares.reqValidators.pods
12const secureMiddleware = middlewares.secure 12const signatureValidator = middlewares.reqValidators.remote.signature
13const secureRequest = middlewares.reqValidators.remote.secureRequest
14const videos = require('../../../lib/videos') 13const videos = require('../../../lib/videos')
15const Videos = require('../../../models/videos') 14const Videos = require('../../../models/videos')
16 15
17const router = express.Router() 16const router = express.Router()
18 17
19router.get('/', listPods) 18router.get('/', listPodsUrl)
20router.post('/', reqValidator.podsAdd, addPods) 19router.post('/', reqValidator.podsAdd, addPods)
21router.get('/makefriends', oAuth2.authenticate, reqValidator.makeFriends, makeFriends) 20router.get('/makefriends', oAuth2.authenticate, reqValidator.makeFriends, makeFriends)
22router.get('/quitfriends', oAuth2.authenticate, quitFriends) 21router.get('/quitfriends', oAuth2.authenticate, quitFriends)
23// Post because this is a secured request 22// Post because this is a secured request
24router.post('/remove', secureRequest, secureMiddleware.decryptBody, removePods) 23router.post('/remove', signatureValidator, removePods)
25 24
26// --------------------------------------------------------------------------- 25// ---------------------------------------------------------------------------
27 26
@@ -30,22 +29,17 @@ module.exports = router
30// --------------------------------------------------------------------------- 29// ---------------------------------------------------------------------------
31 30
32function addPods (req, res, next) { 31function 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
79function listPods (req, res, next) { 62function 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
3const async = require('async')
4const express = require('express')
5
6const middlewares = require('../../../middlewares')
7const secureMiddleware = middlewares.secure
8const reqValidator = middlewares.reqValidators.remote
9const logger = require('../../../helpers/logger')
10const Videos = require('../../../models/videos')
11const videos = require('../../../lib/videos')
12
13const router = express.Router()
14
15router.post('/videos',
16 reqValidator.signature,
17 reqValidator.dataToDecrypt,
18 secureMiddleware.decryptBody,
19 reqValidator.remoteVideos,
20 remoteVideos
21)
22
23// ---------------------------------------------------------------------------
24
25module.exports = router
26
27// ---------------------------------------------------------------------------
28
29function 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
49function 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
60function 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
3const express = require('express')
4const map = require('lodash/map')
5
6const middlewares = require('../../../middlewares')
7const secureMiddleware = middlewares.secure
8const reqValidator = middlewares.reqValidators.remote
9const logger = require('../../../helpers/logger')
10const Videos = require('../../../models/videos')
11const videos = require('../../../lib/videos')
12
13const router = express.Router()
14
15router.post('/add',
16 reqValidator.secureRequest,
17 secureMiddleware.decryptBody,
18 reqValidator.remoteVideosAdd,
19 addRemoteVideos
20)
21
22router.post('/remove',
23 reqValidator.secureRequest,
24 secureMiddleware.decryptBody,
25 reqValidator.remoteVideosRemove,
26 removeRemoteVideo
27)
28
29// ---------------------------------------------------------------------------
30
31module.exports = router
32
33// ---------------------------------------------------------------------------
34
35function 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
47function 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 @@
3const async = require('async') 3const async = require('async')
4const config = require('config') 4const config = require('config')
5const express = require('express') 5const express = require('express')
6const fs = require('fs')
7const path = require('path')
8const multer = require('multer') 6const multer = require('multer')
9 7
10const constants = require('../../../initializers/constants') 8const constants = require('../../../initializers/constants')
@@ -46,7 +44,6 @@ const storage = multer.diskStorage({
46}) 44})
47 45
48const reqFiles = multer({ storage: storage }).fields([{ name: 'videofile', maxCount: 1 }]) 46const reqFiles = multer({ storage: storage }).fields([{ name: 'videofile', maxCount: 1 }])
49const thumbnailsDir = path.join(__dirname, '..', '..', '..', '..', config.get('storage.thumbnails'))
50 47
51router.get('/', 48router.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) {