Commit 2ffea56824c2ef9ab575272811479b94baa78e63
1 parent
6ea50936
Exists in
master
and in
1 other branch
Adiciona endpoint audio
Showing
6 changed files
with
206 additions
and
4 deletions
Show diff stats
| @@ -0,0 +1,105 @@ | @@ -0,0 +1,105 @@ | ||
| 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 | + | ||
| 6 | +var uuid = require('node-uuid'); | ||
| 7 | +var mkdirp = require('mkdirp'); | ||
| 8 | +var async = require('async'); | ||
| 9 | + | ||
| 10 | +function init(req, res) { | ||
| 11 | + res.set("Content-Type", "application/json"); | ||
| 12 | + | ||
| 13 | + /* Verifica se os paramêtros [transparencia, texto] possuem algum valor */ | ||
| 14 | + if (req.body.transparencia === '') { | ||
| 15 | + res.send(500, parameters.errorMessage('O valor de algum parâmetro está vazio')); | ||
| 16 | + return; | ||
| 17 | + } | ||
| 18 | + | ||
| 19 | + /* Verifica se o paramêtro transparencia possui os seus únicos valores possíveis */ | ||
| 20 | + if (parameters.checkTransparency(req.body.transparencia) === false) { | ||
| 21 | + res.send(500, parameters.errorMessage('Parâmetros insuficientes ou inválidos')); | ||
| 22 | + return; | ||
| 23 | + } | ||
| 24 | + | ||
| 25 | + process(req, res); | ||
| 26 | +}; | ||
| 27 | + | ||
| 28 | +function process(req, res) { | ||
| 29 | + var id = uuid.v4(); | ||
| 30 | + var folder = properties.uploads_folder + id; | ||
| 31 | + var locals = {}; | ||
| 32 | + | ||
| 33 | + async.series([ | ||
| 34 | + // Cria a pasta apropriada | ||
| 35 | + function(callback) { | ||
| 36 | + console.log("== Criando pasta " + folder); | ||
| 37 | + | ||
| 38 | + mkdirp(folder, function(err) { | ||
| 39 | + var error; | ||
| 40 | + | ||
| 41 | + if (err) { error = "Erro na criação da pasta com o id: " + id + "; " + err; } | ||
| 42 | + | ||
| 43 | + callback(error); | ||
| 44 | + }); | ||
| 45 | + }, | ||
| 46 | + | ||
| 47 | + // Baixa e move os arquivos para a pasta correta | ||
| 48 | + function(callback) { | ||
| 49 | + console.log("== Baixando os arquivos"); | ||
| 50 | + | ||
| 51 | + downloadAndMoveFiles(folder, req, locals, callback); | ||
| 52 | + }, | ||
| 53 | + | ||
| 54 | + // Chama o core | ||
| 55 | + function(callback) { | ||
| 56 | + console.log("== Chamando o core"); | ||
| 57 | + | ||
| 58 | + // Faz a chamada ao core | ||
| 59 | + try { | ||
| 60 | + callCore(id, locals.audio, req, res); | ||
| 61 | + callback(); | ||
| 62 | + } catch (err) { | ||
| 63 | + callback(err); | ||
| 64 | + } | ||
| 65 | + } | ||
| 66 | + ], function(err) { | ||
| 67 | + // Se tiver erro | ||
| 68 | + if (err) { | ||
| 69 | + res.send(500, parameters.errorMessage(err)); | ||
| 70 | + | ||
| 71 | + return; | ||
| 72 | + } | ||
| 73 | + }); | ||
| 74 | +} | ||
| 75 | + | ||
| 76 | + | ||
| 77 | +function downloadAndMoveFiles(folder, req, locals, callback) { | ||
| 78 | + async.parallel([ | ||
| 79 | + // Download audio | ||
| 80 | + function(callback) { | ||
| 81 | + files.downloadAndMoveAudio(folder, req, locals, callback); | ||
| 82 | + } | ||
| 83 | + ], function(err) { | ||
| 84 | + console.log("=== audio baixado"); | ||
| 85 | + | ||
| 86 | + // Callback chamado depois de todas as tarefas | ||
| 87 | + // Se tiver erro, vai passar para cima | ||
| 88 | + callback(err); | ||
| 89 | + }); | ||
| 90 | +} | ||
| 91 | + | ||
| 92 | + | ||
| 93 | +function callCore(id, audio, req, res) { | ||
| 94 | + | ||
| 95 | + /* Cria a linha de comando */ | ||
| 96 | + /* slice(2) é para transformar ./path em path */ | ||
| 97 | + var command_line = 'vlibras_user/vlibras-core/./vlibras ' + parameters.getServiceType(req.body.servico) + ' ' + | ||
| 98 | + audio.path.slice(2) + ' ' + parameters.getTransparency(req.body.transparencia) + ' ' + id + ' > /tmp/core_log 2>&1'; | ||
| 99 | + | ||
| 100 | + console.log("=== Core: " + command_line); | ||
| 101 | + | ||
| 102 | + core.call(id, command_line, req, res); | ||
| 103 | +}; | ||
| 104 | + | ||
| 105 | +module.exports.init = init; |
helpers/core.js
| @@ -27,7 +27,7 @@ function call(id, command_line, req, res) { | @@ -27,7 +27,7 @@ function call(id, command_line, req, res) { | ||
| 27 | } | 27 | } |
| 28 | 28 | ||
| 29 | // Se o core executou normal | 29 | // Se o core executou normal |
| 30 | - res.send(200, { 'response' : 'http://' + properties.SERVER_IP + ':' + properties.port + '/' + id + '.flv', 'id' : id }); | 30 | + res.send(200, { 'response' : 'http://' + properties.SERVER_IP + ':' + properties.port + '/' + id + '.mp4'}); |
| 31 | }); | 31 | }); |
| 32 | 32 | ||
| 33 | // Se a chamada deu erro | 33 | // Se a chamada deu erro |
helpers/files.js
| @@ -175,5 +175,88 @@ function downloadAndMoveSubtitle(folder, req, locals, callback) { | @@ -175,5 +175,88 @@ function downloadAndMoveSubtitle(folder, req, locals, callback) { | ||
| 175 | } | 175 | } |
| 176 | } | 176 | } |
| 177 | 177 | ||
| 178 | +function downloadAndMoveAudio(folder, req, locals, callback) { | ||
| 179 | + | ||
| 180 | + // Se enviou o arquivo na requisição | ||
| 181 | + if (req.files.audio !== undefined) { | ||
| 182 | + | ||
| 183 | + // Se a validação falhar | ||
| 184 | + if (parameters.checkAudio(req.files.audio.name) === false) { | ||
| 185 | + var error = 'Áudio enviado com extensão inválida'; | ||
| 186 | + return callback(error); | ||
| 187 | + } | ||
| 188 | + | ||
| 189 | + /* Move o áudio submetido para a pasta com o seu ID correspondente */ | ||
| 190 | + try { | ||
| 191 | + fs.renameSync(req.files.audio.path, folder + '/' + req.files.audio.name); | ||
| 192 | + } catch (err) { | ||
| 193 | + callback("Erro ao mover o áudio submetido: " + err); | ||
| 194 | + } | ||
| 195 | + | ||
| 196 | + // Se não, retorna o áudio enviado | ||
| 197 | + locals.audio = { | ||
| 198 | + 'path': folder + '/' + req.files.audio.name | ||
| 199 | + } | ||
| 200 | + | ||
| 201 | + return callback(); | ||
| 202 | + | ||
| 203 | + // Se o arquivo não foi enviado, mas um audio_url foi | ||
| 204 | + } else if (req.body.audio_url !== undefined) { | ||
| 205 | + | ||
| 206 | + // Requisição para baixar o vídeo | ||
| 207 | + http.get(req.body.audio_url, function(response) { | ||
| 208 | + | ||
| 209 | + // Se o áudio não foi baixado com sucesso | ||
| 210 | + if (response.statusCode !== 200) { | ||
| 211 | + var error = 'Problema ao carregar audio_url: status ' + response.statusCode; | ||
| 212 | + return callback(error); | ||
| 213 | + } | ||
| 214 | + | ||
| 215 | + // Nome do arquivo | ||
| 216 | + var filename = req.body.audio_url.substring(req.body.audio_url.lastIndexOf('/') + 1); | ||
| 217 | + | ||
| 218 | + // Tira os parâmetros HTTP | ||
| 219 | + if (filename.lastIndexOf("?") !== -1) { | ||
| 220 | + filename = filename.substring(0, filename.lastIndexOf("?")); | ||
| 221 | + } | ||
| 222 | + | ||
| 223 | + var path = folder + '/' + filename; | ||
| 224 | + | ||
| 225 | + // Cria o stream para escrita | ||
| 226 | + var file = fs.createWriteStream(path); | ||
| 227 | + | ||
| 228 | + // Salva o arquivo em disco | ||
| 229 | + response.pipe(file); | ||
| 230 | + | ||
| 231 | + // Quando a escrita acabar | ||
| 232 | + file.on('finish', function() { | ||
| 233 | + | ||
| 234 | + // Fecha o arquivo | ||
| 235 | + file.close(function() { | ||
| 236 | + | ||
| 237 | + // Retorna o áudio baixado | ||
| 238 | + locals.audio = { | ||
| 239 | + 'path': path | ||
| 240 | + } | ||
| 241 | + | ||
| 242 | + // Chama o callback para prosseguir execução | ||
| 243 | + callback(); | ||
| 244 | + }); | ||
| 245 | + }); | ||
| 246 | + | ||
| 247 | + // Se deu erro na requisição de baixar o áudio | ||
| 248 | + }).on('error', function(e) { | ||
| 249 | + var error = 'Problema ao carregar audio_url: ' + e.message; | ||
| 250 | + return callback(error); | ||
| 251 | + }); | ||
| 252 | + | ||
| 253 | + // Se nem o áudio foi enviado e nem o audio_url foi preenchido | ||
| 254 | + } else { | ||
| 255 | + var error = "Áudio deve ser enviado como parâmetro 'audio' ou como 'audio_url'"; | ||
| 256 | + return callback(error); | ||
| 257 | + } | ||
| 258 | +} | ||
| 259 | + | ||
| 178 | module.exports.downloadAndMoveVideo = downloadAndMoveVideo; | 260 | module.exports.downloadAndMoveVideo = downloadAndMoveVideo; |
| 179 | -module.exports.downloadAndMoveSubtitle = downloadAndMoveSubtitle; | ||
| 180 | \ No newline at end of file | 261 | \ No newline at end of file |
| 262 | +module.exports.downloadAndMoveSubtitle = downloadAndMoveSubtitle; | ||
| 263 | +module.exports.downloadAndMoveAudio = downloadAndMoveAudio; | ||
| 181 | \ No newline at end of file | 264 | \ No newline at end of file |
helpers/parameters.js
| 1 | function getServiceType(service_type) { | 1 | function getServiceType(service_type) { |
| 2 | - switch(service_type) { | 2 | + switch(service_type) { |
| 3 | case 'video-legenda': | 3 | case 'video-legenda': |
| 4 | return 2; | 4 | return 2; |
| 5 | break; | 5 | break; |
| @@ -19,6 +19,9 @@ function getServiceType(service_type) { | @@ -19,6 +19,9 @@ function getServiceType(service_type) { | ||
| 19 | case 'legenda': | 19 | case 'legenda': |
| 20 | return 5; | 20 | return 5; |
| 21 | break; | 21 | break; |
| 22 | + case 'audio': | ||
| 23 | + return 6; | ||
| 24 | + break; | ||
| 22 | } | 25 | } |
| 23 | }; | 26 | }; |
| 24 | 27 | ||
| @@ -152,6 +155,11 @@ function checkSubtitle(file) { | @@ -152,6 +155,11 @@ function checkSubtitle(file) { | ||
| 152 | return check_type(file, accepted_file_types) | 155 | return check_type(file, accepted_file_types) |
| 153 | }; | 156 | }; |
| 154 | 157 | ||
| 158 | +function checkAudio(file) { | ||
| 159 | + var accepted_file_types = ['mp3', 'wav']; | ||
| 160 | + return check_type(file, accepted_file_types) | ||
| 161 | +}; | ||
| 162 | + | ||
| 155 | function check_type(file, accepted_file_types) { | 163 | function check_type(file, accepted_file_types) { |
| 156 | 164 | ||
| 157 | var ext = file.substring(file.lastIndexOf('.') + 1).toLowerCase(); | 165 | var ext = file.substring(file.lastIndexOf('.') + 1).toLowerCase(); |
| @@ -189,5 +197,6 @@ module.exports.checkTransparency = checkTransparency; | @@ -189,5 +197,6 @@ module.exports.checkTransparency = checkTransparency; | ||
| 189 | 197 | ||
| 190 | module.exports.checkVideo = checkVideo; | 198 | module.exports.checkVideo = checkVideo; |
| 191 | module.exports.checkSubtitle = checkSubtitle; | 199 | module.exports.checkSubtitle = checkSubtitle; |
| 200 | +module.exports.checkAudio = checkAudio; | ||
| 192 | 201 | ||
| 193 | module.exports.errorMessage = errorMessage; | 202 | module.exports.errorMessage = errorMessage; |
| 194 | \ No newline at end of file | 203 | \ No newline at end of file |
helpers/properties.js
server.js
| @@ -4,6 +4,7 @@ var properties = require('./helpers/properties'); | @@ -4,6 +4,7 @@ var properties = require('./helpers/properties'); | ||
| 4 | var ep_texto = require('./endpoints/texto'); | 4 | var ep_texto = require('./endpoints/texto'); |
| 5 | var ep_ios = require('./endpoints/ios'); | 5 | var ep_ios = require('./endpoints/ios'); |
| 6 | var ep_video = require('./endpoints/video'); | 6 | var ep_video = require('./endpoints/video'); |
| 7 | +var ep_audio = require('./endpoints/audio'); | ||
| 7 | var ep_legenda = require('./endpoints/legenda'); | 8 | var ep_legenda = require('./endpoints/legenda'); |
| 8 | var ep_video_legenda = require('./endpoints/video_legenda'); | 9 | var ep_video_legenda = require('./endpoints/video_legenda'); |
| 9 | 10 | ||
| @@ -43,6 +44,11 @@ app.post('/api', function(req, res) { | @@ -43,6 +44,11 @@ app.post('/api', function(req, res) { | ||
| 43 | ep_video.init(req, res); | 44 | ep_video.init(req, res); |
| 44 | break; | 45 | break; |
| 45 | 46 | ||
| 47 | + /* Tipo de Serviço: Só o Áudio */ | ||
| 48 | + case 'audio': | ||
| 49 | + ep_audio.init(req, res); | ||
| 50 | + break; | ||
| 51 | + | ||
| 46 | /* Tipo de Serviço: Só a Legenda */ | 52 | /* Tipo de Serviço: Só a Legenda */ |
| 47 | case 'legenda': | 53 | case 'legenda': |
| 48 | ep_legenda.init(req, res); | 54 | ep_legenda.init(req, res); |