File: source/data-process.js

  1. /**
  2. * Function that converts Base64 string into Blob object.
  3. * This is referenced from devnull69@stackoverflow.com #6850276.
  4. * @method _base64ToBlob
  5. * @private
  6. * @for Skylink
  7. * @since 0.1.0
  8. */
  9. Skylink.prototype._base64ToBlob = function(dataURL) {
  10. var byteString = atob(dataURL);
  11. // write the bytes of the string to an ArrayBuffer
  12. var ab = new ArrayBuffer(byteString.length);
  13. var ia = new Uint8Array(ab);
  14. for (var j = 0; j < byteString.length; j++) {
  15. ia[j] = byteString.charCodeAt(j);
  16. }
  17. // write the ArrayBuffer to a blob, and you're done
  18. return new Blob([ab]);
  19. };
  20.  
  21. /**
  22. * Function that converts a Blob object into Base64 string.
  23. * @method _blobToBase64
  24. * @private
  25. * @for Skylink
  26. * @since 0.1.0
  27. */
  28. Skylink.prototype._blobToBase64 = function(data, callback) {
  29. var fileReader = new FileReader();
  30. fileReader.onload = function() {
  31. // Load Blob as dataurl base64 string
  32. var base64BinaryString = fileReader.result.split(',')[1];
  33. callback(base64BinaryString);
  34. };
  35. fileReader.readAsDataURL(data);
  36. };
  37.  
  38. /**
  39. * Function that converts a Blob object into ArrayBuffer object.
  40. * @method _blobToArrayBuffer
  41. * @private
  42. * @for Skylink
  43. * @since 0.1.0
  44. */
  45. Skylink.prototype._blobToArrayBuffer = function(data, callback) {
  46. var self = this;
  47. var fileReader = new FileReader();
  48. fileReader.onload = function() {
  49. // Load Blob as dataurl base64 string
  50. if (AdapterJS.webrtcDetectedType === 'plugin') {
  51. callback(new Int8Array(fileReader.result));
  52. } else {
  53. callback(fileReader.result);
  54. }
  55. };
  56. fileReader.readAsArrayBuffer(data);
  57. };
  58.  
  59. /**
  60. * Function that chunks Blob object based on the data chunk size provided.
  61. * If provided Blob object size is lesser than or equals to the chunk size, it should return an array
  62. * of length of <code>1</code>.
  63. * @method _chunkBlobData
  64. * @private
  65. * @for Skylink
  66. * @since 0.5.2
  67. */
  68. Skylink.prototype._chunkBlobData = function(blob, chunkSize) {
  69. var chunksArray = [];
  70. var startCount = 0;
  71. var endCount = 0;
  72. var blobByteSize = blob.size;
  73.  
  74. if (blobByteSize > chunkSize) {
  75. // File Size greater than Chunk size
  76. while ((blobByteSize - 1) > endCount) {
  77. endCount = startCount + chunkSize;
  78. chunksArray.push(blob.slice(startCount, endCount));
  79. startCount += chunkSize;
  80. }
  81. if ((blobByteSize - (startCount + 1)) > 0) {
  82. chunksArray.push(blob.slice(startCount, blobByteSize - 1));
  83. }
  84. } else {
  85. // File Size below Chunk size
  86. chunksArray.push(blob);
  87. }
  88. return chunksArray;
  89. };
  90.  
  91. /**
  92. * Function that chunks large string into string chunks based on the data chunk size provided.
  93. * If provided string length is lesser than or equals to the chunk size, it should return an array
  94. * of length of <code>1</code>.
  95. * @method _chunkDataURL
  96. * @private
  97. * @for Skylink
  98. * @since 0.6.1
  99. */
  100. Skylink.prototype._chunkDataURL = function(dataURL, chunkSize) {
  101. var outputStr = dataURL; //encodeURIComponent(dataURL);
  102. var dataURLArray = [];
  103. var startCount = 0;
  104. var endCount = 0;
  105. var dataByteSize = dataURL.size || dataURL.length;
  106.  
  107. if (dataByteSize > chunkSize) {
  108. // File Size greater than Chunk size
  109. while ((dataByteSize - 1) > endCount) {
  110. endCount = startCount + chunkSize;
  111. dataURLArray.push(outputStr.slice(startCount, endCount));
  112. startCount += chunkSize;
  113. }
  114. if ((dataByteSize - (startCount + 1)) > 0) {
  115. chunksArray.push(outputStr.slice(startCount, dataByteSize - 1));
  116. }
  117. } else {
  118. // File Size below Chunk size
  119. dataURLArray.push(outputStr);
  120. }
  121.  
  122. return dataURLArray;
  123. };