]>
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 fromPod
= res
.locals
.secure
.pod
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
, fromPod
, callbackEach
)
40 } else if (request
.type
=== 'remove') {
41 removeRemoteVideo(videoData
, fromPod
, 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
, fromPod
, 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 findOrCreateAuthor (t
, callback
) {
65 const name
= videoToCreateData
.author
66 const podId
= fromPod
.id
67 // This author is from another pod so we do not associate a user
70 db
.Author
.findOrCreateAuthor(name
, podId
, userId
, t
, function (err
, authorInstance
) {
71 return callback(err
, t
, authorInstance
)
75 function findOrCreateTags (t
, author
, callback
) {
76 const tags
= videoToCreateData
.tags
78 db
.Tag
.findOrCreateTags(tags
, t
, function (err
, tagInstances
) {
79 return callback(err
, t
, author
, tagInstances
)
83 function createVideoObject (t
, author
, tagInstances
, callback
) {
85 name: videoToCreateData
.name
,
86 remoteId: videoToCreateData
.remoteId
,
87 extname: videoToCreateData
.extname
,
88 infoHash: videoToCreateData
.infoHash
,
89 description: videoToCreateData
.description
,
91 duration: videoToCreateData
.duration
,
92 createdAt: videoToCreateData
.createdAt
95 const video
= db
.Video
.build(videoData
)
97 return callback(null, t
, tagInstances
, video
)
100 function generateThumbnail (t
, tagInstances
, video
, callback
) {
101 db
.Video
.generateThumbnailFromData(video
, videoToCreateData
.thumbnailData
, function (err
) {
103 logger
.error('Cannot generate thumbnail from data.', { error: err
})
107 return callback(err
, t
, tagInstances
, video
)
111 function insertVideoIntoDB (t
, tagInstances
, video
, callback
) {
116 video
.save(options
).asCallback(function (err
, videoCreated
) {
117 return callback(err
, t
, tagInstances
, videoCreated
)
121 function associateTagsToVideo (t
, tagInstances
, video
, callback
) {
122 const options
= { transaction: t
}
124 video
.setTags(tagInstances
, options
).asCallback(function (err
) {
125 return callback(err
, t
)
129 ], function (err
, t
) {
131 logger
.error('Cannot insert the remote video.')
133 // Abort transaction?
136 return finalCallback(err
)
139 // Commit transaction
142 return finalCallback()
146 function removeRemoteVideo (videoToRemoveData
, fromPod
, callback
) {
147 // TODO: use bulkDestroy?
149 // We need the list because we have to remove some other stuffs (thumbnail etc)
150 db
.Video
.listByHostAndRemoteId(fromPod
.host
, videoToRemoveData
.remoteId
, function (err
, videosList
) {
152 logger
.error('Cannot list videos from host and remote id.', { error: err
.message
})
156 if (videosList
.length
=== 0) {
157 logger
.error('No remote video was found for this pod.', { remoteId: videoToRemoveData
.remoteId
, podHost: fromPod
.host
})
160 each(videosList
, function (video
, callbackEach
) {
161 logger
.debug('Removing remote video %s.', video
.remoteId
)
163 video
.destroy().asCallback(callbackEach
)