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 databaseUtils
= require('../../../helpers/database-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
) {
70 arguments: [ videoToCreateData
, fromPod
],
71 errorMessage: 'Cannot insert the remote video with many retries.'
74 databaseUtils
.retryTransactionWrapper(addRemoteVideo
, options
, finalCallback
)
77 function addRemoteVideo (videoToCreateData
, fromPod
, finalCallback
) {
78 logger
.debug('Adding remote video "%s".', videoToCreateData
.remoteId
)
82 databaseUtils
.startSerializableTransaction
,
84 function findOrCreateAuthor (t
, callback
) {
85 const name
= videoToCreateData
.author
86 const podId
= fromPod
.id
87 // This author is from another pod so we do not associate a user
90 db
.Author
.findOrCreateAuthor(name
, podId
, userId
, t
, function (err
, authorInstance
) {
91 return callback(err
, t
, authorInstance
)
95 function findOrCreateTags (t
, author
, callback
) {
96 const tags
= videoToCreateData
.tags
98 db
.Tag
.findOrCreateTags(tags
, t
, function (err
, tagInstances
) {
99 return callback(err
, t
, author
, tagInstances
)
103 function createVideoObject (t
, author
, tagInstances
, callback
) {
105 name: videoToCreateData
.name
,
106 remoteId: videoToCreateData
.remoteId
,
107 extname: videoToCreateData
.extname
,
108 infoHash: videoToCreateData
.infoHash
,
109 description: videoToCreateData
.description
,
111 duration: videoToCreateData
.duration
,
112 createdAt: videoToCreateData
.createdAt
,
113 // FIXME: updatedAt does not seems to be considered by Sequelize
114 updatedAt: videoToCreateData
.updatedAt
117 const video
= db
.Video
.build(videoData
)
119 return callback(null, t
, tagInstances
, video
)
122 function generateThumbnail (t
, tagInstances
, video
, callback
) {
123 db
.Video
.generateThumbnailFromData(video
, videoToCreateData
.thumbnailData
, function (err
) {
125 logger
.error('Cannot generate thumbnail from data.', { error: err
})
129 return callback(err
, t
, tagInstances
, video
)
133 function insertVideoIntoDB (t
, tagInstances
, video
, callback
) {
138 video
.save(options
).asCallback(function (err
, videoCreated
) {
139 return callback(err
, t
, tagInstances
, videoCreated
)
143 function associateTagsToVideo (t
, tagInstances
, video
, callback
) {
144 const options
= { transaction: t
}
146 video
.setTags(tagInstances
, options
).asCallback(function (err
) {
147 return callback(err
, t
)
151 databaseUtils
.commitTransaction
153 ], function (err
, t
) {
155 // This is just a debug because we will retry the insert
156 logger
.debug('Cannot insert the remote video.', { error: err
})
157 return databaseUtils
.rollbackTransaction(err
, t
, finalCallback
)
160 logger
.info('Remote video %s inserted.', videoToCreateData
.name
)
161 return finalCallback(null)
165 // Handle retries on fail
166 function updateRemoteVideoRetryWrapper (videoAttributesToUpdate
, fromPod
, finalCallback
) {
168 arguments: [ videoAttributesToUpdate
, fromPod
],
169 errorMessage: 'Cannot update the remote video with many retries'
172 databaseUtils
.retryTransactionWrapper(updateRemoteVideo
, options
, finalCallback
)
175 function updateRemoteVideo (videoAttributesToUpdate
, fromPod
, finalCallback
) {
176 logger
.debug('Updating remote video "%s".', videoAttributesToUpdate
.remoteId
)
180 databaseUtils
.startSerializableTransaction
,
182 function findVideo (t
, callback
) {
183 fetchVideo(fromPod
.host
, videoAttributesToUpdate
.remoteId
, function (err
, videoInstance
) {
184 return callback(err
, t
, videoInstance
)
188 function findOrCreateTags (t
, videoInstance
, callback
) {
189 const tags
= videoAttributesToUpdate
.tags
191 db
.Tag
.findOrCreateTags(tags
, t
, function (err
, tagInstances
) {
192 return callback(err
, t
, videoInstance
, tagInstances
)
196 function updateVideoIntoDB (t
, videoInstance
, tagInstances
, callback
) {
197 const options
= { transaction: t
}
199 videoInstance
.set('name', videoAttributesToUpdate
.name
)
200 videoInstance
.set('description', videoAttributesToUpdate
.description
)
201 videoInstance
.set('infoHash', videoAttributesToUpdate
.infoHash
)
202 videoInstance
.set('duration', videoAttributesToUpdate
.duration
)
203 videoInstance
.set('createdAt', videoAttributesToUpdate
.createdAt
)
204 videoInstance
.set('updatedAt', videoAttributesToUpdate
.updatedAt
)
205 videoInstance
.set('extname', videoAttributesToUpdate
.extname
)
207 videoInstance
.save(options
).asCallback(function (err
) {
208 return callback(err
, t
, videoInstance
, tagInstances
)
212 function associateTagsToVideo (t
, videoInstance
, tagInstances
, callback
) {
213 const options
= { transaction: t
}
215 videoInstance
.setTags(tagInstances
, options
).asCallback(function (err
) {
216 return callback(err
, t
)
220 databaseUtils
.commitTransaction
222 ], function (err
, t
) {
224 // This is just a debug because we will retry the insert
225 logger
.debug('Cannot update the remote video.', { error: err
})
226 return databaseUtils
.rollbackTransaction(err
, t
, finalCallback
)
229 logger
.info('Remote video %s updated', videoAttributesToUpdate
.name
)
230 return finalCallback(null)
234 function removeRemoteVideo (videoToRemoveData
, fromPod
, callback
) {
235 // We need the instance because we have to remove some other stuffs (thumbnail etc)
236 fetchVideo(fromPod
.host
, videoToRemoveData
.remoteId
, function (err
, video
) {
237 // Do not return the error, continue the process
238 if (err
) return callback(null)
240 logger
.debug('Removing remote video %s.', video
.remoteId
)
241 video
.destroy().asCallback(function (err
) {
242 // Do not return the error, continue the process
244 logger
.error('Cannot remove remote video with id %s.', videoToRemoveData
.remoteId
, { error: err
})
247 return callback(null)
252 function reportAbuseRemoteVideo (reportData
, fromPod
, callback
) {
253 db
.Video
.load(reportData
.videoRemoteId
, function (err
, video
) {
255 if (!err
) err
= new Error('video not found')
257 logger
.error('Cannot load video from id.', { error: err
, id: reportData
.videoRemoteId
})
258 // Do not return the error, continue the process
259 return callback(null)
262 logger
.debug('Reporting remote abuse for video %s.', video
.id
)
264 const videoAbuseData
= {
265 reporterUsername: reportData
.reporterUsername
,
266 reason: reportData
.reportReason
,
267 reporterPodId: fromPod
.id
,
271 db
.VideoAbuse
.create(videoAbuseData
).asCallback(function (err
) {
273 logger
.error('Cannot create remote abuse video.', { error: err
})
276 return callback(null)
281 function fetchVideo (podHost
, remoteId
, callback
) {
282 db
.Video
.loadByHostAndRemoteId(podHost
, remoteId
, function (err
, video
) {
284 if (!err
) err
= new Error('video not found')
286 logger
.error('Cannot load video from host and remote id.', { error: err
, podHost
, remoteId
})
290 return callback(null, video
)