2014-05-03 8 views
10

Non riesco a capire come utilizzare la libreria flow.js con un backend di nodo e basando il mio codice sull'esempio sul github flow.js.riassembla binario dopo il caricamento di flow.js su nodo/server express

Sto recuperando i file BLOB, ma non sto creando il file binario in seguito al completamento dell'upload. The Get finale non è sempre attivato o il mio percorso è sbagliato:

app.get('/download/:identifier', function(req, res){ 
    console.log('we writin') 
    flow.write(req.params.identifier, res); 
    }); 

Qualcuno ha qualche esperienza con questo potrebbe ottenere come un milione di punti StackOverflow perché questo sembra essere un problema comune quando si utilizza node.js e il flusso. js e qui sono altre due domande senza risposta:

Flowjs file upload - AngularJS and Node Reassembling file chunks produced in a multi-part upload

risposta

8

Ho trovato un metodo che funziona ma potrebbe non essere l'approccio ideale.

Qui sto chiamando flow.write in flow.post se status è done e currentTestChunk > numberOfChunks. Faccio il maggiore di controllo perché a volte flow.post invia status done più di una volta come indicato here.

Modifica: Ho aggiunto un metodo per pulire i blocchi dopo aver creato il file.

flow.post(req, function(status, filename, original_filename, identifier, currentTestChunk, numberOfChunks) { 
     console.log('POST', status, original_filename, identifier); 
     res.send(200); 
     if (status === 'done' && currentTestChunk > numberOfChunks) { 
      var stream = fs.createWriteStream('./tmp/' + filename); 
      //EDIT: I removed options {end: true} because it isn't needed 
      //and added {onDone: flow.clean} to remove the chunks after writing 
      //the file. 
      flow.write(identifier, stream, { onDone: flow.clean });    
     }    
    }) 

ho dovuto modificare richiamata s' flow.post per inviare currentTestChunk e numberOfChunks.

File: portate node.js

$.post = function(req, callback){ 

//There's some codez here that we can overlook... 

    fs.rename(files[$.fileParameterName].path, chunkFilename, function(){ 

    // Do we have all the chunks? 
    var currentTestChunk = 1; 
    var numberOfChunks = Math.max(Math.floor(totalSize/(chunkSize*1.0)), 1); 
    var testChunkExists = function(){ 
      fs.exists(getChunkFilename(currentTestChunk, identifier), function(exists){ 
      if(exists){ 
       currentTestChunk++; 
       if(currentTestChunk>numberOfChunks) { 

       //Add currentTestChunk and numberOfChunks to the callback 

       callback('done', filename, original_filename, identifier, currentTestChunk, numberOfChunks); 
       } else { 
       // Recursion 
       testChunkExists(); 
       } 
      } else { 

       //Add currentTestChunk and numberOfChunks to the callback 

       callback('partly_done', filename, original_filename, identifier, currentTestChunk, numberOfChunks); 
      } 
      }); 
     } 
    testChunkExists(); 
    }); 
} else { 
     callback(validation, filename, original_filename, identifier); 
} 

}

nei call flow.clean flow.write con onDone se si desidera rimuovere i pezzi.

$.write = function(identifier, writableStream, options) { 
     options = options || {}; 
     options.end = (typeof options['end'] == 'undefined' ? true : options['end']); 

     // Iterate over each chunk 
     var pipeChunk = function(number) { 

      var chunkFilename = getChunkFilename(number, identifier); 
      fs.exists(chunkFilename, function(exists) { 

       if (exists) { 
        // If the chunk with the current number exists, 
        // then create a ReadStream from the file 
        // and pipe it to the specified writableStream. 
        var sourceStream = fs.createReadStream(chunkFilename); 
        sourceStream.pipe(writableStream, { 
         end: false 
        }); 
        sourceStream.on('end', function() { 
         // When the chunk is fully streamed, 
         // jump to the next one 
         pipeChunk(number + 1); 
        }); 
       } else { 
        // When all the chunks have been piped, end the stream 
        if (options.end) { 
          writableStream.end(); 
         } 

        //Options.onDone contains flow.clean so here I'm deleting all the chunked files. 

        if (options.onDone) { 
         options.onDone(identifier); 
        } 
       } 
      }); 
     } 
     pipeChunk(1); 
    } 
+0

Fantastico! Qualche possibilità di inviare un PR al flusso. ecco un link: https://github.com/flowjs/flow.js/issues/17#issuecomment-49737531 – flashpunk

+0

Ehi @flashpunk, lo esaminerò quando ne avrò l'occasione. – cleversprocket

+0

Non vedo come ripetere il controllo, "if (currentTestChunk> numberOfChunks)", nel callback subito dopo il primo controllo può aiutare a risolvere il problema dello stato di fatto che viene attivato più di una volta. –

3

Ok, quindi ho lavorato su questo e sono venuto con questo, si spera otterrà qualcuno ha iniziato ...

exports.post = function (req, res, next) { 

    flow.post(req, function(status, filename, original_filename, identifier) { 

     console.log('status: '+ status, filename, original_filename, identifier); 

     if(status==='done'){ 

      var s = fs.createWriteStream('./uploads/' + filename); 
      s.on('finish', function() { 

       res.send(200, { 
        // NOTE: Uncomment this funciton to enable cross-domain request. 
        //'Access-Control-Allow-Origin': '*' 
       }); 

      }); 

      flow.write(identifier, s, {end: true}); 
     } 

    }); 

}; 
Problemi correlati