diff options
Diffstat (limited to 'server/controllers/api/videos.js')
-rw-r--r-- | server/controllers/api/videos.js | 99 |
1 files changed, 79 insertions, 20 deletions
diff --git a/server/controllers/api/videos.js b/server/controllers/api/videos.js index a61f2b2c9..992f03db0 100644 --- a/server/controllers/api/videos.js +++ b/server/controllers/api/videos.js | |||
@@ -1,5 +1,6 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | const each = require('async/each') | ||
3 | const express = require('express') | 4 | const express = require('express') |
4 | const fs = require('fs') | 5 | const fs = require('fs') |
5 | const multer = require('multer') | 6 | const multer = require('multer') |
@@ -87,7 +88,13 @@ function addVideo (req, res, next) { | |||
87 | 88 | ||
88 | waterfall([ | 89 | waterfall([ |
89 | 90 | ||
90 | function findOrCreateAuthor (callback) { | 91 | function startTransaction (callback) { |
92 | db.sequelize.transaction().asCallback(function (err, t) { | ||
93 | return callback(err, t) | ||
94 | }) | ||
95 | }, | ||
96 | |||
97 | function findOrCreateAuthor (t, callback) { | ||
91 | const username = res.locals.oauth.token.user.username | 98 | const username = res.locals.oauth.token.user.username |
92 | 99 | ||
93 | const query = { | 100 | const query = { |
@@ -98,75 +105,125 @@ function addVideo (req, res, next) { | |||
98 | defaults: { | 105 | defaults: { |
99 | name: username, | 106 | name: username, |
100 | podId: null // null because it is OUR pod | 107 | podId: null // null because it is OUR pod |
101 | } | 108 | }, |
109 | transaction: t | ||
102 | } | 110 | } |
103 | 111 | ||
104 | db.Author.findOrCreate(query).asCallback(function (err, result) { | 112 | db.Author.findOrCreate(query).asCallback(function (err, result) { |
105 | // [ instance, wasCreated ] | 113 | // [ instance, wasCreated ] |
106 | return callback(err, result[0]) | 114 | return callback(err, t, result[0]) |
115 | }) | ||
116 | }, | ||
117 | |||
118 | function findOrCreateTags (t, author, callback) { | ||
119 | const tags = videoInfos.tags | ||
120 | const tagInstances = [] | ||
121 | |||
122 | each(tags, function (tag, callbackEach) { | ||
123 | const query = { | ||
124 | where: { | ||
125 | name: tag | ||
126 | }, | ||
127 | defaults: { | ||
128 | name: tag | ||
129 | }, | ||
130 | transaction: t | ||
131 | } | ||
132 | |||
133 | db.Tag.findOrCreate(query).asCallback(function (err, res) { | ||
134 | if (err) return callbackEach(err) | ||
135 | |||
136 | // res = [ tag, isCreated ] | ||
137 | const tag = res[0] | ||
138 | tagInstances.push(tag) | ||
139 | return callbackEach() | ||
140 | }) | ||
141 | }, function (err) { | ||
142 | return callback(err, t, author, tagInstances) | ||
107 | }) | 143 | }) |
108 | }, | 144 | }, |
109 | 145 | ||
110 | function createVideoObject (author, callback) { | 146 | function createVideoObject (t, author, tagInstances, callback) { |
111 | const videoData = { | 147 | const videoData = { |
112 | name: videoInfos.name, | 148 | name: videoInfos.name, |
113 | remoteId: null, | 149 | remoteId: null, |
114 | extname: path.extname(videoFile.filename), | 150 | extname: path.extname(videoFile.filename), |
115 | description: videoInfos.description, | 151 | description: videoInfos.description, |
116 | duration: videoFile.duration, | 152 | duration: videoFile.duration, |
117 | tags: videoInfos.tags, | ||
118 | authorId: author.id | 153 | authorId: author.id |
119 | } | 154 | } |
120 | 155 | ||
121 | const video = db.Video.build(videoData) | 156 | const video = db.Video.build(videoData) |
122 | 157 | ||
123 | return callback(null, author, video) | 158 | return callback(null, t, author, tagInstances, video) |
124 | }, | 159 | }, |
125 | 160 | ||
126 | // Set the videoname the same as the id | 161 | // Set the videoname the same as the id |
127 | function renameVideoFile (author, video, callback) { | 162 | function renameVideoFile (t, author, tagInstances, video, callback) { |
128 | const videoDir = constants.CONFIG.STORAGE.VIDEOS_DIR | 163 | const videoDir = constants.CONFIG.STORAGE.VIDEOS_DIR |
129 | const source = path.join(videoDir, videoFile.filename) | 164 | const source = path.join(videoDir, videoFile.filename) |
130 | const destination = path.join(videoDir, video.getVideoFilename()) | 165 | const destination = path.join(videoDir, video.getVideoFilename()) |
131 | 166 | ||
132 | fs.rename(source, destination, function (err) { | 167 | fs.rename(source, destination, function (err) { |
133 | return callback(err, author, video) | 168 | return callback(err, t, author, tagInstances, video) |
134 | }) | 169 | }) |
135 | }, | 170 | }, |
136 | 171 | ||
137 | function insertIntoDB (author, video, callback) { | 172 | function insertVideoIntoDB (t, author, tagInstances, video, callback) { |
138 | video.save().asCallback(function (err, videoCreated) { | 173 | const options = { transaction: t } |
174 | |||
175 | // Add tags association | ||
176 | video.save(options).asCallback(function (err, videoCreated) { | ||
177 | if (err) return callback(err) | ||
178 | |||
139 | // Do not forget to add Author informations to the created video | 179 | // Do not forget to add Author informations to the created video |
140 | videoCreated.Author = author | 180 | videoCreated.Author = author |
141 | 181 | ||
142 | return callback(err, videoCreated) | 182 | return callback(err, t, tagInstances, videoCreated) |
143 | }) | 183 | }) |
144 | }, | 184 | }, |
145 | 185 | ||
146 | function sendToFriends (video, callback) { | 186 | function associateTagsToVideo (t, tagInstances, video, callback) { |
187 | const options = { transaction: t } | ||
188 | |||
189 | video.setTags(tagInstances, options).asCallback(function (err) { | ||
190 | video.Tags = tagInstances | ||
191 | |||
192 | return callback(err, t, video) | ||
193 | }) | ||
194 | }, | ||
195 | |||
196 | function sendToFriends (t, video, callback) { | ||
147 | video.toRemoteJSON(function (err, remoteVideo) { | 197 | video.toRemoteJSON(function (err, remoteVideo) { |
148 | if (err) return callback(err) | 198 | if (err) return callback(err) |
149 | 199 | ||
150 | // Now we'll add the video's meta data to our friends | 200 | // Now we'll add the video's meta data to our friends |
151 | friends.addVideoToFriends(remoteVideo) | 201 | friends.addVideoToFriends(remoteVideo) |
152 | 202 | ||
153 | return callback(null) | 203 | return callback(null, t) |
154 | }) | 204 | }) |
155 | } | 205 | } |
156 | 206 | ||
157 | ], function andFinally (err) { | 207 | ], function andFinally (err, t) { |
158 | if (err) { | 208 | if (err) { |
159 | logger.error('Cannot insert the video.') | 209 | logger.error('Cannot insert the video.') |
210 | |||
211 | // Abort transaction? | ||
212 | if (t) t.rollback() | ||
213 | |||
160 | return next(err) | 214 | return next(err) |
161 | } | 215 | } |
162 | 216 | ||
217 | // Commit transaction | ||
218 | t.commit() | ||
219 | |||
163 | // TODO : include Location of the new video -> 201 | 220 | // TODO : include Location of the new video -> 201 |
164 | return res.type('json').status(204).end() | 221 | return res.type('json').status(204).end() |
165 | }) | 222 | }) |
166 | } | 223 | } |
167 | 224 | ||
168 | function getVideo (req, res, next) { | 225 | function getVideo (req, res, next) { |
169 | db.Video.loadAndPopulateAuthorAndPod(req.params.id, function (err, video) { | 226 | db.Video.loadAndPopulateAuthorAndPodAndTags(req.params.id, function (err, video) { |
170 | if (err) return next(err) | 227 | if (err) return next(err) |
171 | 228 | ||
172 | if (!video) { | 229 | if (!video) { |
@@ -222,12 +279,14 @@ function removeVideo (req, res, next) { | |||
222 | } | 279 | } |
223 | 280 | ||
224 | function searchVideos (req, res, next) { | 281 | function searchVideos (req, res, next) { |
225 | db.Video.searchAndPopulateAuthorAndPod(req.params.value, req.query.field, req.query.start, req.query.count, req.query.sort, | 282 | db.Video.searchAndPopulateAuthorAndPodAndTags( |
226 | function (err, videosList, videosTotal) { | 283 | req.params.value, req.query.field, req.query.start, req.query.count, req.query.sort, |
227 | if (err) return next(err) | 284 | function (err, videosList, videosTotal) { |
285 | if (err) return next(err) | ||
228 | 286 | ||
229 | res.json(getFormatedVideos(videosList, videosTotal)) | 287 | res.json(getFormatedVideos(videosList, videosTotal)) |
230 | }) | 288 | } |
289 | ) | ||
231 | } | 290 | } |
232 | 291 | ||
233 | // --------------------------------------------------------------------------- | 292 | // --------------------------------------------------------------------------- |