Blob.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* Blob.js
  2. * A Blob implementation.
  3. * 2013-12-27
  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 Blob === "object") || typeof URL === "undefined")
  15. if ((typeof Blob === "function" || typeof Blob === "object") && typeof webkitURL !== "undefined") self.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. , ArrayBuffer = view.ArrayBuffer
  50. , Uint8Array = view.Uint8Array
  51. ;
  52. FakeBlob.fake = FB_proto.fake = true;
  53. while (file_ex_code--) {
  54. FileException.prototype[file_ex_codes[file_ex_code]] = file_ex_code + 1;
  55. }
  56. if (!real_URL.createObjectURL) {
  57. URL = view.URL = {};
  58. }
  59. URL.createObjectURL = function(blob) {
  60. var
  61. type = blob.type
  62. , data_URI_header
  63. ;
  64. if (type === null) {
  65. type = "application/octet-stream";
  66. }
  67. if (blob instanceof FakeBlob) {
  68. data_URI_header = "data:" + type;
  69. if (blob.encoding === "base64") {
  70. return data_URI_header + ";base64," + blob.data;
  71. } else if (blob.encoding === "URI") {
  72. return data_URI_header + "," + decodeURIComponent(blob.data);
  73. } if (btoa) {
  74. return data_URI_header + ";base64," + btoa(blob.data);
  75. } else {
  76. return data_URI_header + "," + encodeURIComponent(blob.data);
  77. }
  78. } else if (real_create_object_URL) {
  79. return real_create_object_URL.call(real_URL, blob);
  80. }
  81. };
  82. URL.revokeObjectURL = function(object_URL) {
  83. if (object_URL.substring(0, 5) !== "data:" && real_revoke_object_URL) {
  84. real_revoke_object_URL.call(real_URL, object_URL);
  85. }
  86. };
  87. FBB_proto.append = function(data/*, endings*/) {
  88. var bb = this.data;
  89. // decode data to a binary string
  90. if (Uint8Array && (data instanceof ArrayBuffer || data instanceof Uint8Array)) {
  91. var
  92. str = ""
  93. , buf = new Uint8Array(data)
  94. , i = 0
  95. , buf_len = buf.length
  96. ;
  97. for (; i < buf_len; i++) {
  98. str += String.fromCharCode(buf[i]);
  99. }
  100. bb.push(str);
  101. } else if (get_class(data) === "Blob" || get_class(data) === "File") {
  102. if (FileReaderSync) {
  103. var fr = new FileReaderSync;
  104. bb.push(fr.readAsBinaryString(data));
  105. } else {
  106. // async FileReader won't work as BlobBuilder is sync
  107. throw new FileException("NOT_READABLE_ERR");
  108. }
  109. } else if (data instanceof FakeBlob) {
  110. if (data.encoding === "base64" && atob) {
  111. bb.push(atob(data.data));
  112. } else if (data.encoding === "URI") {
  113. bb.push(decodeURIComponent(data.data));
  114. } else if (data.encoding === "raw") {
  115. bb.push(data.data);
  116. }
  117. } else {
  118. if (typeof data !== "string") {
  119. data += ""; // convert unsupported types to strings
  120. }
  121. // decode UTF-16 to binary string
  122. bb.push(unescape(encodeURIComponent(data)));
  123. }
  124. };
  125. FBB_proto.getBlob = function(type) {
  126. if (!arguments.length) {
  127. type = null;
  128. }
  129. return new FakeBlob(this.data.join(""), type, "raw");
  130. };
  131. FBB_proto.toString = function() {
  132. return "[object BlobBuilder]";
  133. };
  134. FB_proto.slice = function(start, end, type) {
  135. var args = arguments.length;
  136. if (args < 3) {
  137. type = null;
  138. }
  139. return new FakeBlob(
  140. this.data.slice(start, args > 1 ? end : this.data.length)
  141. , type
  142. , this.encoding
  143. );
  144. };
  145. FB_proto.toString = function() {
  146. return "[object Blob]";
  147. };
  148. return FakeBlobBuilder;
  149. }(view));
  150. return function Blob(blobParts, options) {
  151. var type = options ? (options.type || "") : "";
  152. var builder = new BlobBuilder();
  153. if (blobParts) {
  154. for (var i = 0, len = blobParts.length; i < len; i++) {
  155. builder.append(blobParts[i]);
  156. }
  157. }
  158. return builder.getBlob(type);
  159. };
  160. }(typeof self !== "undefined" && self || typeof window !== "undefined" && window || this.content || this));