mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-05 19:42:24 +02:00
Try to make a better communication (between pods) module
This commit is contained in:
parent
b2e4c0ba1a
commit
528a9efa82
20 changed files with 493 additions and 439 deletions
|
@ -5,12 +5,12 @@ const express = require('express')
|
|||
const router = express.Router()
|
||||
|
||||
const podsController = require('./pods')
|
||||
const remoteVideosController = require('./remoteVideos')
|
||||
const remoteController = require('./remote')
|
||||
const usersController = require('./users')
|
||||
const videosController = require('./videos')
|
||||
|
||||
router.use('/pods', podsController)
|
||||
router.use('/remotevideos', remoteVideosController)
|
||||
router.use('/remote', remoteController)
|
||||
router.use('/users', usersController)
|
||||
router.use('/videos', videosController)
|
||||
router.use('/*', badRequest)
|
||||
|
|
|
@ -9,19 +9,18 @@ const middlewares = require('../../../middlewares')
|
|||
const Pods = require('../../../models/pods')
|
||||
const oAuth2 = middlewares.oauth2
|
||||
const reqValidator = middlewares.reqValidators.pods
|
||||
const secureMiddleware = middlewares.secure
|
||||
const secureRequest = middlewares.reqValidators.remote.secureRequest
|
||||
const signatureValidator = middlewares.reqValidators.remote.signature
|
||||
const videos = require('../../../lib/videos')
|
||||
const Videos = require('../../../models/videos')
|
||||
|
||||
const router = express.Router()
|
||||
|
||||
router.get('/', listPods)
|
||||
router.get('/', listPodsUrl)
|
||||
router.post('/', reqValidator.podsAdd, addPods)
|
||||
router.get('/makefriends', oAuth2.authenticate, reqValidator.makeFriends, makeFriends)
|
||||
router.get('/quitfriends', oAuth2.authenticate, quitFriends)
|
||||
// Post because this is a secured request
|
||||
router.post('/remove', secureRequest, secureMiddleware.decryptBody, removePods)
|
||||
router.post('/remove', signatureValidator, removePods)
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
|
@ -30,22 +29,17 @@ module.exports = router
|
|||
// ---------------------------------------------------------------------------
|
||||
|
||||
function addPods (req, res, next) {
|
||||
const informations = req.body.data
|
||||
const informations = req.body
|
||||
|
||||
async.waterfall([
|
||||
function addPod (callback) {
|
||||
Pods.add(informations, function (err) {
|
||||
return callback(err)
|
||||
})
|
||||
Pods.add(informations, callback)
|
||||
},
|
||||
|
||||
function createVideosOfThisPod (callback) {
|
||||
// Create the remote videos from the new pod
|
||||
videos.createRemoteVideos(informations.videos, function (err) {
|
||||
if (err) logger.error('Cannot create remote videos.', { error: err })
|
||||
function sendMyVideos (podCreated, callback) {
|
||||
friends.sendOwnedVideosToPod(podCreated._id)
|
||||
|
||||
return callback(err)
|
||||
})
|
||||
callback(null)
|
||||
},
|
||||
|
||||
function fetchMyCertificate (callback) {
|
||||
|
@ -57,30 +51,19 @@ function addPods (req, res, next) {
|
|||
|
||||
return callback(null, cert)
|
||||
})
|
||||
},
|
||||
|
||||
function getListOfMyVideos (cert, callback) {
|
||||
Videos.listOwned(function (err, videosList) {
|
||||
if (err) {
|
||||
logger.error('Cannot get the list of owned videos.')
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
return callback(null, cert, videosList)
|
||||
})
|
||||
}
|
||||
], function (err, cert, videosList) {
|
||||
], function (err, cert) {
|
||||
if (err) return next(err)
|
||||
|
||||
return res.json({ cert: cert, videos: videosList })
|
||||
return res.json({ cert: cert })
|
||||
})
|
||||
}
|
||||
|
||||
function listPods (req, res, next) {
|
||||
Pods.list(function (err, podsList) {
|
||||
function listPodsUrl (req, res, next) {
|
||||
Pods.listAllUrls(function (err, podsUrlList) {
|
||||
if (err) return next(err)
|
||||
|
||||
res.json(podsList)
|
||||
res.json(podsUrlList)
|
||||
})
|
||||
}
|
||||
|
||||
|
|
80
server/controllers/api/v1/remote.js
Normal file
80
server/controllers/api/v1/remote.js
Normal file
|
@ -0,0 +1,80 @@
|
|||
'use strict'
|
||||
|
||||
const async = require('async')
|
||||
const express = require('express')
|
||||
|
||||
const middlewares = require('../../../middlewares')
|
||||
const secureMiddleware = middlewares.secure
|
||||
const reqValidator = middlewares.reqValidators.remote
|
||||
const logger = require('../../../helpers/logger')
|
||||
const Videos = require('../../../models/videos')
|
||||
const videos = require('../../../lib/videos')
|
||||
|
||||
const router = express.Router()
|
||||
|
||||
router.post('/videos',
|
||||
reqValidator.signature,
|
||||
reqValidator.dataToDecrypt,
|
||||
secureMiddleware.decryptBody,
|
||||
reqValidator.remoteVideos,
|
||||
remoteVideos
|
||||
)
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
module.exports = router
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
function remoteVideos (req, res, next) {
|
||||
const requests = req.body.data
|
||||
const fromUrl = req.body.signature.url
|
||||
|
||||
// We need to process in the same order to keep consistency
|
||||
// TODO: optimization
|
||||
async.eachSeries(requests, function (request, callbackEach) {
|
||||
const video = request.data
|
||||
|
||||
if (request.type === 'add') {
|
||||
addRemoteVideo(video, callbackEach)
|
||||
} else if (request.type === 'remove') {
|
||||
removeRemoteVideo(video, fromUrl, callbackEach)
|
||||
}
|
||||
})
|
||||
|
||||
// We don't need to keep the other pod waiting
|
||||
return res.type('json').status(204).end()
|
||||
}
|
||||
|
||||
function addRemoteVideo (videoToCreate, callback) {
|
||||
videos.createRemoteVideos([ videoToCreate ], function (err, remoteVideos) {
|
||||
if (err) {
|
||||
logger.error('Cannot create remote videos.', { error: err })
|
||||
// Don't break the process
|
||||
}
|
||||
|
||||
return callback()
|
||||
})
|
||||
}
|
||||
|
||||
function removeRemoteVideo (videoToRemove, fromUrl, callback) {
|
||||
const magnetUris = [ videoToRemove.magnetUri ]
|
||||
|
||||
// We need the list because we have to remove some other stuffs (thumbnail etc)
|
||||
Videos.listFromUrlAndMagnets(fromUrl, magnetUris, function (err, videosList) {
|
||||
if (err) {
|
||||
logger.error('Cannot list videos from url and magnets.', { error: err })
|
||||
// Don't break the process
|
||||
return callback()
|
||||
}
|
||||
|
||||
videos.removeRemoteVideos(videosList, function (err) {
|
||||
if (err) {
|
||||
logger.error('Cannot remove remote videos.', { error: err })
|
||||
// Don't break the process
|
||||
}
|
||||
|
||||
return callback()
|
||||
})
|
||||
})
|
||||
}
|
|
@ -1,66 +0,0 @@
|
|||
'use strict'
|
||||
|
||||
const express = require('express')
|
||||
const map = require('lodash/map')
|
||||
|
||||
const middlewares = require('../../../middlewares')
|
||||
const secureMiddleware = middlewares.secure
|
||||
const reqValidator = middlewares.reqValidators.remote
|
||||
const logger = require('../../../helpers/logger')
|
||||
const Videos = require('../../../models/videos')
|
||||
const videos = require('../../../lib/videos')
|
||||
|
||||
const router = express.Router()
|
||||
|
||||
router.post('/add',
|
||||
reqValidator.secureRequest,
|
||||
secureMiddleware.decryptBody,
|
||||
reqValidator.remoteVideosAdd,
|
||||
addRemoteVideos
|
||||
)
|
||||
|
||||
router.post('/remove',
|
||||
reqValidator.secureRequest,
|
||||
secureMiddleware.decryptBody,
|
||||
reqValidator.remoteVideosRemove,
|
||||
removeRemoteVideo
|
||||
)
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
module.exports = router
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
function addRemoteVideos (req, res, next) {
|
||||
const videosToCreate = req.body.data
|
||||
videos.createRemoteVideos(videosToCreate, function (err, remoteVideos) {
|
||||
if (err) {
|
||||
logger.error('Cannot create remote videos.', { error: err })
|
||||
return next(err)
|
||||
}
|
||||
|
||||
res.type('json').status(201).end()
|
||||
})
|
||||
}
|
||||
|
||||
function removeRemoteVideo (req, res, next) {
|
||||
const fromUrl = req.body.signature.url
|
||||
const magnetUris = map(req.body.data, 'magnetUri')
|
||||
|
||||
Videos.listFromUrlAndMagnets(fromUrl, magnetUris, function (err, videosList) {
|
||||
if (err) {
|
||||
logger.error('Cannot list videos from url and magnets.', { error: err })
|
||||
return next(err)
|
||||
}
|
||||
|
||||
videos.removeRemoteVideos(videosList, function (err) {
|
||||
if (err) {
|
||||
logger.error('Cannot remove remote videos.', { error: err })
|
||||
return next(err)
|
||||
}
|
||||
|
||||
res.type('json').status(204).end()
|
||||
})
|
||||
})
|
||||
}
|
|
@ -3,8 +3,6 @@
|
|||
const async = require('async')
|
||||
const config = require('config')
|
||||
const express = require('express')
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const multer = require('multer')
|
||||
|
||||
const constants = require('../../../initializers/constants')
|
||||
|
@ -46,7 +44,6 @@ const storage = multer.diskStorage({
|
|||
})
|
||||
|
||||
const reqFiles = multer({ storage: storage }).fields([{ name: 'videofile', maxCount: 1 }])
|
||||
const thumbnailsDir = path.join(__dirname, '..', '..', '..', '..', config.get('storage.thumbnails'))
|
||||
|
||||
router.get('/',
|
||||
reqValidatorPagination.pagination,
|
||||
|
@ -127,34 +124,25 @@ function addVideo (req, res, next) {
|
|||
return callback(err)
|
||||
}
|
||||
|
||||
return callback(null, torrent, thumbnailName, videoData, insertedVideo)
|
||||
return callback(null, insertedVideo)
|
||||
})
|
||||
},
|
||||
|
||||
function getThumbnailBase64 (torrent, thumbnailName, videoData, insertedVideo, callback) {
|
||||
videoData.createdDate = insertedVideo.createdDate
|
||||
|
||||
fs.readFile(thumbnailsDir + thumbnailName, function (err, thumbnailData) {
|
||||
function sendToFriends (insertedVideo, callback) {
|
||||
videos.convertVideoToRemote(insertedVideo, function (err, remoteVideo) {
|
||||
if (err) {
|
||||
// TODO unseed the video
|
||||
// TODO remove thumbnail
|
||||
// TODO: remove video
|
||||
logger.error('Cannot read the thumbnail of the video')
|
||||
// TODO delete from DB
|
||||
logger.error('Cannot convert video to remote.')
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
return callback(null, videoData, thumbnailData)
|
||||
// Now we'll add the video's meta data to our friends
|
||||
friends.addVideoToFriends(remoteVideo)
|
||||
|
||||
return callback(null)
|
||||
})
|
||||
},
|
||||
|
||||
function sendToFriends (videoData, thumbnailData, callback) {
|
||||
// Set the image in base64
|
||||
videoData.thumbnailBase64 = new Buffer(thumbnailData).toString('base64')
|
||||
|
||||
// Now we'll add the video's meta data to our friends
|
||||
friends.addVideoToFriends(videoData)
|
||||
|
||||
return callback(null)
|
||||
}
|
||||
|
||||
], function andFinally (err) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue