Commit 52773ef6e841b5efd0433be3c130887eaa02e397
1 parent
4abe5356
Exists in
master
and in
4 other branches
Assync video generation implementation
Showing
1 changed file
with
53 additions
and
4 deletions
Show diff stats
translate-api/controllers/video.js
@@ -9,16 +9,65 @@ | @@ -9,16 +9,65 @@ | ||
9 | * Required libs. | 9 | * Required libs. |
10 | */ | 10 | */ |
11 | var shortid = require('shortid') | 11 | var shortid = require('shortid') |
12 | + , Video = require('../models/video') | ||
12 | , amqp = require('../helpers/amqpManager'); | 13 | , amqp = require('../helpers/amqpManager'); |
13 | 14 | ||
14 | -exports.createVideo = function(req, res) { | 15 | +exports.create = function(req, res) { |
15 | if (!req.body.gloss) | 16 | if (!req.body.gloss) |
16 | return console.log('Gloss key missing'); | 17 | return console.log('Gloss key missing'); |
17 | 18 | ||
18 | var id = shortid.generate(); | 19 | var id = shortid.generate(); |
19 | var gloss = req.body.gloss; | 20 | var gloss = req.body.gloss; |
20 | 21 | ||
21 | - amqp.sendToQueue(gloss, id, 'glosses', false, res); | ||
22 | - amqp.sendToQueue(gloss, id, 'logs', true, res); | ||
23 | - amqp.receiveFromQueue(id, 'videos', false, res); | 22 | + var video = new Video({ |
23 | + status: 'processing' | ||
24 | + }); | ||
25 | + | ||
26 | + new Promise(function (resolve) { | ||
27 | + video.save(function(err) { | ||
28 | + if (err) | ||
29 | + return console.error(err); | ||
30 | + res.json({ id: video._id }); | ||
31 | + resolve(gloss); | ||
32 | + }); | ||
33 | + }) | ||
34 | + .then(function(gloss) { | ||
35 | + return new Promise(function(resolve) { | ||
36 | + amqp.sendToQueue(gloss, id, 'glosses', false, res, function(err) { | ||
37 | + if (err) | ||
38 | + return console.error(err); | ||
39 | + amqp.receiveFromQueue(id, 'videos', false, res, function(err, message) { | ||
40 | + if (err) | ||
41 | + return console.error(err); | ||
42 | + resolve(message); | ||
43 | + }); | ||
44 | + }); | ||
45 | + }) | ||
46 | + .then(function(message) { | ||
47 | + var body = JSON.parse(message); | ||
48 | + Video.findOneAndUpdate( {_id: video._id}, { | ||
49 | + $set:{ | ||
50 | + file: body.file, | ||
51 | + size: body.size, | ||
52 | + status: body.status | ||
53 | + } | ||
54 | + }, {new: true}, function(err, content) { | ||
55 | + if (err) | ||
56 | + return console.log(err); | ||
57 | + }); | ||
58 | + }); | ||
59 | + }); | ||
60 | +}; | ||
61 | + | ||
62 | +exports.status = function(req, res) { | ||
63 | + // Receive param id | ||
64 | + var contentID = req.params.id; | ||
65 | + // Find content by id on MongoDB | ||
66 | + return Video.findById(contentID, function (err, video) { | ||
67 | + // returns error when can not find content | ||
68 | + if (err) | ||
69 | + res.status(404).send('Not Found'); | ||
70 | + else | ||
71 | + res.json( { 'status': video.status, 'file': video.file, 'size': video.size } ); | ||
72 | + }); | ||
24 | }; | 73 | }; |