aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/remote.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers/api/remote.js')
-rw-r--r--server/controllers/api/remote.js95
1 files changed, 80 insertions, 15 deletions
diff --git a/server/controllers/api/remote.js b/server/controllers/api/remote.js
index f1046c534..d856576a9 100644
--- a/server/controllers/api/remote.js
+++ b/server/controllers/api/remote.js
@@ -3,15 +3,15 @@
3const each = require('async/each') 3const each = require('async/each')
4const eachSeries = require('async/eachSeries') 4const eachSeries = require('async/eachSeries')
5const express = require('express') 5const express = require('express')
6const mongoose = require('mongoose') 6const waterfall = require('async/waterfall')
7 7
8const db = require('../../initializers/database')
8const middlewares = require('../../middlewares') 9const middlewares = require('../../middlewares')
9const secureMiddleware = middlewares.secure 10const secureMiddleware = middlewares.secure
10const validators = middlewares.validators.remote 11const validators = middlewares.validators.remote
11const logger = require('../../helpers/logger') 12const logger = require('../../helpers/logger')
12 13
13const router = express.Router() 14const router = express.Router()
14const Video = mongoose.model('Video')
15 15
16router.post('/videos', 16router.post('/videos',
17 validators.signature, 17 validators.signature,
@@ -53,34 +53,99 @@ function remoteVideos (req, res, next) {
53function addRemoteVideo (videoToCreateData, fromHost, callback) { 53function addRemoteVideo (videoToCreateData, fromHost, callback) {
54 logger.debug('Adding remote video "%s".', videoToCreateData.name) 54 logger.debug('Adding remote video "%s".', videoToCreateData.name)
55 55
56 const video = new Video(videoToCreateData) 56 waterfall([
57 video.podHost = fromHost 57
58 Video.generateThumbnailFromBase64(video, videoToCreateData.thumbnailBase64, function (err) { 58 function findOrCreatePod (callback) {
59 if (err) { 59 fromHost
60 logger.error('Cannot generate thumbnail from base 64 data.', { error: err }) 60
61 return callback(err) 61 const query = {
62 where: {
63 host: fromHost
64 },
65 defaults: {
66 host: fromHost
67 }
68 }
69
70 db.Pod.findOrCreate(query).asCallback(function (err, result) {
71 // [ instance, wasCreated ]
72 return callback(err, result[0])
73 })
74 },
75
76 function findOrCreateAuthor (pod, callback) {
77 const username = videoToCreateData.author
78
79 const query = {
80 where: {
81 name: username,
82 podId: pod.id
83 },
84 defaults: {
85 name: username,
86 podId: pod.id
87 }
88 }
89
90 db.Author.findOrCreate(query).asCallback(function (err, result) {
91 // [ instance, wasCreated ]
92 return callback(err, result[0])
93 })
94 },
95
96 function createVideoObject (author, callback) {
97 const videoData = {
98 name: videoToCreateData.name,
99 remoteId: videoToCreateData.remoteId,
100 extname: videoToCreateData.extname,
101 infoHash: videoToCreateData.infoHash,
102 description: videoToCreateData.description,
103 authorId: author.id,
104 duration: videoToCreateData.duration,
105 tags: videoToCreateData.tags
106 }
107
108 const video = db.Video.build(videoData)
109
110 return callback(null, video)
111 },
112
113 function generateThumbnail (video, callback) {
114 db.Video.generateThumbnailFromBase64(video, videoToCreateData.thumbnailBase64, function (err) {
115 if (err) {
116 logger.error('Cannot generate thumbnail from base 64 data.', { error: err })
117 return callback(err)
118 }
119
120 video.save().asCallback(callback)
121 })
122 },
123
124 function insertIntoDB (video, callback) {
125 video.save().asCallback(callback)
62 } 126 }
63 127
64 video.save(callback) 128 ], callback)
65 })
66} 129}
67 130
68function removeRemoteVideo (videoToRemoveData, fromHost, callback) { 131function removeRemoteVideo (videoToRemoveData, fromHost, callback) {
132 // TODO: use bulkDestroy?
133
69 // We need the list because we have to remove some other stuffs (thumbnail etc) 134 // We need the list because we have to remove some other stuffs (thumbnail etc)
70 Video.listByHostAndRemoteId(fromHost, videoToRemoveData.remoteId, function (err, videosList) { 135 db.Video.listByHostAndRemoteId(fromHost, videoToRemoveData.remoteId, function (err, videosList) {
71 if (err) { 136 if (err) {
72 logger.error('Cannot list videos from host and magnets.', { error: err }) 137 logger.error('Cannot list videos from host and remote id.', { error: err.message })
73 return callback(err) 138 return callback(err)
74 } 139 }
75 140
76 if (videosList.length === 0) { 141 if (videosList.length === 0) {
77 logger.error('No remote video was found for this pod.', { magnetUri: videoToRemoveData.magnetUri, podHost: fromHost }) 142 logger.error('No remote video was found for this pod.', { remoteId: videoToRemoveData.remoteId, podHost: fromHost })
78 } 143 }
79 144
80 each(videosList, function (video, callbackEach) { 145 each(videosList, function (video, callbackEach) {
81 logger.debug('Removing remote video %s.', video.magnetUri) 146 logger.debug('Removing remote video %s.', video.remoteId)
82 147
83 video.remove(callbackEach) 148 video.destroy().asCallback(callbackEach)
84 }, callback) 149 }, callback)
85 }) 150 })
86} 151}