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 constants
= require('../../../initializers/constants')
9 const middlewares
= require('../../../middlewares')
10 const secureMiddleware
= middlewares
.secure
11 const videosValidators
= middlewares
.validators
.remote
.videos
12 const signatureValidators
= middlewares
.validators
.remote
.signature
13 const logger
= require('../../../helpers/logger')
14 const databaseUtils
= require('../../../helpers/database-utils')
16 const ENDPOINT_ACTIONS
= constants
.REQUEST_ENDPOINT_ACTIONS
[constants
.REQUEST_ENDPOINTS
.VIDEOS
]
18 // Functions to call when processing a remote request
19 const functionsHash
= {}
20 functionsHash
[ENDPOINT_ACTIONS
.ADD
] = addRemoteVideoRetryWrapper
21 functionsHash
[ENDPOINT_ACTIONS
.UPDATE
] = updateRemoteVideoRetryWrapper
22 functionsHash
[ENDPOINT_ACTIONS
.REMOVE
] = removeRemoteVideo
23 functionsHash
[ENDPOINT_ACTIONS
.REPORT_ABUSE
] = reportAbuseRemoteVideo
25 const router
= express
.Router()
28 signatureValidators
.signature
,
29 secureMiddleware
.checkSignature
,
30 videosValidators
.remoteVideos
,
34 // ---------------------------------------------------------------------------
36 module
.exports
= router
38 // ---------------------------------------------------------------------------
40 function remoteVideos (req
, res
, next
) {
41 const requests
= req
.body
.data
42 const fromPod
= res
.locals
.secure
.pod
44 // We need to process in the same order to keep consistency
46 eachSeries(requests
, function (request
, callbackEach
) {
47 const data
= request
.data
49 // Get the function we need to call in order to process the request
50 const fun
= functionsHash
[request
.type
]
51 if (fun
=== undefined) {
52 logger
.error('Unkown remote request type %s.', request
.type
)
53 return callbackEach(null)
56 fun
.call(this, data
, fromPod
, callbackEach
)
58 if (err
) logger
.error('Error managing remote videos.', { error: err
})
61 // We don't need to keep the other pod waiting
62 return res
.type('json').status(204).end()
65 // Handle retries on fail
66 function addRemoteVideoRetryWrapper (videoToCreateData
, fromPod
, finalCallback
) {
68 arguments: [ videoToCreateData
, fromPod
],
69 errorMessage: 'Cannot insert the remote video with many retries.'
72 databaseUtils
.retryTransactionWrapper(addRemoteVideo
, options
, finalCallback
)
75 function addRemoteVideo (videoToCreateData
, fromPod
, finalCallback
) {
76 logger
.debug('Adding remote video "%s".', videoToCreateData
.remoteId
)
80 databaseUtils
.startSerializableTransaction
,
82 function assertRemoteIdAndHostUnique (t
, callback
) {
83 db
.Video
.loadByHostAndRemoteId(fromPod
.host
, videoToCreateData
.remoteId
, function (err
, video
) {
84 if (err
) return callback(err
)
86 if (video
) return callback(new Error('RemoteId and host pair is not unique.'))
88 return callback(null, t
)
92 function findOrCreateAuthor (t
, callback
) {
93 const name
= videoToCreateData
.author
94 const podId
= fromPod
.id
95 // This author is from another pod so we do not associate a user
98 db
.Author
.findOrCreateAuthor(name
, podId
, userId
, t
, function (err
, authorInstance
) {
99 return callback(err
, t
, authorInstance
)
103 function findOrCreateTags (t
, author
, callback
) {
104 const tags
= videoToCreateData
.tags
106 db
.Tag
.findOrCreateTags(tags
, t
, function (err
, tagInstances
) {
107 return callback(err
, t
, author
, tagInstances
)
111 function createVideoObject (t
, author
, tagInstances
, callback
) {
113 name: videoToCreateData
.name
,
114 remoteId: videoToCreateData
.remoteId
,
115 extname: videoToCreateData
.extname
,
116 infoHash: videoToCreateData
.infoHash
,
117 description: videoToCreateData
.description
,
119 duration: videoToCreateData
.duration
,
120 createdAt: videoToCreateData
.createdAt
,
121 // FIXME: updatedAt does not seems to be considered by Sequelize
122 updatedAt: videoToCreateData
.updatedAt
125 const video
= db
.Video
.build(videoData
)
127 return callback(null, t
, tagInstances
, video
)
130 function generateThumbnail (t
, tagInstances
, video
, callback
) {
131 db
.Video
.generateThumbnailFromData(video
, videoToCreateData
.thumbnailData
, function (err
) {
133 logger
.error('Cannot generate thumbnail from data.', { error: err
})
137 return callback(err
, t
, tagInstances
, video
)
141 function insertVideoIntoDB (t
, tagInstances
, video
, callback
) {
146 video
.save(options
).asCallback(function (err
, videoCreated
) {
147 return callback(err
, t
, tagInstances
, videoCreated
)
151 function associateTagsToVideo (t
, tagInstances
, video
, callback
) {
156 video
.setTags(tagInstances
, options
).asCallback(function (err
) {
157 return callback(err
, t
)
161 databaseUtils
.commitTransaction
163 ], function (err
, t
) {
165 // This is just a debug because we will retry the insert
166 logger
.debug('Cannot insert the remote video.', { error: err
})
167 return databaseUtils
.rollbackTransaction(err
, t
, finalCallback
)
170 logger
.info('Remote video %s inserted.', videoToCreateData
.name
)
171 return finalCallback(null)
175 // Handle retries on fail
176 function updateRemoteVideoRetryWrapper (videoAttributesToUpdate
, fromPod
, finalCallback
) {
178 arguments: [ videoAttributesToUpdate
, fromPod
],
179 errorMessage: 'Cannot update the remote video with many retries'
182 databaseUtils
.retryTransactionWrapper(updateRemoteVideo
, options
, finalCallback
)
185 function updateRemoteVideo (videoAttributesToUpdate
, fromPod
, finalCallback
) {
186 logger
.debug('Updating remote video "%s".', videoAttributesToUpdate
.remoteId
)
190 databaseUtils
.startSerializableTransaction
,
192 function findVideo (t
, callback
) {
193 fetchVideo(fromPod
.host
, videoAttributesToUpdate
.remoteId
, function (err
, videoInstance
) {
194 return callback(err
, t
, videoInstance
)
198 function findOrCreateTags (t
, videoInstance
, callback
) {
199 const tags
= videoAttributesToUpdate
.tags
201 db
.Tag
.findOrCreateTags(tags
, t
, function (err
, tagInstances
) {
202 return callback(err
, t
, videoInstance
, tagInstances
)
206 function updateVideoIntoDB (t
, videoInstance
, tagInstances
, callback
) {
207 const options
= { transaction: t
}
209 videoInstance
.set('name', videoAttributesToUpdate
.name
)
210 videoInstance
.set('description', videoAttributesToUpdate
.description
)
211 videoInstance
.set('infoHash', videoAttributesToUpdate
.infoHash
)
212 videoInstance
.set('duration', videoAttributesToUpdate
.duration
)
213 videoInstance
.set('createdAt', videoAttributesToUpdate
.createdAt
)
214 videoInstance
.set('updatedAt', videoAttributesToUpdate
.updatedAt
)
215 videoInstance
.set('extname', videoAttributesToUpdate
.extname
)
217 videoInstance
.save(options
).asCallback(function (err
) {
218 return callback(err
, t
, videoInstance
, tagInstances
)
222 function associateTagsToVideo (t
, videoInstance
, tagInstances
, callback
) {
223 const options
= { transaction: t
}
225 videoInstance
.setTags(tagInstances
, options
).asCallback(function (err
) {
226 return callback(err
, t
)
230 databaseUtils
.commitTransaction
232 ], function (err
, t
) {
234 // This is just a debug because we will retry the insert
235 logger
.debug('Cannot update the remote video.', { error: err
})
236 return databaseUtils
.rollbackTransaction(err
, t
, finalCallback
)
239 logger
.info('Remote video %s updated', videoAttributesToUpdate
.name
)
240 return finalCallback(null)
244 function removeRemoteVideo (videoToRemoveData
, fromPod
, callback
) {
245 // We need the instance because we have to remove some other stuffs (thumbnail etc)
246 fetchVideo(fromPod
.host
, videoToRemoveData
.remoteId
, function (err
, video
) {
247 // Do not return the error, continue the process
248 if (err
) return callback(null)
250 logger
.debug('Removing remote video %s.', video
.remoteId
)
251 video
.destroy().asCallback(function (err
) {
252 // Do not return the error, continue the process
254 logger
.error('Cannot remove remote video with id %s.', videoToRemoveData
.remoteId
, { error: err
})
257 return callback(null)
262 function reportAbuseRemoteVideo (reportData
, fromPod
, callback
) {
263 db
.Video
.load(reportData
.videoRemoteId
, function (err
, video
) {
265 if (!err
) err
= new Error('video not found')
267 logger
.error('Cannot load video from id.', { error: err
, id: reportData
.videoRemoteId
})
268 // Do not return the error, continue the process
269 return callback(null)
272 logger
.debug('Reporting remote abuse for video %s.', video
.id
)
274 const videoAbuseData
= {
275 reporterUsername: reportData
.reporterUsername
,
276 reason: reportData
.reportReason
,
277 reporterPodId: fromPod
.id
,
281 db
.VideoAbuse
.create(videoAbuseData
).asCallback(function (err
) {
283 logger
.error('Cannot create remote abuse video.', { error: err
})
286 return callback(null)
291 function fetchVideo (podHost
, remoteId
, callback
) {
292 db
.Video
.loadByHostAndRemoteId(podHost
, remoteId
, function (err
, video
) {
294 if (!err
) err
= new Error('video not found')
296 logger
.error('Cannot load video from host and remote id.', { error: err
, podHost
, remoteId
})
300 return callback(null, video
)