]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/remote.js
d856576a9f2ae880c4f134bc4f9b3750566e0d41
3 const each
= require('async/each')
4 const eachSeries
= require('async/eachSeries')
5 const express
= require('express')
6 const waterfall
= require('async/waterfall')
8 const db
= require('../../initializers/database')
9 const middlewares
= require('../../middlewares')
10 const secureMiddleware
= middlewares
.secure
11 const validators
= middlewares
.validators
.remote
12 const logger
= require('../../helpers/logger')
14 const router
= express
.Router()
16 router
.post('/videos',
18 secureMiddleware
.checkSignature
,
19 validators
.remoteVideos
,
23 // ---------------------------------------------------------------------------
25 module
.exports
= router
27 // ---------------------------------------------------------------------------
29 function remoteVideos (req
, res
, next
) {
30 const requests
= req
.body
.data
31 const fromHost
= req
.body
.signature
.host
33 // We need to process in the same order to keep consistency
35 eachSeries(requests
, function (request
, callbackEach
) {
36 const videoData
= request
.data
38 if (request
.type
=== 'add') {
39 addRemoteVideo(videoData
, fromHost
, callbackEach
)
40 } else if (request
.type
=== 'remove') {
41 removeRemoteVideo(videoData
, fromHost
, 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
, fromHost
, callback
) {
54 logger
.debug('Adding remote video "%s".', videoToCreateData
.name
)
58 function findOrCreatePod (callback
) {
70 db
.Pod
.findOrCreate(query
).asCallback(function (err
, result
) {
71 // [ instance, wasCreated ]
72 return callback(err
, result
[0])
76 function findOrCreateAuthor (pod
, callback
) {
77 const username
= videoToCreateData
.author
90 db
.Author
.findOrCreate(query
).asCallback(function (err
, result
) {
91 // [ instance, wasCreated ]
92 return callback(err
, result
[0])
96 function createVideoObject (author
, callback
) {
98 name: videoToCreateData
.name
,
99 remoteId: videoToCreateData
.remoteId
,
100 extname: videoToCreateData
.extname
,
101 infoHash: videoToCreateData
.infoHash
,
102 description: videoToCreateData
.description
,
104 duration: videoToCreateData
.duration
,
105 tags: videoToCreateData
.tags
108 const video
= db
.Video
.build(videoData
)
110 return callback(null, video
)
113 function generateThumbnail (video
, callback
) {
114 db
.Video
.generateThumbnailFromBase64(video
, videoToCreateData
.thumbnailBase64
, function (err
) {
116 logger
.error('Cannot generate thumbnail from base 64 data.', { error: err
})
120 video
.save().asCallback(callback
)
124 function insertIntoDB (video
, callback
) {
125 video
.save().asCallback(callback
)
131 function removeRemoteVideo (videoToRemoveData
, fromHost
, callback
) {
132 // TODO: use bulkDestroy?
134 // We need the list because we have to remove some other stuffs (thumbnail etc)
135 db
.Video
.listByHostAndRemoteId(fromHost
, videoToRemoveData
.remoteId
, function (err
, videosList
) {
137 logger
.error('Cannot list videos from host and remote id.', { error: err
.message
})
141 if (videosList
.length
=== 0) {
142 logger
.error('No remote video was found for this pod.', { remoteId: videoToRemoveData
.remoteId
, podHost: fromHost
})
145 each(videosList
, function (video
, callbackEach
) {
146 logger
.debug('Removing remote video %s.', video
.remoteId
)
148 video
.destroy().asCallback(callbackEach
)