71 lines
2.1 KiB
JavaScript
71 lines
2.1 KiB
JavaScript
// Last time updated at May 23, 2015, 08:32:23
|
|
|
|
// Latest file can be found here: https://cdn.webrtc-experiment.com/ConcatenateBlobs.js
|
|
|
|
// Muaz Khan - www.MuazKhan.com
|
|
// MIT License - www.WebRTC-Experiment.com/licence
|
|
// Source Code - https://github.com/muaz-khan/ConcatenateBlobs
|
|
// Demo - https://www.WebRTC-Experiment.com/ConcatenateBlobs/
|
|
|
|
// ___________________
|
|
// ConcatenateBlobs.js
|
|
|
|
// Simply pass array of blobs.
|
|
// This javascript library will concatenate all blobs in single "Blob" object.
|
|
|
|
(function() {
|
|
function ConcatenateBlobs (blobs, type, callback) {
|
|
var buffers = [];
|
|
|
|
var index = 0;
|
|
|
|
function readAsArrayBuffer() {
|
|
if (!blobs[index]) {
|
|
return concatenateBuffers();
|
|
}
|
|
var reader = new FileReader();
|
|
reader.onload = function(event) {
|
|
buffers.push(event.target.result);
|
|
index++;
|
|
readAsArrayBuffer();
|
|
};
|
|
reader.readAsArrayBuffer(blobs[index]);
|
|
}
|
|
|
|
readAsArrayBuffer();
|
|
|
|
function concatenateBuffers() {
|
|
var byteLength = 0;
|
|
buffers.forEach(function(buffer) {
|
|
byteLength += buffer.byteLength;
|
|
});
|
|
|
|
var tmp = new Uint16Array(byteLength);
|
|
var lastOffset = 0;
|
|
buffers.forEach(function(buffer) {
|
|
// BYTES_PER_ELEMENT == 2 for Uint16Array
|
|
var reusableByteLength = buffer.byteLength;
|
|
if (reusableByteLength % 2 != 0) {
|
|
buffer = buffer.slice(0, reusableByteLength - 1)
|
|
}
|
|
tmp.set(new Uint16Array(buffer), lastOffset);
|
|
lastOffset += reusableByteLength;
|
|
});
|
|
|
|
var blob = new Blob([tmp.buffer], {
|
|
type: type
|
|
});
|
|
|
|
callback(blob);
|
|
}
|
|
}
|
|
|
|
if(typeof modules !== 'undefined') {
|
|
modules.export = ConcatenateBlobs;
|
|
}
|
|
|
|
if(typeof window !== 'undefined') {
|
|
window.ConcatenateBlobs = ConcatenateBlobs;
|
|
}
|
|
})();
|