diff options
Diffstat (limited to 'server/controllers/api/remote.js')
-rw-r--r-- | server/controllers/api/remote.js | 95 |
1 files changed, 79 insertions, 16 deletions
diff --git a/server/controllers/api/remote.js b/server/controllers/api/remote.js index a6753a2b0..c7a5658e8 100644 --- a/server/controllers/api/remote.js +++ b/server/controllers/api/remote.js | |||
@@ -50,28 +50,35 @@ function remoteVideos (req, res, next) { | |||
50 | return res.type('json').status(204).end() | 50 | return res.type('json').status(204).end() |
51 | } | 51 | } |
52 | 52 | ||
53 | function addRemoteVideo (videoToCreateData, fromHost, callback) { | 53 | function addRemoteVideo (videoToCreateData, fromHost, finalCallback) { |
54 | logger.debug('Adding remote video "%s".', videoToCreateData.name) | 54 | logger.debug('Adding remote video "%s".', videoToCreateData.name) |
55 | 55 | ||
56 | waterfall([ | 56 | waterfall([ |
57 | 57 | ||
58 | function findOrCreatePod (callback) { | 58 | function startTransaction (callback) { |
59 | db.sequelize.transaction().asCallback(function (err, t) { | ||
60 | return callback(err, t) | ||
61 | }) | ||
62 | }, | ||
63 | |||
64 | function findOrCreatePod (t, callback) { | ||
59 | const query = { | 65 | const query = { |
60 | where: { | 66 | where: { |
61 | host: fromHost | 67 | host: fromHost |
62 | }, | 68 | }, |
63 | defaults: { | 69 | defaults: { |
64 | host: fromHost | 70 | host: fromHost |
65 | } | 71 | }, |
72 | transaction: t | ||
66 | } | 73 | } |
67 | 74 | ||
68 | db.Pod.findOrCreate(query).asCallback(function (err, result) { | 75 | db.Pod.findOrCreate(query).asCallback(function (err, result) { |
69 | // [ instance, wasCreated ] | 76 | // [ instance, wasCreated ] |
70 | return callback(err, result[0]) | 77 | return callback(err, t, result[0]) |
71 | }) | 78 | }) |
72 | }, | 79 | }, |
73 | 80 | ||
74 | function findOrCreateAuthor (pod, callback) { | 81 | function findOrCreateAuthor (t, pod, callback) { |
75 | const username = videoToCreateData.author | 82 | const username = videoToCreateData.author |
76 | 83 | ||
77 | const query = { | 84 | const query = { |
@@ -82,16 +89,45 @@ function addRemoteVideo (videoToCreateData, fromHost, callback) { | |||
82 | defaults: { | 89 | defaults: { |
83 | name: username, | 90 | name: username, |
84 | podId: pod.id | 91 | podId: pod.id |
85 | } | 92 | }, |
93 | transaction: t | ||
86 | } | 94 | } |
87 | 95 | ||
88 | db.Author.findOrCreate(query).asCallback(function (err, result) { | 96 | db.Author.findOrCreate(query).asCallback(function (err, result) { |
89 | // [ instance, wasCreated ] | 97 | // [ instance, wasCreated ] |
90 | return callback(err, result[0]) | 98 | return callback(err, t, result[0]) |
91 | }) | 99 | }) |
92 | }, | 100 | }, |
93 | 101 | ||
94 | function createVideoObject (author, callback) { | 102 | function findOrCreateTags (t, author, callback) { |
103 | const tags = videoToCreateData.tags | ||
104 | const tagInstances = [] | ||
105 | |||
106 | each(tags, function (tag, callbackEach) { | ||
107 | const query = { | ||
108 | where: { | ||
109 | name: tag | ||
110 | }, | ||
111 | defaults: { | ||
112 | name: tag | ||
113 | }, | ||
114 | transaction: t | ||
115 | } | ||
116 | |||
117 | db.Tag.findOrCreate(query).asCallback(function (err, res) { | ||
118 | if (err) return callbackEach(err) | ||
119 | |||
120 | // res = [ tag, isCreated ] | ||
121 | const tag = res[0] | ||
122 | tagInstances.push(tag) | ||
123 | return callbackEach() | ||
124 | }) | ||
125 | }, function (err) { | ||
126 | return callback(err, t, author, tagInstances) | ||
127 | }) | ||
128 | }, | ||
129 | |||
130 | function createVideoObject (t, author, tagInstances, callback) { | ||
95 | const videoData = { | 131 | const videoData = { |
96 | name: videoToCreateData.name, | 132 | name: videoToCreateData.name, |
97 | remoteId: videoToCreateData.remoteId, | 133 | remoteId: videoToCreateData.remoteId, |
@@ -99,31 +135,58 @@ function addRemoteVideo (videoToCreateData, fromHost, callback) { | |||
99 | infoHash: videoToCreateData.infoHash, | 135 | infoHash: videoToCreateData.infoHash, |
100 | description: videoToCreateData.description, | 136 | description: videoToCreateData.description, |
101 | authorId: author.id, | 137 | authorId: author.id, |
102 | duration: videoToCreateData.duration, | 138 | duration: videoToCreateData.duration |
103 | tags: videoToCreateData.tags | ||
104 | } | 139 | } |
105 | 140 | ||
106 | const video = db.Video.build(videoData) | 141 | const video = db.Video.build(videoData) |
107 | 142 | ||
108 | return callback(null, video) | 143 | return callback(null, t, tagInstances, video) |
109 | }, | 144 | }, |
110 | 145 | ||
111 | function generateThumbnail (video, callback) { | 146 | function generateThumbnail (t, tagInstances, video, callback) { |
112 | db.Video.generateThumbnailFromBase64(video, videoToCreateData.thumbnailBase64, function (err) { | 147 | db.Video.generateThumbnailFromBase64(video, videoToCreateData.thumbnailBase64, function (err) { |
113 | if (err) { | 148 | if (err) { |
114 | logger.error('Cannot generate thumbnail from base 64 data.', { error: err }) | 149 | logger.error('Cannot generate thumbnail from base 64 data.', { error: err }) |
115 | return callback(err) | 150 | return callback(err) |
116 | } | 151 | } |
117 | 152 | ||
118 | video.save().asCallback(callback) | 153 | return callback(err, t, tagInstances, video) |
119 | }) | 154 | }) |
120 | }, | 155 | }, |
121 | 156 | ||
122 | function insertIntoDB (video, callback) { | 157 | function insertVideoIntoDB (t, tagInstances, video, callback) { |
123 | video.save().asCallback(callback) | 158 | const options = { |
159 | transaction: t | ||
160 | } | ||
161 | |||
162 | video.save(options).asCallback(function (err, videoCreated) { | ||
163 | return callback(err, t, tagInstances, videoCreated) | ||
164 | }) | ||
165 | }, | ||
166 | |||
167 | function associateTagsToVideo (t, tagInstances, video, callback) { | ||
168 | const options = { transaction: t } | ||
169 | |||
170 | video.setTags(tagInstances, options).asCallback(function (err) { | ||
171 | return callback(err, t) | ||
172 | }) | ||
124 | } | 173 | } |
125 | 174 | ||
126 | ], callback) | 175 | ], function (err, t) { |
176 | if (err) { | ||
177 | logger.error('Cannot insert the remote video.') | ||
178 | |||
179 | // Abort transaction? | ||
180 | if (t) t.rollback() | ||
181 | |||
182 | return finalCallback(err) | ||
183 | } | ||
184 | |||
185 | // Commit transaction | ||
186 | t.commit() | ||
187 | |||
188 | return finalCallback() | ||
189 | }) | ||
127 | } | 190 | } |
128 | 191 | ||
129 | function removeRemoteVideo (videoToRemoveData, fromHost, callback) { | 192 | function removeRemoteVideo (videoToRemoveData, fromHost, callback) { |