File: source/data-process.js
/**
* Function that converts Base64 string into Blob object.
* This is referenced from devnull69@stackoverflow.com #6850276.
* @method _base64ToBlob
* @private
* @for Skylink
* @since 0.1.0
*/
Skylink.prototype._base64ToBlob = function(dataURL) {
var byteString = atob(dataURL);
// write the bytes of the string to an ArrayBuffer
var ab = new ArrayBuffer(byteString.length);
var ia = new Uint8Array(ab);
for (var j = 0; j < byteString.length; j++) {
ia[j] = byteString.charCodeAt(j);
}
// write the ArrayBuffer to a blob, and you're done
return new Blob([ab]);
};
/**
* Function that converts a Blob object into Base64 string.
* @method _blobToBase64
* @private
* @for Skylink
* @since 0.1.0
*/
Skylink.prototype._blobToBase64 = function(data, callback) {
var fileReader = new FileReader();
fileReader.onload = function() {
// Load Blob as dataurl base64 string
var base64BinaryString = fileReader.result.split(',')[1];
callback(base64BinaryString);
};
fileReader.readAsDataURL(data);
};
/**
* Function that converts a Blob object into ArrayBuffer object.
* @method _blobToArrayBuffer
* @private
* @for Skylink
* @since 0.1.0
*/
Skylink.prototype._blobToArrayBuffer = function(data, callback) {
var self = this;
var fileReader = new FileReader();
fileReader.onload = function() {
// Load Blob as dataurl base64 string
if (AdapterJS.webrtcDetectedType === 'plugin') {
callback(new Int8Array(fileReader.result));
} else {
callback(fileReader.result);
}
};
fileReader.readAsArrayBuffer(data);
};
/**
* Function that chunks Blob object based on the data chunk size provided.
* If provided Blob object size is lesser than or equals to the chunk size, it should return an array
* of length of <code>1</code>.
* @method _chunkBlobData
* @private
* @for Skylink
* @since 0.5.2
*/
Skylink.prototype._chunkBlobData = function(blob, chunkSize) {
var chunksArray = [];
var startCount = 0;
var endCount = 0;
var blobByteSize = blob.size;
if (blobByteSize > chunkSize) {
// File Size greater than Chunk size
while ((blobByteSize - 1) > endCount) {
endCount = startCount + chunkSize;
chunksArray.push(blob.slice(startCount, endCount));
startCount += chunkSize;
}
if ((blobByteSize - (startCount + 1)) > 0) {
chunksArray.push(blob.slice(startCount, blobByteSize - 1));
}
} else {
// File Size below Chunk size
chunksArray.push(blob);
}
return chunksArray;
};
/**
* Function that chunks large string into string chunks based on the data chunk size provided.
* If provided string length is lesser than or equals to the chunk size, it should return an array
* of length of <code>1</code>.
* @method _chunkDataURL
* @private
* @for Skylink
* @since 0.6.1
*/
Skylink.prototype._chunkDataURL = function(dataURL, chunkSize) {
var outputStr = dataURL; //encodeURIComponent(dataURL);
var dataURLArray = [];
var startCount = 0;
var endCount = 0;
var dataByteSize = dataURL.size || dataURL.length;
if (dataByteSize > chunkSize) {
// File Size greater than Chunk size
while ((dataByteSize - 1) > endCount) {
endCount = startCount + chunkSize;
dataURLArray.push(outputStr.slice(startCount, endCount));
startCount += chunkSize;
}
if ((dataByteSize - (startCount + 1)) > 0) {
chunksArray.push(outputStr.slice(startCount, dataByteSize - 1));
}
} else {
// File Size below Chunk size
dataURLArray.push(outputStr);
}
return dataURLArray;
};