diff options
Diffstat (limited to 'server/controllers/api')
-rw-r--r-- | server/controllers/api/remote.js | 108 | ||||
-rw-r--r-- | server/controllers/api/videos.js | 2 |
2 files changed, 89 insertions, 21 deletions
diff --git a/server/controllers/api/remote.js b/server/controllers/api/remote.js index 929ade555..254ae56d5 100644 --- a/server/controllers/api/remote.js +++ b/server/controllers/api/remote.js | |||
@@ -35,12 +35,21 @@ function remoteVideos (req, res, next) { | |||
35 | eachSeries(requests, function (request, callbackEach) { | 35 | eachSeries(requests, function (request, callbackEach) { |
36 | const videoData = request.data | 36 | const videoData = request.data |
37 | 37 | ||
38 | if (request.type === 'add') { | 38 | switch (request.type) { |
39 | addRemoteVideo(videoData, fromPod, callbackEach) | 39 | case 'add': |
40 | } else if (request.type === 'remove') { | 40 | addRemoteVideo(videoData, fromPod, callbackEach) |
41 | removeRemoteVideo(videoData, fromPod, callbackEach) | 41 | break |
42 | } else { | 42 | |
43 | logger.error('Unkown remote request type %s.', request.type) | 43 | case 'update': |
44 | updateRemoteVideo(videoData, fromPod, callbackEach) | ||
45 | break | ||
46 | |||
47 | case 'remove': | ||
48 | removeRemoteVideo(videoData, fromPod, callbackEach) | ||
49 | break | ||
50 | |||
51 | default: | ||
52 | logger.error('Unkown remote request type %s.', request.type) | ||
44 | } | 53 | } |
45 | }, function (err) { | 54 | }, function (err) { |
46 | if (err) logger.error('Error managing remote videos.', { error: err }) | 55 | if (err) logger.error('Error managing remote videos.', { error: err }) |
@@ -143,24 +152,85 @@ function addRemoteVideo (videoToCreateData, fromPod, finalCallback) { | |||
143 | }) | 152 | }) |
144 | } | 153 | } |
145 | 154 | ||
146 | function removeRemoteVideo (videoToRemoveData, fromPod, callback) { | 155 | function updateRemoteVideo (videoAttributesToUpdate, fromPod, finalCallback) { |
147 | // TODO: use bulkDestroy? | 156 | logger.debug('Updating remote video "%s".', videoAttributesToUpdate.name) |
148 | 157 | ||
149 | // We need the list because we have to remove some other stuffs (thumbnail etc) | 158 | waterfall([ |
150 | db.Video.listByHostAndRemoteId(fromPod.host, videoToRemoveData.remoteId, function (err, videosList) { | 159 | |
151 | if (err) { | 160 | function startTransaction (callback) { |
152 | logger.error('Cannot list videos from host and remote id.', { error: err.message }) | 161 | db.sequelize.transaction().asCallback(function (err, t) { |
153 | return callback(err) | 162 | return callback(err, t) |
163 | }) | ||
164 | }, | ||
165 | |||
166 | function findVideo (t, callback) { | ||
167 | db.Video.loadByHostAndRemoteId(fromPod.host, videoAttributesToUpdate.remoteId, function (err, videoInstance) { | ||
168 | if (err || !videoInstance) { | ||
169 | logger.error('Cannot load video from host and remote id.', { error: err.message }) | ||
170 | return callback(err) | ||
171 | } | ||
172 | |||
173 | return callback(null, t, videoInstance) | ||
174 | }) | ||
175 | }, | ||
176 | |||
177 | function findOrCreateTags (t, videoInstance, callback) { | ||
178 | const tags = videoAttributesToUpdate.tags | ||
179 | |||
180 | db.Tag.findOrCreateTags(tags, t, function (err, tagInstances) { | ||
181 | return callback(err, t, videoInstance, tagInstances) | ||
182 | }) | ||
183 | }, | ||
184 | |||
185 | function updateVideoIntoDB (t, videoInstance, tagInstances, callback) { | ||
186 | const options = { transaction: t } | ||
187 | |||
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('extname', videoAttributesToUpdate.extname) | ||
194 | |||
195 | videoInstance.save(options).asCallback(function (err) { | ||
196 | return callback(err, t, videoInstance, tagInstances) | ||
197 | }) | ||
198 | }, | ||
199 | |||
200 | function associateTagsToVideo (t, videoInstance, tagInstances, callback) { | ||
201 | const options = { transaction: t } | ||
202 | |||
203 | videoInstance.setTags(tagInstances, options).asCallback(function (err) { | ||
204 | return callback(err, t) | ||
205 | }) | ||
154 | } | 206 | } |
155 | 207 | ||
156 | if (videosList.length === 0) { | 208 | ], function (err, t) { |
157 | logger.error('No remote video was found for this pod.', { remoteId: videoToRemoveData.remoteId, podHost: fromPod.host }) | 209 | if (err) { |
210 | logger.error('Cannot update the remote video.') | ||
211 | |||
212 | // Abort transaction? | ||
213 | if (t) t.rollback() | ||
214 | |||
215 | return finalCallback(err) | ||
158 | } | 216 | } |
159 | 217 | ||
160 | each(videosList, function (video, callbackEach) { | 218 | // Commit transaction |
161 | logger.debug('Removing remote video %s.', video.remoteId) | 219 | t.commit() |
220 | |||
221 | return finalCallback() | ||
222 | }) | ||
223 | } | ||
224 | |||
225 | function removeRemoteVideo (videoToRemoveData, fromPod, callback) { | ||
226 | // We need the instance because we have to remove some other stuffs (thumbnail etc) | ||
227 | db.Video.loadByHostAndRemoteId(fromPod.host, videoToRemoveData.remoteId, function (err, video) { | ||
228 | if (err || !video) { | ||
229 | logger.error('Cannot load video from host and remote id.', { error: err.message }) | ||
230 | return callback(err) | ||
231 | } | ||
162 | 232 | ||
163 | video.destroy().asCallback(callbackEach) | 233 | logger.debug('Removing remote video %s.', video.remoteId) |
164 | }, callback) | 234 | video.destroy().asCallback(callback) |
165 | }) | 235 | }) |
166 | } | 236 | } |
diff --git a/server/controllers/api/videos.js b/server/controllers/api/videos.js index 1b306d1cf..e5c52a87b 100644 --- a/server/controllers/api/videos.js +++ b/server/controllers/api/videos.js | |||
@@ -229,8 +229,6 @@ function updateVideo (req, res, next) { | |||
229 | 229 | ||
230 | // Add tags association | 230 | // Add tags association |
231 | videoInstance.save(options).asCallback(function (err) { | 231 | videoInstance.save(options).asCallback(function (err) { |
232 | if (err) return callback(err) | ||
233 | |||
234 | return callback(err, t, tagInstances) | 232 | return callback(err, t, tagInstances) |
235 | }) | 233 | }) |
236 | }, | 234 | }, |