]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/remote.js
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
98 db
.Author
.findOrCreate(query
).asCallback(function (err
, result
) {
99 // [ instance, wasCreated ]
100 return callback(err
, t
, result
[0])
104 function findOrCreateTags (t
, author
, callback
) {
105 const tags
= videoToCreateData
.tags
106 const tagInstances
= []
108 each(tags
, function (tag
, callbackEach
) {
119 db
.Tag
.findOrCreate(query
).asCallback(function (err
, res
) {
120 if (err
) return callbackEach(err
)
122 // res = [ tag, isCreated ]
124 tagInstances
.push(tag
)
125 return callbackEach()
128 return callback(err
, t
, author
, tagInstances
)
132 function createVideoObject (t
, author
, tagInstances
, callback
) {
134 name: videoToCreateData
.name
,
135 remoteId: videoToCreateData
.remoteId
,
136 extname: videoToCreateData
.extname
,
137 infoHash: videoToCreateData
.infoHash
,
138 description: videoToCreateData
.description
,
140 duration: videoToCreateData
.duration
,
141 createdAt: videoToCreateData
.createdAt
144 const video
= db
.Video
.build(videoData
)
146 return callback(null, t
, tagInstances
, video
)
149 function generateThumbnail (t
, tagInstances
, video
, callback
) {
150 db
.Video
.generateThumbnailFromData(video
, videoToCreateData
.thumbnailData
, function (err
) {
152 logger
.error('Cannot generate thumbnail from data.', { error: err
})
156 return callback(err
, t
, tagInstances
, video
)
160 function insertVideoIntoDB (t
, tagInstances
, video
, callback
) {
165 video
.save(options
).asCallback(function (err
, videoCreated
) {
166 return callback(err
, t
, tagInstances
, videoCreated
)
170 function associateTagsToVideo (t
, tagInstances
, video
, callback
) {
171 const options
= { transaction: t
}
173 video
.setTags(tagInstances
, options
).asCallback(function (err
) {
174 return callback(err
, t
)
178 ], function (err
, t
) {
180 logger
.error('Cannot insert the remote video.')
182 // Abort transaction?
185 return finalCallback(err
)
188 // Commit transaction
191 return finalCallback()
195 function removeRemoteVideo (videoToRemoveData
, fromHost
, callback
) {
196 // TODO: use bulkDestroy?
198 // We need the list because we have to remove some other stuffs (thumbnail etc)
199 db
.Video
.listByHostAndRemoteId(fromHost
, videoToRemoveData
.remoteId
, function (err
, videosList
) {
201 logger
.error('Cannot list videos from host and remote id.', { error: err
.message
})
205 if (videosList
.length
=== 0) {
206 logger
.error('No remote video was found for this pod.', { remoteId: videoToRemoveData
.remoteId
, podHost: fromHost
})
209 each(videosList
, function (video
, callbackEach
) {
210 logger
.debug('Removing remote video %s.', video
.remoteId
)
212 video
.destroy().asCallback(callbackEach
)