]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/remote/videos.js
3 const eachSeries
= require('async/eachSeries')
4 const express
= require('express')
5 const waterfall
= require('async/waterfall')
7 const db
= require('../../../initializers/database')
8 const middlewares
= require('../../../middlewares')
9 const secureMiddleware
= middlewares
.secure
10 const videosValidators
= middlewares
.validators
.remote
.videos
11 const signatureValidators
= middlewares
.validators
.remote
.signature
12 const logger
= require('../../../helpers/logger')
13 const utils
= require('../../../helpers/utils')
15 const router
= express
.Router()
18 signatureValidators
.signature
,
19 secureMiddleware
.checkSignature
,
20 videosValidators
.remoteVideos
,
24 // ---------------------------------------------------------------------------
26 module
.exports
= router
28 // ---------------------------------------------------------------------------
30 function remoteVideos (req
, res
, next
) {
31 const requests
= req
.body
.data
32 const fromPod
= res
.locals
.secure
.pod
34 // We need to process in the same order to keep consistency
36 eachSeries(requests
, function (request
, callbackEach
) {
37 const data
= request
.data
39 switch (request
.type
) {
41 addRemoteVideoRetryWrapper(data
, fromPod
, callbackEach
)
45 updateRemoteVideoRetryWrapper(data
, fromPod
, callbackEach
)
49 removeRemoteVideo(data
, fromPod
, callbackEach
)
53 reportAbuseRemoteVideo(data
, fromPod
, callbackEach
)
57 logger
.error('Unkown remote request type %s.', request
.type
)
60 if (err
) logger
.error('Error managing remote videos.', { error: err
})
63 // We don't need to keep the other pod waiting
64 return res
.type('json').status(204).end()
67 // Handle retries on fail
68 function addRemoteVideoRetryWrapper (videoToCreateData
, fromPod
, finalCallback
) {
69 utils
.transactionRetryer(
71 return addRemoteVideo(videoToCreateData
, fromPod
, callback
)
75 logger
.error('Cannot insert the remote video with many retries.', { error: err
})
76 return finalCallback(err
)
79 return finalCallback()
84 function addRemoteVideo (videoToCreateData
, fromPod
, finalCallback
) {
85 logger
.debug('Adding remote video "%s".', videoToCreateData
.remoteId
)
89 function startTransaction (callback
) {
90 db
.sequelize
.transaction({ isolationLevel: 'SERIALIZABLE' }).asCallback(function (err
, t
) {
91 return callback(err
, t
)
95 function findOrCreateAuthor (t
, callback
) {
96 const name
= videoToCreateData
.author
97 const podId
= fromPod
.id
98 // This author is from another pod so we do not associate a user
101 db
.Author
.findOrCreateAuthor(name
, podId
, userId
, t
, function (err
, authorInstance
) {
102 return callback(err
, t
, authorInstance
)
106 function findOrCreateTags (t
, author
, callback
) {
107 const tags
= videoToCreateData
.tags
109 db
.Tag
.findOrCreateTags(tags
, t
, function (err
, tagInstances
) {
110 return callback(err
, t
, author
, tagInstances
)
114 function createVideoObject (t
, author
, tagInstances
, callback
) {
116 name: videoToCreateData
.name
,
117 remoteId: videoToCreateData
.remoteId
,
118 extname: videoToCreateData
.extname
,
119 infoHash: videoToCreateData
.infoHash
,
120 description: videoToCreateData
.description
,
122 duration: videoToCreateData
.duration
,
123 createdAt: videoToCreateData
.createdAt
,
124 // FIXME: updatedAt does not seems to be considered by Sequelize
125 updatedAt: videoToCreateData
.updatedAt
128 const video
= db
.Video
.build(videoData
)
130 return callback(null, t
, tagInstances
, video
)
133 function generateThumbnail (t
, tagInstances
, video
, callback
) {
134 db
.Video
.generateThumbnailFromData(video
, videoToCreateData
.thumbnailData
, function (err
) {
136 logger
.error('Cannot generate thumbnail from data.', { error: err
})
140 return callback(err
, t
, tagInstances
, video
)
144 function insertVideoIntoDB (t
, tagInstances
, video
, callback
) {
149 video
.save(options
).asCallback(function (err
, videoCreated
) {
150 return callback(err
, t
, tagInstances
, videoCreated
)
154 function associateTagsToVideo (t
, tagInstances
, video
, callback
) {
155 const options
= { transaction: t
}
157 video
.setTags(tagInstances
, options
).asCallback(function (err
) {
158 return callback(err
, t
)
162 ], function (err
, t
) {
164 // This is just a debug because we will retry the insert
165 logger
.debug('Cannot insert the remote video.', { error: err
})
167 // Abort transaction?
170 return finalCallback(err
)
173 // Commit transaction
176 return finalCallback()
180 // Handle retries on fail
181 function updateRemoteVideoRetryWrapper (videoAttributesToUpdate
, fromPod
, finalCallback
) {
182 utils
.transactionRetryer(
183 function (callback
) {
184 return updateRemoteVideo(videoAttributesToUpdate
, fromPod
, callback
)
188 logger
.error('Cannot update the remote video with many retries.', { error: err
})
189 return finalCallback(err
)
192 return finalCallback()
197 function updateRemoteVideo (videoAttributesToUpdate
, fromPod
, finalCallback
) {
198 logger
.debug('Updating remote video "%s".', videoAttributesToUpdate
.remoteId
)
202 function startTransaction (callback
) {
203 db
.sequelize
.transaction().asCallback(function (err
, t
) {
204 return callback(err
, t
)
208 function findVideo (t
, callback
) {
209 fetchVideo(fromPod
.host
, videoAttributesToUpdate
.remoteId
, function (err
, videoInstance
) {
210 return callback(err
, t
, videoInstance
)
214 function findOrCreateTags (t
, videoInstance
, callback
) {
215 const tags
= videoAttributesToUpdate
.tags
217 db
.Tag
.findOrCreateTags(tags
, t
, function (err
, tagInstances
) {
218 return callback(err
, t
, videoInstance
, tagInstances
)
222 function updateVideoIntoDB (t
, videoInstance
, tagInstances
, callback
) {
223 const options
= { transaction: t
}
225 videoInstance
.set('name', videoAttributesToUpdate
.name
)
226 videoInstance
.set('description', videoAttributesToUpdate
.description
)
227 videoInstance
.set('infoHash', videoAttributesToUpdate
.infoHash
)
228 videoInstance
.set('duration', videoAttributesToUpdate
.duration
)
229 videoInstance
.set('createdAt', videoAttributesToUpdate
.createdAt
)
230 videoInstance
.set('updatedAt', videoAttributesToUpdate
.updatedAt
)
231 videoInstance
.set('extname', videoAttributesToUpdate
.extname
)
233 videoInstance
.save(options
).asCallback(function (err
) {
234 return callback(err
, t
, videoInstance
, tagInstances
)
238 function associateTagsToVideo (t
, videoInstance
, tagInstances
, callback
) {
239 const options
= { transaction: t
}
241 videoInstance
.setTags(tagInstances
, options
).asCallback(function (err
) {
242 return callback(err
, t
)
246 ], function (err
, t
) {
248 // This is just a debug because we will retry the insert
249 logger
.debug('Cannot update the remote video.', { error: err
})
251 // Abort transaction?
254 return finalCallback(err
)
257 // Commit transaction
260 return finalCallback()
264 function removeRemoteVideo (videoToRemoveData
, fromPod
, callback
) {
265 // We need the instance because we have to remove some other stuffs (thumbnail etc)
266 fetchVideo(fromPod
.host
, videoToRemoveData
.remoteId
, function (err
, video
) {
267 if (err
) return callback(err
)
269 logger
.debug('Removing remote video %s.', video
.remoteId
)
270 video
.destroy().asCallback(callback
)
274 function reportAbuseRemoteVideo (reportData
, fromPod
, callback
) {
275 db
.Video
.load(reportData
.videoRemoteId
, function (err
, video
) {
277 if (!err
) err
= new Error('video not found')
279 logger
.error('Cannot load video from id.', { error: err
, id: reportData
.videoRemoteId
})
283 logger
.debug('Reporting remote abuse for video %s.', video
.id
)
285 const videoAbuseData
= {
286 reporterUsername: reportData
.reporterUsername
,
287 reason: reportData
.reportReason
,
288 reporterPodId: fromPod
.id
,
292 db
.VideoAbuse
.create(videoAbuseData
).asCallback(callback
)
296 function fetchVideo (podHost
, remoteId
, callback
) {
297 db
.Video
.loadByHostAndRemoteId(podHost
, remoteId
, function (err
, video
) {
299 if (!err
) err
= new Error('video not found')
301 logger
.error('Cannot load video from host and remote id.', { error: err
, podHost
, remoteId
})
305 return callback(null, video
)