Blob.js 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. ,
  58. ArrayBuffer = view.ArrayBuffer,
  59. Uint8Array = view.Uint8Array;
  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. if (type === null) {
  72. type = "application/octet-stream";
  73. }
  74. if (blob instanceof FakeBlob) {
  75. data_URI_header = "data:" + type;
  76. if (blob.encoding === "base64") {
  77. return data_URI_header + ";base64," + blob.data;
  78. } else if (blob.encoding === "URI") {
  79. return data_URI_header + "," + decodeURIComponent(blob.data);
  80. }
  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. for (; i < buf_len; i++) {
  105. str += String.fromCharCode(buf[i]);
  106. }
  107. bb.push(str);
  108. } else if (get_class(data) === "Blob" || get_class(data) === "File") {
  109. if (FileReaderSync) {
  110. var fr = new FileReaderSync;
  111. bb.push(fr.readAsBinaryString(data));
  112. } else {
  113. // async FileReader won't work as BlobBuilder is sync
  114. throw new FileException("NOT_READABLE_ERR");
  115. }
  116. } else if (data instanceof FakeBlob) {
  117. if (data.encoding === "base64" && atob) {
  118. bb.push(atob(data.data));
  119. } else if (data.encoding === "URI") {
  120. bb.push(decodeURIComponent(data.data));
  121. } else if (data.encoding === "raw") {
  122. bb.push(data.data);
  123. }
  124. } else {
  125. if (typeof data !== "string") {
  126. data += ""; // convert unsupported types to strings
  127. }
  128. // decode UTF-16 to binary string
  129. bb.push(unescape(encodeURIComponent(data)));
  130. }
  131. };
  132. FBB_proto.getBlob = function (type) {
  133. if (!arguments.length) {
  134. type = null;
  135. }
  136. return new FakeBlob(this.data.join(""), type, "raw");
  137. };
  138. FBB_proto.toString = function () {
  139. return "[object BlobBuilder]";
  140. };
  141. FB_proto.slice = function (start, end, type) {
  142. var args = arguments.length;
  143. if (args < 3) {
  144. type = null;
  145. }
  146. return new FakeBlob(
  147. this.data.slice(start, args > 1 ? end : this.data.length), type, this.encoding
  148. );
  149. };
  150. FB_proto.toString = function () {
  151. return "[object Blob]";
  152. };
  153. FB_proto.close = function () {
  154. this.size = this.data.length = 0;
  155. };
  156. return FakeBlobBuilder;
  157. }(view));
  158. view.Blob = function Blob(blobParts, options) {
  159. var type = options ? (options.type || "") : "";
  160. var builder = new BlobBuilder();
  161. if (blobParts) {
  162. for (var i = 0, len = blobParts.length; i < len; i++) {
  163. builder.append(blobParts[i]);
  164. }
  165. }
  166. return builder.getBlob(type);
  167. };
  168. }(typeof self !== "undefined" && self || typeof window !== "undefined" && window || this.content || this));