Blob.js 6.5 KB

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