Commit bfb665368f1dd5c76f1b91327ae618f0628b4f5d

Authored by Cassio Cabral
1 parent 69192450
Exists in master and in 1 other branch devel

merge enviado por renan

db/api.js 100755 → 100644
... ... @@ -14,12 +14,31 @@ function create(object, callback) {
14 14 });
15 15 };
16 16  
17   -function update(object, status, callback) {
18   - object.update({}, { $set : { 'type' : status }}, function (err, request) {
  17 +function update(Request, id, status, callback) {
  18 + console.log("== Update requisicao");
  19 + Request.update({'id': id}, {$set : { 'status' : status }}, function (err, result) {
  20 + if (err) callback(null);
  21 + callback(result);
  22 + });
  23 +};
  24 +
  25 +function findById(Request, requestId, callback) {
  26 + Request.find({ id : requestId }, { _id: 0, __v: 0 }, function(err, result) {
  27 + if (err) callback(null);
  28 +
  29 + callback(result);
  30 + });
  31 +};
  32 +
  33 +function findByIds(Request, requests, callback) {
  34 + console.log(requests);
  35 + Request.find({
  36 + 'id': { $in: requests }
  37 + }, { _id: 0, __v: 0 }, function(err, request){
19 38 if (err) callback(null);
20 39 callback(request);
21 40 });
22   -}
  41 +};
23 42  
24 43 function remove(Request, hash, callback) {
25 44 Request.remove({ id: hash }, function(err, request) {
... ... @@ -33,4 +52,5 @@ module.exports.read_all = read_all;
33 52 module.exports.create = create;
34 53 module.exports.remove = remove;
35 54 module.exports.update = update;
36   -
  55 +module.exports.findById = findById;
  56 +module.exports.findByIds = findByIds;
... ...
db/config.js 100755 → 100644
db/schemas/request.js 100755 → 100644
... ... @@ -3,6 +3,14 @@ function init_schema(mongoose) {
3 3 id: String,
4 4 status: String,
5 5 type: String,
  6 + link: String,
  7 + institution: String,
  8 + author: String,
  9 + user: String,
  10 + subtitle: String,
  11 + manualrevision: String,
  12 + reasonofrevision: String,
  13 + dictionary: String,
6 14 created_at: { type: Date },
7 15 updated_at: { type: Date }
8 16 });
... ... @@ -12,4 +20,4 @@ function init_schema(mongoose) {
12 20 return Request;
13 21 };
14 22  
15   -module.exports.init = init_schema;
16 23 \ No newline at end of file
  24 +module.exports.init = init_schema;
... ...
db/schemas/request.js.save 0 → 100644
... ... @@ -0,0 +1,23 @@
  1 +function init_schema(mongoose) {
  2 + var requestSchema = new mongoose.Schema({
  3 + id: String,
  4 + status: String,
  5 + type: String,
  6 + link: String,
  7 + institution: String,
  8 + author: String,
  9 + user: String,
  10 + subtitle: String,
  11 + manualrevision: String,
  12 +
  13 + dictionary: String,
  14 + created_at: { type: Date },
  15 + updated_at: { type: Date }
  16 + });
  17 +
  18 + var Request = mongoose.model('Request', requestSchema);
  19 +
  20 + return Request;
  21 +};
  22 +
  23 +module.exports.init = init_schema;
... ...
endpoints/texto.js 100755 → 100644
endpoints/videornp.js 0 → 100644
... ... @@ -0,0 +1,173 @@
  1 +var parameters = require('../helpers/parameters');
  2 +var properties = require('../helpers/properties');
  3 +var files = require('../helpers/files');
  4 +var core = require('../helpers/core');
  5 +var db = require('../db/api');
  6 +
  7 +var uuid = require('node-uuid');
  8 +var mkdirp = require('mkdirp');
  9 +var async = require('async');
  10 +
  11 +function init(req, res, Request) {
  12 + res.set("Content-Type", "application/json");
  13 +
  14 + /* Verifica se os paramêtros [transparencia, texto] possuem algum valor */
  15 + if (((req.body.legenda_url === '') && (req.body.video_url === '')) || ((typeof req.body.legenda_url === 'undefined') && (typeof req.body.video_url === 'undefined'))) {
  16 + res.send(500, parameters.errorMessage('O valor de algum parâmetro está vazio'));
  17 + return;
  18 + }
  19 +
  20 + /* Verifica se os paramêtros [transparencia, texto] possuem algum valor */
  21 + if ((typeof req.body.revisaomanual === 'undefined') || ((req.body.revisaomanual.toUpperCase() !== "SIM") && (req.body.revisaomanual.toUpperCase() !== "NAO"))) {
  22 + res.send(500, parameters.errorMessage('O valor do parâmetro revisaomanual é inválido.'));
  23 + return;
  24 + }
  25 +
  26 + if ((typeof req.body.conteudista === 'undefined') || (req.body.conteudista === '')) {
  27 + res.send(500, parameters.errorMessage('O valor de algum parâmetro está vazio'));
  28 + return;
  29 + }
  30 +
  31 + if ((typeof req.body.instituicao === 'undefined') || (req.body.instituicao === '')) {
  32 + res.send(500, parameters.errorMessage('O valor de algum parâmetro está vazio'));
  33 + return;
  34 + }
  35 +
  36 + if ((typeof req.body.usuario === 'undefined') || (req.body.usuario === '')) {
  37 + res.send(500, parameters.errorMessage('O valor de algum parâmetro está vazio'));
  38 + return;
  39 + }
  40 +
  41 + process(req, res, Request);
  42 +};
  43 +
  44 +function process(req, res, Request) {
  45 + var id = uuid.v4();
  46 + var folder = properties.uploads_folder + id;
  47 + var locals = {};
  48 +
  49 + var request_object = new Request({
  50 + id: id,
  51 + type: req.body.servico,
  52 + status: 'Submitted',
  53 + link: 'http://' + properties.SERVER_IP + ':' + properties.port + '/' + id + '.mp4',
  54 + user: req.body.usuario,
  55 + institution: req.body.instituicao,
  56 + author: req.body.conteudista,
  57 + manualrevision: req.body.revisaomanual,
  58 + reasonofrevision: req.body.motivodarevisao,
  59 + subtitle: 'http://150.165.204.30:5000/api/subtitle.srt',
  60 + dictionary: '1.0',
  61 + created_at: new Date(),
  62 + updated_at: new Date(),
  63 + });
  64 +
  65 + db.create(request_object, function(result) {
  66 + if (result !== null) {
  67 + res.send(200, { 'id': result.id });
  68 + } else {
  69 + res.send(500, { 'error': 'Erro na criação da requisição.'});
  70 + }
  71 + });
  72 +
  73 + async.series([
  74 + // Cria a pasta apropriada
  75 + function(callback) {
  76 + console.log("== Criando pasta " + folder);
  77 +
  78 + mkdirp(folder, function(err) {
  79 + var error;
  80 +
  81 + if (err) { error = "Erro na criação da pasta com o id: " + id + "; " + err; }
  82 +
  83 + callback(error);
  84 + });
  85 + },
  86 +
  87 + // Baixa e move os arquivos para a pasta correta
  88 + function(callback) {
  89 + console.log("== Baixando os arquivos");
  90 +
  91 + downloadAndMoveFiles(folder, req, locals, callback);
  92 + },
  93 +
  94 + // Chama o core
  95 + function(callback) {
  96 + console.log("== Chamando o core");
  97 +
  98 + // Faz a chamada ao core
  99 + try {
  100 + callCore(id, locals.video, locals.subtitle, req, res, Request, request_object);
  101 + callback();
  102 + } catch (err) {
  103 + callback(err);
  104 + }
  105 + }
  106 + ], function(err) {
  107 + // Se tiver erro
  108 + if (err) {
  109 + res.send(500, parameters.errorMessage(err));
  110 +
  111 + return;
  112 + }
  113 + });
  114 +}
  115 +
  116 +
  117 +function downloadAndMoveFiles(folder, req, locals, callback) {
  118 + async.parallel([
  119 + // Download video
  120 + function(callback) {
  121 + files.downloadAndMoveVideo(folder, req, locals, callback);
  122 + }
  123 + ], function(err) {
  124 + console.log("=== Video baixado");
  125 +
  126 + // Callback chamado depois de todas as tarefas
  127 + // Se tiver erro, vai passar para cima
  128 + callback(err);
  129 + });
  130 +}
  131 +
  132 +
  133 +function callCore(id, video, subtitle, req, res, Request, request_object) {
  134 +
  135 + /* Cria a linha de comando */
  136 + /* slice(2) é para transformar ./path em path */
  137 + var command_line = 'vlibras_user/vlibras-core/./vlibras 7 ' + video.path.slice(2) + ' ' + id + ' > /tmp/core_log 2>&1';
  138 +
  139 + console.log("=== Core: " + command_line);
  140 +
  141 + console.log("ID: " + request_object.id);
  142 + core.call(id, command_line, req, res, Request, request_object);
  143 +};
  144 +
  145 +function callCoreSubtitle(id, subtitle, req, res, Request, request_object) {
  146 + /* Move a legenda submetido para a pasta com o seu ID correspondente */
  147 + fs.rename(req.files.legenda.path, __dirname + '/uploads/' + id + '/' + req.files.legenda.name, function(error) {
  148 + if (error) { console.log(error); }
  149 + });
  150 +
  151 + /* Cria a linha de comando */
  152 + var command_line = 'vlibras_user/vlibras-core/./vlibras 7 ' + ' uploads/' + id + '/' +
  153 + req.files.legenda.name + ' opaco ' + id;
  154 +
  155 + /* Executa a linha de comando */
  156 + child = exec(command_line, function(err, stdout, stderr) {
  157 + // [stdout] = vlibras-core output
  158 + // console.log(stdout);
  159 + });
  160 +
  161 + /* Listener que dispara quando a requisição ao core finaliza */
  162 + child.on('close', function(code, signal){
  163 + res.send(200, { 'response' : 'http://' + properties.SERVER_IP + ':' + properties.port + '/' + id + '.flv' });
  164 + });
  165 +
  166 + /* Listener que dispara quando a requisição ao core da erro */
  167 + child.on('error', function(code, signal){
  168 + res.send(500, parameters.errorMessage('Erro na chamada ao core'));
  169 + });
  170 +
  171 +};
  172 +
  173 +module.exports.init = init;
... ...
server.js 100755 → 100644
... ... @@ -52,6 +52,10 @@ app.post('/api', function(req, res) {
52 52 ep_video.init(req, res);
53 53 break;
54 54  
  55 + case 'videornp':
  56 + ep_video_rnp.init(req, res, Request);
  57 + break;
  58 +
55 59 /* Tipo de Serviço: Só a Legenda */
56 60 case 'legenda':
57 61 ep_legenda.init(req, res);
... ... @@ -73,7 +77,7 @@ app.post('/api', function(req, res) {
73 77 }
74 78 });
75 79  
76   -app.get('/api/requests', function(req, res) {
  80 +app.get('/requests', function(req, res) {
77 81 db.read_all(Request, function(result) {
78 82 if (result !== null) {
79 83 res.send(200, result);
... ... @@ -83,15 +87,39 @@ app.get('/api/requests', function(req, res) {
83 87 });
84 88 });
85 89  
86   -app.post('/glosa', function(req, res) {
87   -// options.args = JSON.stringify(req.body);
88   - PythonShell.run('vlibras_user/vlibras-translate/PortGlosa.py', req.body.texto, function (err, results) {
89   - if (err) { console.log(err); res.send(400); return; }
90   - // results is an array consisting of messages collected during execution
91   - res.send(results);
  90 +
  91 +app.get('/api/requests/', function(req,res) {
  92 + db.findByIds(Request, req.param("id"), function(result) {
  93 + console.log(result);
  94 + if (result !== null) {
  95 + res.send(200, result);
  96 + } else {
  97 + res.send(500, { 'error': 'Erro na busca.'});
  98 + }
  99 + });
  100 +});
  101 +
  102 +
  103 +app.get('/api/requests/:id', function(req, res) {
  104 + db.findById(Request, req.param.id, function(result) {
  105 + if (result !== null) {
  106 + res.send(200, result);
  107 + } else {
  108 + res.send(500, { 'error': 'Erro na busca.'});
  109 + }
92 110 });
93 111 });
94 112  
  113 +app.get('/glosa', function(req, res) {
  114 + res.header("Access-Control-Allow-Origin", "*");
  115 + options.args = req.param("texto");
  116 + console.log(options.args);
  117 + PythonShell.run('PortGlosa.py', options, function (err, results) {
  118 + if (err) { console.log(err); res.send(400); return;}
  119 + res.send(200,results[0]);
  120 + });
  121 +});
  122 +
95 123 app.get('/limparfila', function(req, res) {
96 124  
97 125 // graceful shutdown
... ...
vlibras-api
... ... @@ -1 +0,0 @@
1   -vlibras-api
2 0 \ No newline at end of file
vlibras-api 0 → 100644
... ... @@ -0,0 +1 @@
  1 +vlibras-api
0 2 \ No newline at end of file
... ...