Blob.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* Blob.js
  2. * A Blob implementation.
  3. * 2013-06-20
  4. *
  5. * By Eli Grey, http://eligrey.com
  6. * By Devin Samarin, https://github.com/eboyjr
  7. * License: X11/MIT
  8. * See LICENSE.md
  9. */
  10. /*global self, unescape */
  11. /*jslint bitwise: true, regexp: true, confusion: true, es5: true, vars: true, white: true,
  12. plusplus: true */
  13. /*! @source http://purl.eligrey.com/github/Blob.js/blob/master/Blob.js */
  14. if (typeof Blob !== "function" || typeof URL === "undefined")
  15. if (typeof Blob === "function" && typeof webkitURL !== "undefined") var URL = webkitURL;
  16. else var Blob = (function (view) {
  17. "use strict";
  18. var BlobBuilder = view.BlobBuilder || view.WebKitBlobBuilder || view.MozBlobBuilder || view.MSBlobBuilder || (function(view) {
  19. var
  20. get_class = function(object) {
  21. return Object.prototype.toString.call(object).match(/^\[object\s(.*)\]$/)[1];
  22. }
  23. , FakeBlobBuilder = function BlobBuilder() {
  24. this.data = [];
  25. }
  26. , FakeBlob = function Blob(data, type, encoding) {
  27. this.data = data;
  28. this.size = data.length;
  29. this.type = type;
  30. this.encoding = encoding;
  31. }
  32. , FBB_proto = FakeBlobBuilder.prototype
  33. , FB_proto = FakeBlob.prototype
  34. , FileReaderSync = view.FileReaderSync
  35. , FileException = function(type) {
  36. this.code = this[this.name = type];
  37. }
  38. , file_ex_codes = (
  39. "NOT_FOUND_ERR SECURITY_ERR ABORT_ERR NOT_READABLE_ERR ENCODING_ERR "
  40. + "NO_MODIFICATION_ALLOWED_ERR INVALID_STATE_ERR SYNTAX_ERR"
  41. ).split(" ")
  42. , file_ex_code = file_ex_codes.length
  43. , real_URL = view.URL || view.webkitURL || view
  44. , real_create_object_URL = real_URL.createObjectURL
  45. , real_revoke_object_URL = real_URL.revokeObjectURL
  46. , URL = real_URL
  47. , btoa = view.btoa
  48. , atob = view.atob
  49. , can_apply_typed_arrays = false
  50. , can_apply_typed_arrays_test = function(pass) {
  51. can_apply_typed_arrays = !pass;
  52. }
  53. , ArrayBuffer = view.ArrayBuffer
  54. , Uint8Array = view.Uint8Array
  55. ;
  56. FakeBlob.fake = FB_proto.fake = true;
  57. while (file_ex_code--) {
  58. FileException.prototype[file_ex_codes[file_ex_code]] = file_ex_code + 1;
  59. }
  60. try {
  61. if (Uint8Array) {
  62. can_apply_typed_arrays_test.apply(0, new Uint8Array(1));
  63. }
  64. } catch (ex) {}
  65. if (!real_URL.createObjectURL) {
  66. URL = view.URL = {};
  67. }
  68. URL.createObjectURL = function(blob) {
  69. var
  70. type = blob.type
  71. , data_URI_header
  72. ;
  73. if (type === null) {
  74. type = "application/octet-stream";
  75. }
  76. if (blob instanceof FakeBlob) {
  77. data_URI_header = "data:" + type;
  78. if (blob.encoding === "base64") {
  79. return data_URI_header + ";base64," + blob.data;
  80. } else if (blob.encoding === "URI") {
  81. return data_URI_header + "," + decodeURIComponent(blob.data);
  82. } if (btoa) {
  83. return data_URI_header + ";base64," + btoa(blob.data);
  84. } else {
  85. return data_URI_header + "," + encodeURIComponent(blob.data);
  86. }
  87. } else if (real_create_object_URL) {
  88. return real_create_object_URL.call(real_URL, blob);
  89. }
  90. };
  91. URL.revokeObjectURL = function(object_URL) {
  92. if (object_URL.substring(0, 5) !== "data:" && real_revoke_object_URL) {
  93. real_revoke_object_URL.call(real_URL, object_URL);
  94. }
  95. };
  96. FBB_proto.append = function(data/*, endings*/) {
  97. var bb = this.data;
  98. // decode data to a binary string
  99. if (Uint8Array && (data instanceof ArrayBuffer || data instanceof Uint8Array)) {
  100. if (can_apply_typed_arrays) {
  101. bb.push(String.fromCharCode.apply(String, new Uint8Array(data)));
  102. } else {
  103. var
  104. str = ""
  105. , buf = new Uint8Array(data)
  106. , i = 0
  107. , buf_len = buf.length
  108. ;
  109. for (; i < buf_len; i++) {
  110. str += String.fromCharCode(buf[i]);
  111. }
  112. }
  113. } else if (get_class(data) === "Blob" || get_class(data) === "File") {
  114. if (FileReaderSync) {
  115. var fr = new FileReaderSync;
  116. bb.push(fr.readAsBinaryString(data));
  117. } else {
  118. // async FileReader won't work as BlobBuilder is sync
  119. throw new FileException("NOT_READABLE_ERR");
  120. }
  121. } else if (data instanceof FakeBlob) {
  122. if (data.encoding === "base64" && atob) {
  123. bb.push(atob(data.data));
  124. } else if (data.encoding === "URI") {
  125. bb.push(decodeURIComponent(data.data));
  126. } else if (data.encoding === "raw") {
  127. bb.push(data.data);
  128. }
  129. } else {
  130. if (typeof data !== "string") {
  131. data += ""; // convert unsupported types to strings
  132. }
  133. // decode UTF-16 to binary string
  134. bb.push(unescape(encodeURIComponent(data)));
  135. }
  136. };
  137. FBB_proto.getBlob = function(type) {
  138. if (!arguments.length) {
  139. type = null;
  140. }
  141. return new FakeBlob(this.data.join(""), type, "raw");
  142. };
  143. FBB_proto.toString = function() {
  144. return "[object BlobBuilder]";
  145. };
  146. FB_proto.slice = function(start, end, type) {
  147. var args = arguments.length;
  148. if (args < 3) {
  149. type = null;
  150. }
  151. return new FakeBlob(
  152. this.data.slice(start, args > 1 ? end : this.data.length)
  153. , type
  154. , this.encoding
  155. );
  156. };
  157. FB_proto.toString = function() {
  158. return "[object Blob]";
  159. };
  160. return FakeBlobBuilder;
  161. }(view));
  162. return function Blob(blobParts, options) {
  163. var type = options ? (options.type || "") : "";
  164. var builder = new BlobBuilder();
  165. if (blobParts) {
  166. for (var i = 0, len = blobParts.length; i < len; i++) {
  167. builder.append(blobParts[i]);
  168. }
  169. }
  170. return builder.getBlob(type);
  171. };
  172. }(self));