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