I encoded the meteor application by sending fragments of binary data to the server using the FileReader API.
Client-side slicing processing method:
var readSlice = function() { var start, end; start = chunk * chunkSize; if (start > file.size) { start = end + 1; } end = start + (chunkSize - 1) >= file.size ? file.size : start + (chunkSize - 1); fileReader.onload = function(event) { if (++chunk <= chunks) { Meteor .apply("saveChunk", [ event.target.result.split(',')[1], file.name, clientRegistration, now, (chunk === chunks) ], { wait : true }); readSlice(); } else { console.log("reading done"); } }; fileReader.readAsDataURL(blobSlice.call(file, start, end)); };
Slices are received by the server in the correct order and combined as follows:
saveChunk : function(chunk, name, registration, timestamp, last) { ... tempFile = Meteor.fileTemp + "/" + identifier; fs.appendFile(tempFile, new Buffer(chunk, "base64"), { encoding : "base64", mode : 438, flag : "w" }, function(error) { if (error) { throw (new Meteor.Error(500, "Failed to save file.", err)); } }); }
This process works almost perfectly. However, these are minor differences in the file, resulting in corrupt output. Binary differences occur at a point in the file where the output ist is split, so maybe something is wrong with the split call in the event.target.result , or maybe I'm missing something obvious from the coding point of view. ..?
The problem seems to be due to splitting, since the diff of the merged file with the source file shows the following interesting pattern:
