| 1 |
// Copyright 2011 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview A wrapper for the HTML5 FileReader object. |
| 17 |
* |
| 18 |
*/ |
| 19 |
|
| 20 |
goog.provide('goog.fs.FileReader'); |
| 21 |
goog.provide('goog.fs.FileReader.EventType'); |
| 22 |
goog.provide('goog.fs.FileReader.ReadyState'); |
| 23 |
|
| 24 |
goog.require('goog.async.Deferred'); |
| 25 |
goog.require('goog.events.EventTarget'); |
| 26 |
goog.require('goog.fs.Error'); |
| 27 |
goog.require('goog.fs.ProgressEvent'); |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
/** |
| 32 |
* An object for monitoring the reading of files. This emits ProgressEvents of |
| 33 |
* the types listed in {@link goog.fs.FileReader.EventType}. |
| 34 |
* |
| 35 |
* @constructor |
| 36 |
* @extends {goog.events.EventTarget} |
| 37 |
* @final |
| 38 |
*/ |
| 39 |
goog.fs.FileReader = function() { |
| 40 |
goog.fs.FileReader.base(this, 'constructor'); |
| 41 |
|
| 42 |
/** |
| 43 |
* The underlying FileReader object. |
| 44 |
* |
| 45 |
* @type {!FileReader} |
| 46 |
* @private |
| 47 |
*/ |
| 48 |
this.reader_ = new FileReader(); |
| 49 |
|
| 50 |
this.reader_.onloadstart = goog.bind(this.dispatchProgressEvent_, this); |
| 51 |
this.reader_.onprogress = goog.bind(this.dispatchProgressEvent_, this); |
| 52 |
this.reader_.onload = goog.bind(this.dispatchProgressEvent_, this); |
| 53 |
this.reader_.onabort = goog.bind(this.dispatchProgressEvent_, this); |
| 54 |
this.reader_.onerror = goog.bind(this.dispatchProgressEvent_, this); |
| 55 |
this.reader_.onloadend = goog.bind(this.dispatchProgressEvent_, this); |
| 56 |
}; |
| 57 |
goog.inherits(goog.fs.FileReader, goog.events.EventTarget); |
| 58 |
|
| 59 |
|
| 60 |
/** |
| 61 |
* Possible states for a FileReader. |
| 62 |
* |
| 63 |
* @enum {number} |
| 64 |
*/ |
| 65 |
goog.fs.FileReader.ReadyState = { |
| 66 |
/** |
| 67 |
* The object has been constructed, but there is no pending read. |
| 68 |
*/ |
| 69 |
INIT: 0, |
| 70 |
/** |
| 71 |
* Data is being read. |
| 72 |
*/ |
| 73 |
LOADING: 1, |
| 74 |
/** |
| 75 |
* The data has been read from the file, the read was aborted, or an error |
| 76 |
* occurred. |
| 77 |
*/ |
| 78 |
DONE: 2 |
| 79 |
}; |
| 80 |
|
| 81 |
|
| 82 |
/** |
| 83 |
* Events emitted by a FileReader. |
| 84 |
* |
| 85 |
* @enum {string} |
| 86 |
*/ |
| 87 |
goog.fs.FileReader.EventType = { |
| 88 |
/** |
| 89 |
* Emitted when the reading begins. readyState will be LOADING. |
| 90 |
*/ |
| 91 |
LOAD_START: 'loadstart', |
| 92 |
/** |
| 93 |
* Emitted when progress has been made in reading the file. readyState will be |
| 94 |
* LOADING. |
| 95 |
*/ |
| 96 |
PROGRESS: 'progress', |
| 97 |
/** |
| 98 |
* Emitted when the data has been successfully read. readyState will be |
| 99 |
* LOADING. |
| 100 |
*/ |
| 101 |
LOAD: 'load', |
| 102 |
/** |
| 103 |
* Emitted when the reading has been aborted. readyState will be LOADING. |
| 104 |
*/ |
| 105 |
ABORT: 'abort', |
| 106 |
/** |
| 107 |
* Emitted when an error is encountered or the reading has been aborted. |
| 108 |
* readyState will be LOADING. |
| 109 |
*/ |
| 110 |
ERROR: 'error', |
| 111 |
/** |
| 112 |
* Emitted when the reading is finished, whether successfully or not. |
| 113 |
* readyState will be DONE. |
| 114 |
*/ |
| 115 |
LOAD_END: 'loadend' |
| 116 |
}; |
| 117 |
|
| 118 |
|
| 119 |
/** |
| 120 |
* Abort the reading of the file. |
| 121 |
*/ |
| 122 |
goog.fs.FileReader.prototype.abort = function() { |
| 123 |
try { |
| 124 |
this.reader_.abort(); |
| 125 |
} catch (e) { |
| 126 |
throw new goog.fs.Error(e, 'aborting read'); |
| 127 |
} |
| 128 |
}; |
| 129 |
|
| 130 |
|
| 131 |
/** |
| 132 |
* @return {goog.fs.FileReader.ReadyState} The current state of the FileReader. |
| 133 |
*/ |
| 134 |
goog.fs.FileReader.prototype.getReadyState = function() { |
| 135 |
return /** @type {goog.fs.FileReader.ReadyState} */ (this.reader_.readyState); |
| 136 |
}; |
| 137 |
|
| 138 |
|
| 139 |
/** |
| 140 |
* @return {*} The result of the file read. |
| 141 |
*/ |
| 142 |
goog.fs.FileReader.prototype.getResult = function() { |
| 143 |
return this.reader_.result; |
| 144 |
}; |
| 145 |
|
| 146 |
|
| 147 |
/** |
| 148 |
* @return {goog.fs.Error} The error encountered while reading, if any. |
| 149 |
*/ |
| 150 |
goog.fs.FileReader.prototype.getError = function() { |
| 151 |
return this.reader_.error && |
| 152 |
new goog.fs.Error(this.reader_.error, 'reading file'); |
| 153 |
}; |
| 154 |
|
| 155 |
|
| 156 |
/** |
| 157 |
* Wrap a progress event emitted by the underlying file reader and re-emit it. |
| 158 |
* |
| 159 |
* @param {!ProgressEvent} event The underlying event. |
| 160 |
* @private |
| 161 |
*/ |
| 162 |
goog.fs.FileReader.prototype.dispatchProgressEvent_ = function(event) { |
| 163 |
this.dispatchEvent(new goog.fs.ProgressEvent(event, this)); |
| 164 |
}; |
| 165 |
|
| 166 |
|
| 167 |
/** @override */ |
| 168 |
goog.fs.FileReader.prototype.disposeInternal = function() { |
| 169 |
goog.fs.FileReader.base(this, 'disposeInternal'); |
| 170 |
delete this.reader_; |
| 171 |
}; |
| 172 |
|
| 173 |
|
| 174 |
/** |
| 175 |
* Starts reading a blob as a binary string. |
| 176 |
* @param {!Blob} blob The blob to read. |
| 177 |
*/ |
| 178 |
goog.fs.FileReader.prototype.readAsBinaryString = function(blob) { |
| 179 |
this.reader_.readAsBinaryString(blob); |
| 180 |
}; |
| 181 |
|
| 182 |
|
| 183 |
/** |
| 184 |
* Reads a blob as a binary string. |
| 185 |
* @param {!Blob} blob The blob to read. |
| 186 |
* @return {!goog.async.Deferred} The deferred Blob contents as a binary string. |
| 187 |
* If an error occurs, the errback is called with a {@link goog.fs.Error}. |
| 188 |
*/ |
| 189 |
goog.fs.FileReader.readAsBinaryString = function(blob) { |
| 190 |
var reader = new goog.fs.FileReader(); |
| 191 |
var d = goog.fs.FileReader.createDeferred_(reader); |
| 192 |
reader.readAsBinaryString(blob); |
| 193 |
return d; |
| 194 |
}; |
| 195 |
|
| 196 |
|
| 197 |
/** |
| 198 |
* Starts reading a blob as an array buffer. |
| 199 |
* @param {!Blob} blob The blob to read. |
| 200 |
*/ |
| 201 |
goog.fs.FileReader.prototype.readAsArrayBuffer = function(blob) { |
| 202 |
this.reader_.readAsArrayBuffer(blob); |
| 203 |
}; |
| 204 |
|
| 205 |
|
| 206 |
/** |
| 207 |
* Reads a blob as an array buffer. |
| 208 |
* @param {!Blob} blob The blob to read. |
| 209 |
* @return {!goog.async.Deferred} The deferred Blob contents as an array buffer. |
| 210 |
* If an error occurs, the errback is called with a {@link goog.fs.Error}. |
| 211 |
*/ |
| 212 |
goog.fs.FileReader.readAsArrayBuffer = function(blob) { |
| 213 |
var reader = new goog.fs.FileReader(); |
| 214 |
var d = goog.fs.FileReader.createDeferred_(reader); |
| 215 |
reader.readAsArrayBuffer(blob); |
| 216 |
return d; |
| 217 |
}; |
| 218 |
|
| 219 |
|
| 220 |
/** |
| 221 |
* Starts reading a blob as text. |
| 222 |
* @param {!Blob} blob The blob to read. |
| 223 |
* @param {string=} opt_encoding The name of the encoding to use. |
| 224 |
*/ |
| 225 |
goog.fs.FileReader.prototype.readAsText = function(blob, opt_encoding) { |
| 226 |
this.reader_.readAsText(blob, opt_encoding); |
| 227 |
}; |
| 228 |
|
| 229 |
|
| 230 |
/** |
| 231 |
* Reads a blob as text. |
| 232 |
* @param {!Blob} blob The blob to read. |
| 233 |
* @param {string=} opt_encoding The name of the encoding to use. |
| 234 |
* @return {!goog.async.Deferred} The deferred Blob contents as text. |
| 235 |
* If an error occurs, the errback is called with a {@link goog.fs.Error}. |
| 236 |
*/ |
| 237 |
goog.fs.FileReader.readAsText = function(blob, opt_encoding) { |
| 238 |
var reader = new goog.fs.FileReader(); |
| 239 |
var d = goog.fs.FileReader.createDeferred_(reader); |
| 240 |
reader.readAsText(blob, opt_encoding); |
| 241 |
return d; |
| 242 |
}; |
| 243 |
|
| 244 |
|
| 245 |
/** |
| 246 |
* Starts reading a blob as a data URL. |
| 247 |
* @param {!Blob} blob The blob to read. |
| 248 |
*/ |
| 249 |
goog.fs.FileReader.prototype.readAsDataUrl = function(blob) { |
| 250 |
this.reader_.readAsDataURL(blob); |
| 251 |
}; |
| 252 |
|
| 253 |
|
| 254 |
/** |
| 255 |
* Reads a blob as a data URL. |
| 256 |
* @param {!Blob} blob The blob to read. |
| 257 |
* @return {!goog.async.Deferred} The deferred Blob contents as a data URL. |
| 258 |
* If an error occurs, the errback is called with a {@link goog.fs.Error}. |
| 259 |
*/ |
| 260 |
goog.fs.FileReader.readAsDataUrl = function(blob) { |
| 261 |
var reader = new goog.fs.FileReader(); |
| 262 |
var d = goog.fs.FileReader.createDeferred_(reader); |
| 263 |
reader.readAsDataUrl(blob); |
| 264 |
return d; |
| 265 |
}; |
| 266 |
|
| 267 |
|
| 268 |
/** |
| 269 |
* Creates a new deferred object for the results of a read method. |
| 270 |
* @param {goog.fs.FileReader} reader The reader to create a deferred for. |
| 271 |
* @return {!goog.async.Deferred} The deferred results. |
| 272 |
* @private |
| 273 |
*/ |
| 274 |
goog.fs.FileReader.createDeferred_ = function(reader) { |
| 275 |
var deferred = new goog.async.Deferred(); |
| 276 |
reader.listen( |
| 277 |
goog.fs.FileReader.EventType.LOAD_END, goog.partial(function(d, r, e) { |
| 278 |
var result = r.getResult(); |
| 279 |
var error = r.getError(); |
| 280 |
if (result != null && !error) { |
| 281 |
d.callback(result); |
| 282 |
} else { |
| 283 |
d.errback(error); |
| 284 |
} |
| 285 |
r.dispose(); |
| 286 |
}, deferred, reader)); |
| 287 |
return deferred; |
| 288 |
}; |