我發現了一種可行的方法,但可能不是理想的方法。
在這裏,我在flow.post
調用flow.write
如果status
是done
和currentTestChunk > numberOfChunks
。我做的大於檢查,因爲有時flow.post
發送status done
多次提到here。
編輯:我添加了一種方法來創建文件後清理塊。
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 });
}
})
我不得不修改flow.post
的回調發送currentTestChunk
和numberOfChunks
。
文件:流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);
}
}
與onDone flow.write通話flow.clean如果你要刪除的數據塊。
$.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);
}
太棒了! 任何機會,你可以提交PR到flow.Js github回購?這裏是一個鏈接:https://github.com/flowjs/flow.js/issues/17#issuecomment-49737531 – flashpunk
嘿@flashpunk,當我有機會,我會研究。 – cleversprocket
我沒有看到在第一次檢查後立即在回調中重複檢查「if(currentTestChunk> numberOfChunks)」如何可以幫助修復多次觸發完成狀態的問題。 –