video.js
2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* Author: Wesnydy Lima Ribeiro
* Email: wesnydy@lavid.ufpb.br
*/
'use strict';
/**
* Required libs.
*/
var fs = require('fs')
, path = require('path')
, shortid = require('shortid')
, Video = require('../models/video')
, amqp = require('../helpers/amqpManager')
, error = require('../helpers/error')
, settings = require('../config/settings');
exports.create = function(req, res, next) {
if (!req.body.gloss)
return error.badRequest('The gloss key is missing.', next);
var id = shortid.generate();
var gloss = req.body.gloss;
var video = new Video({
status: 'processing'
});
new Promise(function (resolve) {
video.save(function(err) {
if (err)
return error.internalError('A database error has occurred.', next);
res.status(200).json({ id: video._id });
resolve(gloss);
});
})
.then(function(gloss) {
return new Promise(function(resolve) {
amqp.sendToQueue(gloss, id, 'glosses', false, res, function(err) {
if (err)
return error.internalError('An internal communication error has occurred.', next);
amqp.receiveFromQueue(id, 'videos', false, res, function(err, message) {
if (err)
return error.internalError('An internal communication error has occurred.', next);
resolve(message);
});
});
})
.then(function(message) {
var body = JSON.parse(message);
Video.findOneAndUpdate( {_id: video._id}, {
$set:{
file: body.file,
size: body.size,
status: body.status
}
}, {new: true}, function(err, content) {
if (err)
return error.internalError('A database error has occurred.', next);
});
});
});
};
exports.download = function(req, res, next) {
if (!req.params.file)
return error.badRequest('File not specified.', next);
var videoFile = path.join(settings.contentsPath, req.params.file);
res.download(videoFile, function(err) {
if (err)
return error.notFound('Can\'t find any content.', next);
fs.unlink(videoFile, function(err) {
if (err)
return console.error('Can\'t remove ' + videoFile);
console.log(videoFile + ' removed.');
});
});
};
exports.status = function(req, res, next) {
// Receive param id
var contentID = req.params.id;
// Find content by id on MongoDB
return Video.findById(contentID, function (err, video) {
// returns error when can not find content
if (err)
return error.notFound('Can\'t find any content.', next);
else
res.status(200).json({ 'status': video.status, 'file': video.file, 'size': video.size });
});
};