aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/v1/remote.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-06-24 17:42:51 +0200
committerChocobozzz <florian.bigard@gmail.com>2016-06-24 17:42:51 +0200
commitaaf61f3810e6d57c5130af959bd2860df32775e7 (patch)
tree3871b20359aa56995a4b8974ae5cb6b911dd73f5 /server/controllers/api/v1/remote.js
parent07d932038745e773359fa87bac6be70523f593ee (diff)
downloadPeerTube-aaf61f3810e6d57c5130af959bd2860df32775e7.tar.gz
PeerTube-aaf61f3810e6d57c5130af959bd2860df32775e7.tar.zst
PeerTube-aaf61f3810e6d57c5130af959bd2860df32775e7.zip
Video model refractoring -> use mongoose api
Diffstat (limited to 'server/controllers/api/v1/remote.js')
-rw-r--r--server/controllers/api/v1/remote.js45
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
3const async = require('async') 3const async = require('async')
4const express = require('express') 4const express = require('express')
5const mongoose = require('mongoose')
5 6
6const middlewares = require('../../../middlewares') 7const middlewares = require('../../../middlewares')
7const secureMiddleware = middlewares.secure 8const secureMiddleware = middlewares.secure
8const reqValidator = middlewares.reqValidators.remote 9const reqValidator = middlewares.reqValidators.remote
9const logger = require('../../../helpers/logger') 10const logger = require('../../../helpers/logger')
10const Videos = require('../../../models/videos')
11const videos = require('../../../lib/videos')
12 11
13const router = express.Router() 12const router = express.Router()
13const Video = mongoose.model('Video')
14 14
15router.post('/videos', 15router.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
49function addRemoteVideo (videoToCreate, callback) { 51function 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
60function removeRemoteVideo (videoToRemove, fromUrl, callback) { 59function 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}