]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/remote/videos.js
Server: fix update remote video
[github/Chocobozzz/PeerTube.git] / server / controllers / api / remote / videos.js
1 'use strict'
2
3 const eachSeries = require('async/eachSeries')
4 const express = require('express')
5 const waterfall = require('async/waterfall')
6
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')
14
15 const router = express.Router()
16
17 router.post('/',
18 signatureValidators.signature,
19 secureMiddleware.checkSignature,
20 videosValidators.remoteVideos,
21 remoteVideos
22 )
23
24 // ---------------------------------------------------------------------------
25
26 module.exports = router
27
28 // ---------------------------------------------------------------------------
29
30 function remoteVideos (req, res, next) {
31 const requests = req.body.data
32 const fromPod = res.locals.secure.pod
33
34 // We need to process in the same order to keep consistency
35 // TODO: optimization
36 eachSeries(requests, function (request, callbackEach) {
37 const data = request.data
38
39 switch (request.type) {
40 case 'add':
41 addRemoteVideoRetryWrapper(data, fromPod, callbackEach)
42 break
43
44 case 'update':
45 updateRemoteVideoRetryWrapper(data, fromPod, callbackEach)
46 break
47
48 case 'remove':
49 removeRemoteVideo(data, fromPod, callbackEach)
50 break
51
52 case 'report-abuse':
53 reportAbuseRemoteVideo(data, fromPod, callbackEach)
54 break
55
56 default:
57 logger.error('Unkown remote request type %s.', request.type)
58 }
59 }, function (err) {
60 if (err) logger.error('Error managing remote videos.', { error: err })
61 })
62
63 // We don't need to keep the other pod waiting
64 return res.type('json').status(204).end()
65 }
66
67 // Handle retries on fail
68 function addRemoteVideoRetryWrapper (videoToCreateData, fromPod, finalCallback) {
69 const options = {
70 arguments: [ videoToCreateData, fromPod ],
71 errorMessage: 'Cannot insert the remote video with many retries.'
72 }
73
74 databaseUtils.retryTransactionWrapper(addRemoteVideo, options, finalCallback)
75 }
76
77 function addRemoteVideo (videoToCreateData, fromPod, finalCallback) {
78 logger.debug('Adding remote video "%s".', videoToCreateData.remoteId)
79
80 waterfall([
81
82 databaseUtils.startSerializableTransaction,
83
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
88 const userId = null
89
90 db.Author.findOrCreateAuthor(name, podId, userId, t, function (err, authorInstance) {
91 return callback(err, t, authorInstance)
92 })
93 },
94
95 function findOrCreateTags (t, author, callback) {
96 const tags = videoToCreateData.tags
97
98 db.Tag.findOrCreateTags(tags, t, function (err, tagInstances) {
99 return callback(err, t, author, tagInstances)
100 })
101 },
102
103 function createVideoObject (t, author, tagInstances, callback) {
104 const videoData = {
105 name: videoToCreateData.name,
106 remoteId: videoToCreateData.remoteId,
107 extname: videoToCreateData.extname,
108 infoHash: videoToCreateData.infoHash,
109 description: videoToCreateData.description,
110 authorId: author.id,
111 duration: videoToCreateData.duration,
112 createdAt: videoToCreateData.createdAt,
113 // FIXME: updatedAt does not seems to be considered by Sequelize
114 updatedAt: videoToCreateData.updatedAt
115 }
116
117 const video = db.Video.build(videoData)
118
119 return callback(null, t, tagInstances, video)
120 },
121
122 function generateThumbnail (t, tagInstances, video, callback) {
123 db.Video.generateThumbnailFromData(video, videoToCreateData.thumbnailData, function (err) {
124 if (err) {
125 logger.error('Cannot generate thumbnail from data.', { error: err })
126 return callback(err)
127 }
128
129 return callback(err, t, tagInstances, video)
130 })
131 },
132
133 function insertVideoIntoDB (t, tagInstances, video, callback) {
134 const options = {
135 transaction: t
136 }
137
138 video.save(options).asCallback(function (err, videoCreated) {
139 return callback(err, t, tagInstances, videoCreated)
140 })
141 },
142
143 function associateTagsToVideo (t, tagInstances, video, callback) {
144 const options = { transaction: t }
145
146 video.setTags(tagInstances, options).asCallback(function (err) {
147 return callback(err, t)
148 })
149 }
150
151 ], function (err, t) {
152 if (err) {
153 // This is just a debug because we will retry the insert
154 logger.debug('Cannot insert the remote video.', { error: err })
155
156 // Abort transaction?
157 if (t) t.rollback()
158
159 return finalCallback(err)
160 }
161
162 // Commit transaction
163 t.commit().asCallback(function (err) {
164 if (err) return finalCallback(err)
165
166 logger.info('Remote video %s inserted.', videoToCreateData.name)
167 return finalCallback(null)
168 })
169 })
170 }
171
172 // Handle retries on fail
173 function updateRemoteVideoRetryWrapper (videoAttributesToUpdate, fromPod, finalCallback) {
174 const options = {
175 arguments: [ videoAttributesToUpdate, fromPod ],
176 errorMessage: 'Cannot update the remote video with many retries'
177 }
178
179 databaseUtils.retryTransactionWrapper(updateRemoteVideo, options, finalCallback)
180 }
181
182 function updateRemoteVideo (videoAttributesToUpdate, fromPod, finalCallback) {
183 logger.debug('Updating remote video "%s".', videoAttributesToUpdate.remoteId)
184
185 waterfall([
186
187 databaseUtils.startSerializableTransaction,
188
189 function findVideo (t, callback) {
190 fetchVideo(fromPod.host, videoAttributesToUpdate.remoteId, function (err, videoInstance) {
191 return callback(err, t, videoInstance)
192 })
193 },
194
195 function findOrCreateTags (t, videoInstance, callback) {
196 const tags = videoAttributesToUpdate.tags
197
198 db.Tag.findOrCreateTags(tags, t, function (err, tagInstances) {
199 return callback(err, t, videoInstance, tagInstances)
200 })
201 },
202
203 function updateVideoIntoDB (t, videoInstance, tagInstances, callback) {
204 const options = { transaction: t }
205
206 videoInstance.set('name', videoAttributesToUpdate.name)
207 videoInstance.set('description', videoAttributesToUpdate.description)
208 videoInstance.set('infoHash', videoAttributesToUpdate.infoHash)
209 videoInstance.set('duration', videoAttributesToUpdate.duration)
210 videoInstance.set('createdAt', videoAttributesToUpdate.createdAt)
211 videoInstance.set('updatedAt', videoAttributesToUpdate.updatedAt)
212 videoInstance.set('extname', videoAttributesToUpdate.extname)
213
214 videoInstance.save(options).asCallback(function (err) {
215 return callback(err, t, videoInstance, tagInstances)
216 })
217 },
218
219 function associateTagsToVideo (t, videoInstance, tagInstances, callback) {
220 const options = { transaction: t }
221
222 videoInstance.setTags(tagInstances, options).asCallback(function (err) {
223 return callback(err, t)
224 })
225 }
226
227 ], function (err, t) {
228 if (err) {
229 // This is just a debug because we will retry the insert
230 logger.debug('Cannot update the remote video.', { error: err })
231
232 // Abort transaction?
233 if (t) t.rollback()
234
235 return finalCallback(err)
236 }
237
238 // Commit transaction
239 t.commit().asCallback(function (err) {
240 if (err) return finalCallback(err)
241
242 logger.info('Remote video %s updated', videoAttributesToUpdate.name)
243 return finalCallback(null)
244 })
245 })
246 }
247
248 function removeRemoteVideo (videoToRemoveData, fromPod, callback) {
249 // We need the instance because we have to remove some other stuffs (thumbnail etc)
250 fetchVideo(fromPod.host, videoToRemoveData.remoteId, function (err, video) {
251 // Do not return the error, continue the process
252 if (err) return callback(null)
253
254 logger.debug('Removing remote video %s.', video.remoteId)
255 video.destroy().asCallback(function (err) {
256 // Do not return the error, continue the process
257 if (err) {
258 logger.error('Cannot remove remote video with id %s.', videoToRemoveData.remoteId, { error: err })
259 }
260
261 return callback(null)
262 })
263 })
264 }
265
266 function reportAbuseRemoteVideo (reportData, fromPod, callback) {
267 db.Video.load(reportData.videoRemoteId, function (err, video) {
268 if (err || !video) {
269 if (!err) err = new Error('video not found')
270
271 logger.error('Cannot load video from id.', { error: err, id: reportData.videoRemoteId })
272 // Do not return the error, continue the process
273 return callback(null)
274 }
275
276 logger.debug('Reporting remote abuse for video %s.', video.id)
277
278 const videoAbuseData = {
279 reporterUsername: reportData.reporterUsername,
280 reason: reportData.reportReason,
281 reporterPodId: fromPod.id,
282 videoId: video.id
283 }
284
285 db.VideoAbuse.create(videoAbuseData).asCallback(function (err) {
286 if (err) {
287 logger.error('Cannot create remote abuse video.', { error: err })
288 }
289
290 return callback(null)
291 })
292 })
293 }
294
295 function fetchVideo (podHost, remoteId, callback) {
296 db.Video.loadByHostAndRemoteId(podHost, remoteId, function (err, video) {
297 if (err || !video) {
298 if (!err) err = new Error('video not found')
299
300 logger.error('Cannot load video from host and remote id.', { error: err, podHost, remoteId })
301 return callback(err)
302 }
303
304 return callback(null, video)
305 })
306 }