Blob.js 5.8 KB

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