Subversion-Projekte lars-tiefland.ci

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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