diff options
Diffstat (limited to 'server/controllers/api/v1/remote.js')
-rw-r--r-- | server/controllers/api/v1/remote.js | 45 |
1 files changed, 18 insertions, 27 deletions
diff --git a/server/controllers/api/v1/remote.js b/server/controllers/api/v1/remote.js index ced8470d7..2d71c605d 100644 --- a/server/controllers/api/v1/remote.js +++ b/server/controllers/api/v1/remote.js | |||
@@ -2,15 +2,15 @@ | |||
2 | 2 | ||
3 | const async = require('async') | 3 | const async = require('async') |
4 | const express = require('express') | 4 | const express = require('express') |
5 | const mongoose = require('mongoose') | ||
5 | 6 | ||
6 | const middlewares = require('../../../middlewares') | 7 | const middlewares = require('../../../middlewares') |
7 | const secureMiddleware = middlewares.secure | 8 | const secureMiddleware = middlewares.secure |
8 | const reqValidator = middlewares.reqValidators.remote | 9 | const reqValidator = middlewares.reqValidators.remote |
9 | const logger = require('../../../helpers/logger') | 10 | const logger = require('../../../helpers/logger') |
10 | const Videos = require('../../../models/videos') | ||
11 | const videos = require('../../../lib/videos') | ||
12 | 11 | ||
13 | const router = express.Router() | 12 | const router = express.Router() |
13 | const Video = mongoose.model('Video') | ||
14 | 14 | ||
15 | router.post('/videos', | 15 | router.post('/videos', |
16 | reqValidator.signature, | 16 | reqValidator.signature, |
@@ -33,48 +33,39 @@ function remoteVideos (req, res, next) { | |||
33 | // We need to process in the same order to keep consistency | 33 | // We need to process in the same order to keep consistency |
34 | // TODO: optimization | 34 | // TODO: optimization |
35 | async.eachSeries(requests, function (request, callbackEach) { | 35 | async.eachSeries(requests, function (request, callbackEach) { |
36 | const video = request.data | 36 | const videoData = request.data |
37 | 37 | ||
38 | if (request.type === 'add') { | 38 | if (request.type === 'add') { |
39 | addRemoteVideo(video, callbackEach) | 39 | addRemoteVideo(videoData, callbackEach) |
40 | } else if (request.type === 'remove') { | 40 | } else if (request.type === 'remove') { |
41 | removeRemoteVideo(video, fromUrl, callbackEach) | 41 | removeRemoteVideo(videoData, fromUrl, callbackEach) |
42 | } | 42 | } |
43 | }, function (err) { | ||
44 | if (err) logger.error('Error managing remote videos.', { error: err }) | ||
43 | }) | 45 | }) |
44 | 46 | ||
45 | // We don't need to keep the other pod waiting | 47 | // We don't need to keep the other pod waiting |
46 | return res.type('json').status(204).end() | 48 | return res.type('json').status(204).end() |
47 | } | 49 | } |
48 | 50 | ||
49 | function addRemoteVideo (videoToCreate, callback) { | 51 | function addRemoteVideo (videoToCreateData, callback) { |
50 | videos.createRemoteVideos([ videoToCreate ], function (err, remoteVideos) { | 52 | // Mongoose pre hook will automatically create the thumbnail on disk |
51 | if (err) { | 53 | videoToCreateData.thumbnail = videoToCreateData.thumbnailBase64 |
52 | logger.error('Cannot create remote videos.', { error: err }) | ||
53 | // Don't break the process | ||
54 | } | ||
55 | 54 | ||
56 | return callback() | 55 | const video = new Video(videoToCreateData) |
57 | }) | 56 | video.save(callback) |
58 | } | 57 | } |
59 | 58 | ||
60 | function removeRemoteVideo (videoToRemove, fromUrl, callback) { | 59 | function removeRemoteVideo (videoToRemoveData, fromUrl, callback) { |
61 | const magnetUris = [ videoToRemove.magnetUri ] | ||
62 | |||
63 | // We need the list because we have to remove some other stuffs (thumbnail etc) | 60 | // We need the list because we have to remove some other stuffs (thumbnail etc) |
64 | Videos.listFromUrlAndMagnets(fromUrl, magnetUris, function (err, videosList) { | 61 | Video.listByUrlAndMagnet(fromUrl, videoToRemoveData.magnetUri, function (err, videosList) { |
65 | if (err) { | 62 | if (err) { |
66 | logger.error('Cannot list videos from url and magnets.', { error: err }) | 63 | logger.error('Cannot list videos from url and magnets.', { error: err }) |
67 | // Don't break the process | 64 | return callback(err) |
68 | return callback() | ||
69 | } | 65 | } |
70 | 66 | ||
71 | videos.removeRemoteVideos(videosList, function (err) { | 67 | async.each(videosList, function (video, callbackEach) { |
72 | if (err) { | 68 | video.remove(callbackEach) |
73 | logger.error('Cannot remove remote videos.', { error: err }) | 69 | }, callback) |
74 | // Don't break the process | ||
75 | } | ||
76 | |||
77 | return callback() | ||
78 | }) | ||
79 | }) | 70 | }) |
80 | } | 71 | } |