Commit 38c2724205c8e0822d8d94aed200a78ccc7329a0
1 parent
da12bded
Exists in
master
and in
1 other branch
Refatoração requisições POST e download video_url
- Refatora requisições POST para um unico lugar (apenas em video.js) - Move caminho de uploads hardcoded para properties - Implementa download de arquivo de video (apenas em video.js) - Retorna ID do arquivo quando tem callback (apenas em video.js)
Showing
5 changed files
with
85 additions
and
80 deletions
Show diff stats
endpoints/video.js
| 1 | 1 | var parameters = require('../helpers/parameters'); |
| 2 | 2 | var properties = require('../helpers/properties'); |
| 3 | +var requests = require('../helpers/requests'); | |
| 3 | 4 | |
| 4 | 5 | var exec = require('child_process').exec, child; |
| 5 | 6 | var querystring = require('querystring'); |
| ... | ... | @@ -25,25 +26,64 @@ function init(req, res) { |
| 25 | 26 | return; |
| 26 | 27 | } |
| 27 | 28 | |
| 29 | + var video; | |
| 30 | + | |
| 28 | 31 | /* Checa se o arquivo de vídeo submetivo possui uma extensão válida */ |
| 29 | - if (parameters.checkVideo(req.files.video.name) === false) { | |
| 30 | - res.send(500, parameters.errorMessage('Vídeo com Extensão Inválida')); | |
| 32 | + if (req.files.video !== undefined) { | |
| 33 | + if (parameters.checkVideo(req.files.video.name) === false) { | |
| 34 | + res.send(500, parameters.errorMessage('Vídeo enviado com extensão inválida')); | |
| 35 | + return; | |
| 36 | + } | |
| 37 | + | |
| 38 | + video = { | |
| 39 | + 'name': req.files.video.name, | |
| 40 | + 'path': req.files.video.path | |
| 41 | + } | |
| 42 | + | |
| 43 | + processVideo(id, video, req, res); | |
| 44 | + | |
| 45 | + } else if (req.body.video_url !== undefined) { | |
| 46 | + http.get(req.body.video_url, function(response) { | |
| 47 | + console.log("video_url: downloading"); | |
| 48 | + | |
| 49 | + response.pipe(fs.createWriteStream(id)); | |
| 50 | + | |
| 51 | + video = { | |
| 52 | + 'name': req.body.video_url.substring(req.body.video_url.lastIndexOf('/') + 1), | |
| 53 | + 'path': id | |
| 54 | + } | |
| 55 | + | |
| 56 | + console.log("download video"); | |
| 57 | + | |
| 58 | + processVideo(id, video, req, res); | |
| 59 | + | |
| 60 | + }).on('error', function(e) { | |
| 61 | + error = 'Problema ao carregar video_url: ' + e.message; | |
| 62 | + | |
| 63 | + res.send(500, parameters.errorMessage(error)); | |
| 64 | + }); | |
| 65 | + | |
| 66 | + } else { | |
| 67 | + res.send(500, parameters.errorMessage('Video deve ser enviado como parâmetro "video" ou como "video_url"')); | |
| 31 | 68 | return; |
| 32 | 69 | } |
| 70 | +}; | |
| 71 | + | |
| 33 | 72 | |
| 73 | +function processVideo(id, video, req, res) { | |
| 34 | 74 | /* Cria uma pasta cujo o nome é o ID atual */ |
| 35 | - mkdirp('/home/libras/vlibras-api/uploads/' + id, function(error) { | |
| 75 | + mkdirp(properties.uploads_folder + id, function(error) { | |
| 36 | 76 | |
| 37 | 77 | if (error) { console.log(error); res.send(500, parameters.errorMessage('Erro na criação da pasta com o ID: ' + id)); return; } |
| 38 | 78 | |
| 39 | 79 | /* Move o vídeo submetido para a pasta com o seu ID correspondente */ |
| 40 | - fs.rename(req.files.video.path, '/home/libras/vlibras-api/uploads/' + id + '/' + req.files.video.name, function(error) { | |
| 80 | + fs.rename(video.path, properties.uploads_folder + id + '/' + video.name, function(error) { | |
| 41 | 81 | if (error) { console.log(error); res.send(500, parameters.errorMessage('Erro ao mover o vídeo submetido')); return; } |
| 42 | 82 | }); |
| 43 | 83 | |
| 44 | 84 | /* Cria a linha de comando */ |
| 45 | 85 | var command_line = 'vlibras_user/vlibras-core/./vlibras ' + parameters.getServiceType(req.body.servico) + ' uploads/' + id + '/' + |
| 46 | - req.files.video.name + ' 1 ' + parameters.getPosition(req.body.posicao) + ' ' + parameters.getSize(req.body.tamanho) + ' ' + | |
| 86 | + video.name + ' 1 ' + parameters.getPosition(req.body.posicao) + ' ' + parameters.getSize(req.body.tamanho) + ' ' + | |
| 47 | 87 | parameters.getTransparency(req.body.transparencia) + ' ' + id + '> /tmp/core_log 2>&1'; |
| 48 | 88 | |
| 49 | 89 | console.log(command_line); |
| ... | ... | @@ -74,93 +114,30 @@ function init(req, res) { |
| 74 | 114 | child.on('close', function(code, signal){ |
| 75 | 115 | if (code !== 0) { |
| 76 | 116 | var path = url.parse(req.body.callback); |
| 77 | - | |
| 78 | 117 | var data = querystring.stringify( { 'error': 'Erro no Core', 'code': code } ); |
| 79 | 118 | |
| 80 | - var options = { | |
| 81 | - host: path.hostname, | |
| 82 | - port: path.port, | |
| 83 | - path: path.path, | |
| 84 | - method: 'POST', | |
| 85 | - headers: { | |
| 86 | - 'Content-Type': 'application/x-www-form-urlencoded', | |
| 87 | - 'Content-Length': Buffer.byteLength(data) | |
| 88 | - } | |
| 89 | - }; | |
| 90 | - | |
| 91 | - var requesting = http.request(options, function(res) { | |
| 92 | - res.setEncoding('utf8'); | |
| 93 | - }); | |
| 94 | - | |
| 95 | - requesting.on('error', function (e) { | |
| 96 | - console.log("The callback URL can not be reachable"); | |
| 97 | - }); | |
| 98 | - | |
| 99 | - requesting.write(data); | |
| 100 | - requesting.end(); | |
| 119 | + requests.postRequest(path, data); | |
| 101 | 120 | |
| 102 | 121 | return; |
| 103 | 122 | } |
| 104 | 123 | |
| 105 | 124 | var path = url.parse(req.body.callback); |
| 106 | - | |
| 107 | 125 | var data = querystring.stringify({ 'response' : 'http://' + properties.SERVER_IP + ':' + properties.port + '/' + id + '.flv' }); |
| 108 | 126 | |
| 109 | - var options = { | |
| 110 | - host: path.hostname, | |
| 111 | - port: path.port, | |
| 112 | - path: path.path, | |
| 113 | - method: 'POST', | |
| 114 | - headers: { | |
| 115 | - 'Content-Type': 'application/x-www-form-urlencoded', | |
| 116 | - 'Content-Length': Buffer.byteLength(data) | |
| 117 | - } | |
| 118 | - }; | |
| 119 | - | |
| 120 | - var requesting = http.request(options, function(res) { | |
| 121 | - res.setEncoding('utf8'); | |
| 122 | - }); | |
| 123 | - | |
| 124 | - requesting.on('error', function (e) { | |
| 125 | - console.log("The callback URL can not be reachable"); | |
| 126 | - }); | |
| 127 | - | |
| 128 | - requesting.write(data); | |
| 129 | - requesting.end(); | |
| 127 | + requests.postRequest(path, data); | |
| 130 | 128 | }); |
| 131 | 129 | |
| 132 | 130 | /* Listener que dispara quando a requisição ao core da erro */ |
| 133 | 131 | child.on('error', function(code, signal){ |
| 134 | - var path = url.parse(req.body.callback); | |
| 132 | + var path = url.parse(req.body.callback); | |
| 133 | + var data = querystring.stringify( { 'error': 'Erro na chamada ao core', 'code': code, 'id': id } ); | |
| 135 | 134 | |
| 136 | - var data = querystring.stringify( { 'error': 'Erro na chamada ao Core', 'code': code } ); | |
| 137 | - | |
| 138 | - var options = { | |
| 139 | - host: path.hostname, | |
| 140 | - port: path.port, | |
| 141 | - path: path.path, | |
| 142 | - method: 'POST', | |
| 143 | - headers: { | |
| 144 | - 'Content-Type': 'application/x-www-form-urlencoded', | |
| 145 | - 'Content-Length': Buffer.byteLength(data) | |
| 146 | - } | |
| 147 | - }; | |
| 148 | - | |
| 149 | - var requesting = http.request(options, function(res) { | |
| 150 | - res.setEncoding('utf8'); | |
| 151 | - }); | |
| 152 | - | |
| 153 | - requesting.on('error', function (e) { | |
| 154 | - console.log("The callback URL can not be reachable"); | |
| 155 | - }); | |
| 156 | - | |
| 157 | - requesting.write(data); | |
| 158 | - requesting.end(); | |
| 135 | + requests.postRequest(path, data); | |
| 159 | 136 | }); |
| 160 | 137 | |
| 161 | - res.send(200); | |
| 138 | + res.send(200, JSON.stringify({ 'id': id })); | |
| 162 | 139 | } |
| 163 | 140 | }); |
| 164 | -}; | |
| 141 | +} | |
| 165 | 142 | |
| 166 | 143 | module.exports.init = init; | ... | ... |
endpoints/video_legenda.js
| ... | ... | @@ -38,17 +38,17 @@ function init(req, res) { |
| 38 | 38 | } |
| 39 | 39 | |
| 40 | 40 | /* Cria uma pasta cujo o nome é o ID atual */ |
| 41 | - mkdirp('/home/libras/vlibras-api/uploads/' + id, function(error) { | |
| 41 | + mkdirp(properties.uploads_folder + id, function(error) { | |
| 42 | 42 | |
| 43 | 43 | if (error) { console.log(error); return; } |
| 44 | 44 | |
| 45 | 45 | /* Move o vídeo submetido para a pasta com o seu ID correspondente */ |
| 46 | - fs.rename(req.files.video.path, '/home/libras/vlibras-api/uploads/' + id + '/' + req.files.video.name, function(error) { | |
| 46 | + fs.rename(req.files.video.path, properties.uploads_folder + id + '/' + req.files.video.name, function(error) { | |
| 47 | 47 | if (error) { console.log(error); res.send(500, parameters.errorMessage('Erro ao mover o vídeo submetido')); return; } |
| 48 | 48 | }); |
| 49 | 49 | |
| 50 | 50 | /* Move a legenda submetido para a pasta com o seu ID correspondente */ |
| 51 | - fs.rename(req.files.legenda.path, '/home/libras/vlibras-api/uploads/' + id + '/' + req.files.legenda.name, function(error) { | |
| 51 | + fs.rename(req.files.legenda.path, properties.uploads_folder + id + '/' + req.files.legenda.name, function(error) { | |
| 52 | 52 | if (error) { console.log(error); res.send(500, parameters.errorMessage('Erro ao mover a legenda submetido')); return; } |
| 53 | 53 | }); |
| 54 | 54 | ... | ... |
helpers/parameters.js
| ... | ... | @@ -143,7 +143,7 @@ function checkTransparency(transparency) { |
| 143 | 143 | }; |
| 144 | 144 | |
| 145 | 145 | function checkVideo(file) { |
| 146 | - var accepted_file_types = ['flv', 'ts', 'avi', 'mp4', 'mov', 'webm', 'wmv', 'mkv',]; | |
| 146 | + var accepted_file_types = ['flv', 'ts', 'avi', 'mp4', 'mov', 'webm', 'wmv', 'mkv']; | |
| 147 | 147 | return check_type(file, accepted_file_types) |
| 148 | 148 | }; |
| 149 | 149 | ... | ... |
helpers/properties.js
| 1 | 1 | var host = '0.0.0.0'; |
| 2 | 2 | var port = 5000; |
| 3 | 3 | var SERVER_IP = '150.165.204.30'; |
| 4 | -var uploads_folder = '/home/libras/vlibras-api/uploads/'; | |
| 4 | +var uploads_folder = './uploads/'; | |
| 5 | 5 | |
| 6 | 6 | module.exports.host = host; |
| 7 | 7 | module.exports.port = port; |
| 8 | +module.exports.uploads_folder = uploads_folder; | |
| 8 | 9 | module.exports.SERVER_IP = SERVER_IP; |
| 9 | 10 | \ No newline at end of file | ... | ... |
| ... | ... | @@ -0,0 +1,27 @@ |
| 1 | +var http = require('http'); | |
| 2 | + | |
| 3 | +function postRequest(path, data) { | |
| 4 | + var options = { | |
| 5 | + host: path.hostname, | |
| 6 | + port: path.port, | |
| 7 | + path: path.path, | |
| 8 | + method: 'POST', | |
| 9 | + headers: { | |
| 10 | + 'Content-Type': 'application/x-www-form-urlencoded', | |
| 11 | + 'Content-Length': Buffer.byteLength(data) | |
| 12 | + } | |
| 13 | + }; | |
| 14 | + | |
| 15 | + var requesting = http.request(options, function(res) { | |
| 16 | + res.setEncoding('utf8'); | |
| 17 | + }); | |
| 18 | + | |
| 19 | + requesting.on('error', function (e) { | |
| 20 | + console.log("The callback URL can not be reachable"); | |
| 21 | + }); | |
| 22 | + | |
| 23 | + requesting.write(data); | |
| 24 | + requesting.end(); | |
| 25 | +} | |
| 26 | + | |
| 27 | +module.exports.postRequest = postRequest; | |
| 0 | 28 | \ No newline at end of file | ... | ... |