Commit 6271956e700dc41be05048606e551649d4960b96

Authored by Fernando Brito
1 parent 10710b3b
Exists in master and in 1 other branch devel

Corrige download de arquivos

video_url e legenda_url não esperavam acabar o download
Showing 1 changed file with 38 additions and 14 deletions   Show diff stats
helpers/files.js
@@ -54,15 +54,27 @@ function downloadAndMoveVideo(folder, req, locals, callback) { @@ -54,15 +54,27 @@ function downloadAndMoveVideo(folder, req, locals, callback) {
54 54
55 var path = folder + '/' + filename; 55 var path = folder + '/' + filename;
56 56
  57 + // Cria o stream para escrita
  58 + var file = fs.createWriteStream(path);
  59 +
57 // Salva o arquivo em disco 60 // Salva o arquivo em disco
58 - response.pipe(fs.createWriteStream(path)); 61 + response.pipe(file);
  62 +
  63 + // Quando a escrita acabar
  64 + file.on('finish', function() {
59 65
60 - // Retorna o vídeo baixado  
61 - locals.video = {  
62 - 'path': path  
63 - } 66 + // Fecha o arquivo
  67 + file.close(function() {
64 68
65 - return callback(); 69 + // Retorna o vídeo baixado
  70 + locals.video = {
  71 + 'path': path
  72 + }
  73 +
  74 + // Chama o callback para prosseguir execução
  75 + callback();
  76 + });
  77 + });
66 78
67 // Se deu erro na requisição de baixar o vídeo 79 // Se deu erro na requisição de baixar o vídeo
68 }).on('error', function(e) { 80 }).on('error', function(e) {
@@ -128,15 +140,27 @@ function downloadAndMoveSubtitle(folder, req, locals, callback) { @@ -128,15 +140,27 @@ function downloadAndMoveSubtitle(folder, req, locals, callback) {
128 140
129 var path = folder + '/' + filename; 141 var path = folder + '/' + filename;
130 142
131 - // Salva o arquivo em disco  
132 - response.pipe(fs.createWriteStream(path)); 143 + // Cria o stream para escrita
  144 + var file = fs.createWriteStream(path);
133 145
134 - // Retorna o vídeo baixado  
135 - locals.subtitle = {  
136 - 'path': path  
137 - }  
138 -  
139 - return callback(); 146 + // Salva o arquivo em disco
  147 + response.pipe(file);
  148 +
  149 + // Quando a escrita acabar
  150 + file.on('finish', function() {
  151 +
  152 + // Fecha o arquivo
  153 + file.close(function() {
  154 +
  155 + // Retorna o vídeo baixado
  156 + locals.subtitle = {
  157 + 'path': path
  158 + }
  159 +
  160 + // Chama o callback para prosseguir execução
  161 + callback();
  162 + });
  163 + });
140 164
141 // Se deu erro na requisição de baixar a legenda 165 // Se deu erro na requisição de baixar a legenda
142 }).on('error', function(e) { 166 }).on('error', function(e) {