]>
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')
14 const router
= express
.Router()
17 signatureValidators
.signature
,
18 secureMiddleware
.checkSignature
,
19 videosValidators
.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 data
= request
.data
38 switch (request
.type
) {
40 addRemoteVideo(data
, fromPod
, callbackEach
)
44 updateRemoteVideo(data
, fromPod
, callbackEach
)
48 removeRemoteVideo(data
, fromPod
, callbackEach
)
52 reportAbuseRemoteVideo(data
, fromPod
, callbackEach
)
56 logger
.error('Unkown remote request type %s.', request
.type
)
59 if (err
) logger
.error('Error managing remote videos.', { error: err
})
62 // We don't need to keep the other pod waiting
63 return res
.type('json').status(204).end()
66 function addRemoteVideo (videoToCreateData
, fromPod
, finalCallback
) {
67 logger
.debug('Adding remote video "%s".', videoToCreateData
.name
)
71 function startTransaction (callback
) {
72 db
.sequelize
.transaction().asCallback(function (err
, t
) {
73 return callback(err
, t
)
77 function findOrCreateAuthor (t
, callback
) {
78 const name
= videoToCreateData
.author
79 const podId
= fromPod
.id
80 // This author is from another pod so we do not associate a user
83 db
.Author
.findOrCreateAuthor(name
, podId
, userId
, t
, function (err
, authorInstance
) {
84 return callback(err
, t
, authorInstance
)
88 function findOrCreateTags (t
, author
, callback
) {
89 const tags
= videoToCreateData
.tags
91 db
.Tag
.findOrCreateTags(tags
, t
, function (err
, tagInstances
) {
92 return callback(err
, t
, author
, tagInstances
)
96 function createVideoObject (t
, author
, tagInstances
, callback
) {
98 name: videoToCreateData
.name
,
99 remoteId: videoToCreateData
.remoteId
,
100 extname: videoToCreateData
.extname
,
101 infoHash: videoToCreateData
.infoHash
,
102 description: videoToCreateData
.description
,
104 duration: videoToCreateData
.duration
,
105 createdAt: videoToCreateData
.createdAt
,
106 updatedAt: videoToCreateData
.updatedAt
109 const video
= db
.Video
.build(videoData
)
111 return callback(null, t
, tagInstances
, video
)
114 function generateThumbnail (t
, tagInstances
, video
, callback
) {
115 db
.Video
.generateThumbnailFromData(video
, videoToCreateData
.thumbnailData
, function (err
) {
117 logger
.error('Cannot generate thumbnail from data.', { error: err
})
121 return callback(err
, t
, tagInstances
, video
)
125 function insertVideoIntoDB (t
, tagInstances
, video
, callback
) {
130 video
.save(options
).asCallback(function (err
, videoCreated
) {
131 return callback(err
, t
, tagInstances
, videoCreated
)
135 function associateTagsToVideo (t
, tagInstances
, video
, callback
) {
136 const options
= { transaction: t
}
138 video
.setTags(tagInstances
, options
).asCallback(function (err
) {
139 return callback(err
, t
)
143 ], function (err
, t
) {
145 logger
.error('Cannot insert the remote video.')
147 // Abort transaction?
150 return finalCallback(err
)
153 // Commit transaction
156 return finalCallback()
160 function updateRemoteVideo (videoAttributesToUpdate
, fromPod
, finalCallback
) {
161 logger
.debug('Updating remote video "%s".', videoAttributesToUpdate
.name
)
165 function startTransaction (callback
) {
166 db
.sequelize
.transaction().asCallback(function (err
, t
) {
167 return callback(err
, t
)
171 function findVideo (t
, callback
) {
172 fetchVideo(fromPod
.host
, videoAttributesToUpdate
.remoteId
, function (err
, videoInstance
) {
173 return callback(err
, t
, videoInstance
)
177 function findOrCreateTags (t
, videoInstance
, callback
) {
178 const tags
= videoAttributesToUpdate
.tags
180 db
.Tag
.findOrCreateTags(tags
, t
, function (err
, tagInstances
) {
181 return callback(err
, t
, videoInstance
, tagInstances
)
185 function updateVideoIntoDB (t
, videoInstance
, tagInstances
, callback
) {
186 const options
= { transaction: t
}
188 videoInstance
.set('name', videoAttributesToUpdate
.name
)
189 videoInstance
.set('description', videoAttributesToUpdate
.description
)
190 videoInstance
.set('infoHash', videoAttributesToUpdate
.infoHash
)
191 videoInstance
.set('duration', videoAttributesToUpdate
.duration
)
192 videoInstance
.set('createdAt', videoAttributesToUpdate
.createdAt
)
193 videoInstance
.set('updatedAt', videoAttributesToUpdate
.updatedAt
)
194 videoInstance
.set('extname', videoAttributesToUpdate
.extname
)
196 videoInstance
.save(options
).asCallback(function (err
) {
197 return callback(err
, t
, videoInstance
, tagInstances
)
201 function associateTagsToVideo (t
, videoInstance
, tagInstances
, callback
) {
202 const options
= { transaction: t
}
204 videoInstance
.setTags(tagInstances
, options
).asCallback(function (err
) {
205 return callback(err
, t
)
209 ], function (err
, t
) {
211 logger
.error('Cannot update the remote video.')
213 // Abort transaction?
216 return finalCallback(err
)
219 // Commit transaction
222 return finalCallback()
226 function removeRemoteVideo (videoToRemoveData
, fromPod
, callback
) {
227 // We need the instance because we have to remove some other stuffs (thumbnail etc)
228 fetchVideo(fromPod
.host
, videoToRemoveData
.remoteId
, function (err
, video
) {
229 if (err
) return callback(err
)
231 logger
.debug('Removing remote video %s.', video
.remoteId
)
232 video
.destroy().asCallback(callback
)
236 function reportAbuseRemoteVideo (reportData
, fromPod
, callback
) {
237 db
.Video
.load(reportData
.videoRemoteId
, function (err
, video
) {
239 if (!err
) err
= new Error('video not found')
241 logger
.error('Cannot load video from host and remote id.', { error: err
})
245 logger
.debug('Reporting remote abuse for video %s.', video
.id
)
247 const videoAbuseData
= {
248 reporterUsername: reportData
.reporterUsername
,
249 reason: reportData
.reportReason
,
250 reporterPodId: fromPod
.id
,
254 db
.VideoAbuse
.create(videoAbuseData
).asCallback(callback
)
258 function fetchVideo (podHost
, remoteId
, callback
) {
259 db
.Video
.loadByHostAndRemoteId(podHost
, remoteId
, function (err
, video
) {
261 if (!err
) err
= new Error('video not found')
263 logger
.error('Cannot load video from host and remote id.', { error: err
})
267 return callback(null, video
)