]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/v1/remote.js
Try to make a better communication (between pods) module
[github/Chocobozzz/PeerTube.git] / server / controllers / api / v1 / remote.js
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 }