]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/v1/remote.js
3 const async
= require('async')
4 const express
= require('express')
5 const mongoose
= require('mongoose')
7 const middlewares
= require('../../../middlewares')
8 const secureMiddleware
= middlewares
.secure
9 const validators
= middlewares
.validators
.remote
10 const logger
= require('../../../helpers/logger')
12 const router
= express
.Router()
13 const Video
= mongoose
.model('Video')
15 router
.post('/videos',
17 validators
.dataToDecrypt
,
18 secureMiddleware
.decryptBody
,
19 validators
.remoteVideos
,
23 // ---------------------------------------------------------------------------
25 module
.exports
= router
27 // ---------------------------------------------------------------------------
29 function remoteVideos (req
, res
, next
) {
30 const requests
= req
.body
.data
31 const fromUrl
= req
.body
.signature
.url
33 // We need to process in the same order to keep consistency
35 async
.eachSeries(requests
, function (request
, callbackEach
) {
36 const videoData
= request
.data
38 if (request
.type
=== 'add') {
39 addRemoteVideo(videoData
, callbackEach
)
40 } else if (request
.type
=== 'remove') {
41 removeRemoteVideo(videoData
, fromUrl
, callbackEach
)
43 logger
.error('Unkown remote request type %s.', request
.type
)
46 if (err
) logger
.error('Error managing remote videos.', { error: err
})
49 // We don't need to keep the other pod waiting
50 return res
.type('json').status(204).end()
53 function addRemoteVideo (videoToCreateData
, callback
) {
54 logger
.debug('Adding remote video %s.', videoToCreateData
.magnetUri
)
56 // Mongoose pre hook will automatically create the thumbnail on disk
57 videoToCreateData
.thumbnail
= videoToCreateData
.thumbnailBase64
59 const video
= new Video(videoToCreateData
)
63 function removeRemoteVideo (videoToRemoveData
, fromUrl
, callback
) {
64 // We need the list because we have to remove some other stuffs (thumbnail etc)
65 Video
.listByUrlAndMagnet(fromUrl
, videoToRemoveData
.magnetUri
, function (err
, videosList
) {
67 logger
.error('Cannot list videos from url and magnets.', { error: err
})
71 if (videosList
.length
=== 0) {
72 logger
.error('No remote video was found for this pod.', { magnetUri: videoToRemoveData
.magnetUri
, podUrl: fromUrl
})
75 async
.each(videosList
, function (video
, callbackEach
) {
76 logger
.debug('Removing remote video %s.', video
.magnetUri
)
78 video
.remove(callbackEach
)