2014-05-03 22 views
10

Tôi không thể tìm hiểu cách sử dụng thư viện flow.js với nút phụ trợ và đặt mã của tôi khỏi mẫu trên flow.js github.lắp ráp lại nhị phân sau khi tải lên flow.js trên nút/máy chủ tốc

Tôi đang nhận tệp blob, nhưng tôi không xây dựng tệp nhị phân sau khi quá trình tải lên hoàn tất. Các get thức là không nhận được kích hoạt hoặc tuyến đường của tôi là sai:

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

ai có bất kỳ kinh nghiệm với điều này có thể nhận được như một triệu điểm stackoverflow vì điều này dường như là một vấn đề phổ biến khi sử dụng Node.js và dòng chảy. js và đây là hai câu hỏi chưa được trả lời khác:

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

Trả lời

8

Tôi đã tìm thấy một phương pháp hoạt động nhưng có thể không phải là phương pháp lý tưởng.

Ở đây tôi đang gọi flow.write trong flow.post nếu statusdonecurrentTestChunk > numberOfChunks. Tôi làm lớn hơn séc vì đôi khi flow.post gửi status done nhiều lần như đã đề cập here.

Chỉnh sửa: Tôi đã thêm một cách để xóa các khối sau khi tạo tệp.

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 });    
     }    
    }) 

tôi đã phải sửa đổi callback flow.post 's để gửi currentTestChunknumberOfChunks.

File: dòng chảy 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); 
} 

}

Trong flow.clean gọi flow.write với onDone nếu bạn muốn loại bỏ các khối.

$.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

Tuyệt vời! Bất kỳ cơ hội nào bạn có thể gửi PR cho luồng.Js github repo? đây là một liên kết: https://github.com/flowjs/flow.js/issues/17#issuecomment-49737531 – flashpunk

+0

Hey @flashpunk, tôi sẽ xem xét điều đó khi tôi có cơ hội. – cleversprocket

+0

Tôi không thấy làm thế nào lặp lại kiểm tra, "nếu (currentTestChunk> numberOfChunks)", trong cuộc gọi lại ngay sau khi kiểm tra đầu tiên có thể giúp khắc phục vấn đề của tình trạng thực hiện được kích hoạt nhiều lần. –

3

Ok, do đó, tôi đã làm việc về vấn đề này và đã đưa ra với điều này, hy vọng nó sẽ nhận được một người nào đó bắt đầu ...

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}); 
     } 

    }); 

}; 
Các vấn đề liên quan