]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/remote.js
a36c31c38c897ff0f1d03da5d92e2401c93bf85f
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 switch (request
.type
) {
40 addRemoteVideo(videoData
, fromPod
, callbackEach
)
44 updateRemoteVideo(videoData
, fromPod
, callbackEach
)
48 removeRemoteVideo(videoData
, fromPod
, callbackEach
)
52 logger
.error('Unkown remote request type %s.', request
.type
)
55 if (err
) logger
.error('Error managing remote videos.', { error: err
})
58 // We don't need to keep the other pod waiting
59 return res
.type('json').status(204).end()
62 function addRemoteVideo (videoToCreateData
, fromPod
, finalCallback
) {
63 logger
.debug('Adding remote video "%s".', videoToCreateData
.name
)
67 function startTransaction (callback
) {
68 db
.sequelize
.transaction().asCallback(function (err
, t
) {
69 return callback(err
, t
)
73 function findOrCreateAuthor (t
, callback
) {
74 const name
= videoToCreateData
.author
75 const podId
= fromPod
.id
76 // This author is from another pod so we do not associate a user
79 db
.Author
.findOrCreateAuthor(name
, podId
, userId
, t
, function (err
, authorInstance
) {
80 return callback(err
, t
, authorInstance
)
84 function findOrCreateTags (t
, author
, callback
) {
85 const tags
= videoToCreateData
.tags
87 db
.Tag
.findOrCreateTags(tags
, t
, function (err
, tagInstances
) {
88 return callback(err
, t
, author
, tagInstances
)
92 function createVideoObject (t
, author
, tagInstances
, callback
) {
94 name: videoToCreateData
.name
,
95 remoteId: videoToCreateData
.remoteId
,
96 extname: videoToCreateData
.extname
,
97 infoHash: videoToCreateData
.infoHash
,
98 description: videoToCreateData
.description
,
100 duration: videoToCreateData
.duration
,
101 createdAt: videoToCreateData
.createdAt
,
102 updatedAt: videoToCreateData
.updatedAt
105 const video
= db
.Video
.build(videoData
)
107 return callback(null, t
, tagInstances
, video
)
110 function generateThumbnail (t
, tagInstances
, video
, callback
) {
111 db
.Video
.generateThumbnailFromData(video
, videoToCreateData
.thumbnailData
, function (err
) {
113 logger
.error('Cannot generate thumbnail from data.', { error: err
})
117 return callback(err
, t
, tagInstances
, video
)
121 function insertVideoIntoDB (t
, tagInstances
, video
, callback
) {
126 video
.save(options
).asCallback(function (err
, videoCreated
) {
127 return callback(err
, t
, tagInstances
, videoCreated
)
131 function associateTagsToVideo (t
, tagInstances
, video
, callback
) {
132 const options
= { transaction: t
}
134 video
.setTags(tagInstances
, options
).asCallback(function (err
) {
135 return callback(err
, t
)
139 ], function (err
, t
) {
141 logger
.error('Cannot insert the remote video.')
143 // Abort transaction?
146 return finalCallback(err
)
149 // Commit transaction
152 return finalCallback()
156 function updateRemoteVideo (videoAttributesToUpdate
, fromPod
, finalCallback
) {
157 logger
.debug('Updating remote video "%s".', videoAttributesToUpdate
.name
)
161 function startTransaction (callback
) {
162 db
.sequelize
.transaction().asCallback(function (err
, t
) {
163 return callback(err
, t
)
167 function findVideo (t
, callback
) {
168 db
.Video
.loadByHostAndRemoteId(fromPod
.host
, videoAttributesToUpdate
.remoteId
, function (err
, videoInstance
) {
169 if (err
|| !videoInstance
) {
170 logger
.error('Cannot load video from host and remote id.', { error: err
.message
})
174 return callback(null, t
, videoInstance
)
178 function findOrCreateTags (t
, videoInstance
, callback
) {
179 const tags
= videoAttributesToUpdate
.tags
181 db
.Tag
.findOrCreateTags(tags
, t
, function (err
, tagInstances
) {
182 return callback(err
, t
, videoInstance
, tagInstances
)
186 function updateVideoIntoDB (t
, videoInstance
, tagInstances
, callback
) {
187 const options
= { transaction: t
}
189 videoInstance
.set('name', videoAttributesToUpdate
.name
)
190 videoInstance
.set('description', videoAttributesToUpdate
.description
)
191 videoInstance
.set('infoHash', videoAttributesToUpdate
.infoHash
)
192 videoInstance
.set('duration', videoAttributesToUpdate
.duration
)
193 videoInstance
.set('createdAt', videoAttributesToUpdate
.createdAt
)
194 videoInstance
.set('updatedAt', videoAttributesToUpdate
.updatedAt
)
195 videoInstance
.set('extname', videoAttributesToUpdate
.extname
)
197 videoInstance
.save(options
).asCallback(function (err
) {
198 return callback(err
, t
, videoInstance
, tagInstances
)
202 function associateTagsToVideo (t
, videoInstance
, tagInstances
, callback
) {
203 const options
= { transaction: t
}
205 videoInstance
.setTags(tagInstances
, options
).asCallback(function (err
) {
206 return callback(err
, t
)
210 ], function (err
, t
) {
212 logger
.error('Cannot update the remote video.')
214 // Abort transaction?
217 return finalCallback(err
)
220 // Commit transaction
223 return finalCallback()
227 function removeRemoteVideo (videoToRemoveData
, fromPod
, callback
) {
228 // We need the instance because we have to remove some other stuffs (thumbnail etc)
229 db
.Video
.loadByHostAndRemoteId(fromPod
.host
, videoToRemoveData
.remoteId
, function (err
, video
) {
231 logger
.error('Cannot load video from host and remote id.', { error: err
.message
})
235 logger
.debug('Removing remote video %s.', video
.remoteId
)
236 video
.destroy().asCallback(callback
)