]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/remote.js
2cf916ff3ac63b968eb843fc9454311a5e42c853
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
, finalCallback
) {
54 logger
.debug('Adding remote video "%s".', videoToCreateData
.name
)
58 function startTransaction (callback
) {
59 db
.sequelize
.transaction().asCallback(function (err
, t
) {
60 return callback(err
, t
)
64 function findOrCreatePod (t
, callback
) {
75 db
.Pod
.findOrCreate(query
).asCallback(function (err
, result
) {
76 // [ instance, wasCreated ]
77 return callback(err
, t
, result
[0])
81 function findOrCreateAuthor (t
, pod
, callback
) {
82 const username
= videoToCreateData
.author
96 db
.Author
.findOrCreate(query
).asCallback(function (err
, result
) {
97 // [ instance, wasCreated ]
98 return callback(err
, t
, result
[0])
102 function findOrCreateTags (t
, author
, callback
) {
103 const tags
= videoToCreateData
.tags
104 const tagInstances
= []
106 each(tags
, function (tag
, callbackEach
) {
117 db
.Tag
.findOrCreate(query
).asCallback(function (err
, res
) {
118 if (err
) return callbackEach(err
)
120 // res = [ tag, isCreated ]
122 tagInstances
.push(tag
)
123 return callbackEach()
126 return callback(err
, t
, author
, tagInstances
)
130 function createVideoObject (t
, author
, tagInstances
, callback
) {
132 name: videoToCreateData
.name
,
133 remoteId: videoToCreateData
.remoteId
,
134 extname: videoToCreateData
.extname
,
135 infoHash: videoToCreateData
.infoHash
,
136 description: videoToCreateData
.description
,
138 duration: videoToCreateData
.duration
,
139 createdAt: videoToCreateData
.createdAt
142 const video
= db
.Video
.build(videoData
)
144 return callback(null, t
, tagInstances
, video
)
147 function generateThumbnail (t
, tagInstances
, video
, callback
) {
148 db
.Video
.generateThumbnailFromBase64(video
, videoToCreateData
.thumbnailBase64
, function (err
) {
150 logger
.error('Cannot generate thumbnail from base 64 data.', { error: err
})
154 return callback(err
, t
, tagInstances
, video
)
158 function insertVideoIntoDB (t
, tagInstances
, video
, callback
) {
163 video
.save(options
).asCallback(function (err
, videoCreated
) {
164 return callback(err
, t
, tagInstances
, videoCreated
)
168 function associateTagsToVideo (t
, tagInstances
, video
, callback
) {
169 const options
= { transaction: t
}
171 video
.setTags(tagInstances
, options
).asCallback(function (err
) {
172 return callback(err
, t
)
176 ], function (err
, t
) {
178 logger
.error('Cannot insert the remote video.')
180 // Abort transaction?
183 return finalCallback(err
)
186 // Commit transaction
189 return finalCallback()
193 function removeRemoteVideo (videoToRemoveData
, fromHost
, callback
) {
194 // TODO: use bulkDestroy?
196 // We need the list because we have to remove some other stuffs (thumbnail etc)
197 db
.Video
.listByHostAndRemoteId(fromHost
, videoToRemoveData
.remoteId
, function (err
, videosList
) {
199 logger
.error('Cannot list videos from host and remote id.', { error: err
.message
})
203 if (videosList
.length
=== 0) {
204 logger
.error('No remote video was found for this pod.', { remoteId: videoToRemoveData
.remoteId
, podHost: fromHost
})
207 each(videosList
, function (video
, callbackEach
) {
208 logger
.debug('Removing remote video %s.', video
.remoteId
)
210 video
.destroy().asCallback(callbackEach
)