123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
-
-
- var exec = require('cordova/exec');
- var modulemapper = require('cordova/modulemapper');
- var utils = require('cordova/utils');
- var FileError = require('./FileError');
- var ProgressEvent = require('./ProgressEvent');
- var origFileReader = modulemapper.getOriginalSymbol(window, 'FileReader');
-
-
- var FileReader = function () {
- this._readyState = 0;
- this._error = null;
- this._result = null;
- this._progress = null;
- this._localURL = '';
- this._realReader = origFileReader ? new origFileReader() : {};
- };
-
-
- FileReader.READ_CHUNK_SIZE = 256 * 1024;
-
-
- FileReader.EMPTY = 0;
- FileReader.LOADING = 1;
- FileReader.DONE = 2;
-
- utils.defineGetter(FileReader.prototype, 'readyState', function () {
- return this._localURL ? this._readyState : this._realReader.readyState;
- });
-
- utils.defineGetter(FileReader.prototype, 'error', function () {
- return this._localURL ? this._error : this._realReader.error;
- });
-
- utils.defineGetter(FileReader.prototype, 'result', function () {
- return this._localURL ? this._result : this._realReader.result;
- });
-
- function defineEvent (eventName) {
- utils.defineGetterSetter(FileReader.prototype, eventName, function () {
- return this._realReader[eventName] || null;
- }, function (value) {
- this._realReader[eventName] = value;
- });
- }
- defineEvent('onloadstart');
- defineEvent('onprogress');
- defineEvent('onload');
- defineEvent('onerror');
- defineEvent('onloadend');
- defineEvent('onabort');
-
- function initRead (reader, file) {
-
- if (reader.readyState === FileReader.LOADING) {
- throw new FileError(FileError.INVALID_STATE_ERR);
- }
-
- reader._result = null;
- reader._error = null;
- reader._progress = 0;
- reader._readyState = FileReader.LOADING;
-
- if (typeof file.localURL === 'string') {
- reader._localURL = file.localURL;
- } else {
- reader._localURL = '';
- return true;
- }
-
- if (reader.onloadstart) {
- reader.onloadstart(new ProgressEvent('loadstart', {target: reader}));
- }
- }
-
-
- function readSuccessCallback (readType, encoding, offset, totalSize, accumulate, r) {
- if (this._readyState === FileReader.DONE) {
- return;
- }
-
- var CHUNK_SIZE = FileReader.READ_CHUNK_SIZE;
- if (readType === 'readAsDataURL') {
-
-
- CHUNK_SIZE = cordova.platformId === 'windows' ? totalSize :
-
-
- FileReader.READ_CHUNK_SIZE - (FileReader.READ_CHUNK_SIZE % 3) + 3;
- }
-
- if (typeof r !== 'undefined') {
- accumulate(r);
- this._progress = Math.min(this._progress + CHUNK_SIZE, totalSize);
-
- if (typeof this.onprogress === 'function') {
- this.onprogress(new ProgressEvent('progress', {loaded: this._progress, total: totalSize}));
- }
- }
-
- if (typeof r === 'undefined' || this._progress < totalSize) {
- var execArgs = [
- this._localURL,
- offset + this._progress,
- offset + this._progress + Math.min(totalSize - this._progress, CHUNK_SIZE)];
- if (encoding) {
- execArgs.splice(1, 0, encoding);
- }
- exec(
- readSuccessCallback.bind(this, readType, encoding, offset, totalSize, accumulate),
- readFailureCallback.bind(this),
- 'File', readType, execArgs);
- } else {
- this._readyState = FileReader.DONE;
-
- if (typeof this.onload === 'function') {
- this.onload(new ProgressEvent('load', {target: this}));
- }
-
- if (typeof this.onloadend === 'function') {
- this.onloadend(new ProgressEvent('loadend', {target: this}));
- }
- }
- }
-
-
- function readFailureCallback (e) {
- if (this._readyState === FileReader.DONE) {
- return;
- }
-
- this._readyState = FileReader.DONE;
- this._result = null;
- this._error = new FileError(e);
-
- if (typeof this.onerror === 'function') {
- this.onerror(new ProgressEvent('error', {target: this}));
- }
-
- if (typeof this.onloadend === 'function') {
- this.onloadend(new ProgressEvent('loadend', {target: this}));
- }
- }
-
-
- FileReader.prototype.abort = function () {
- if (origFileReader && !this._localURL) {
- return this._realReader.abort();
- }
- this._result = null;
-
- if (this._readyState === FileReader.DONE || this._readyState === FileReader.EMPTY) {
- return;
- }
-
- this._readyState = FileReader.DONE;
-
-
- if (typeof this.onabort === 'function') {
- this.onabort(new ProgressEvent('abort', {target: this}));
- }
-
- if (typeof this.onloadend === 'function') {
- this.onloadend(new ProgressEvent('loadend', {target: this}));
- }
- };
-
-
- FileReader.prototype.readAsText = function (file, encoding) {
- if (initRead(this, file)) {
- return this._realReader.readAsText(file, encoding);
- }
-
-
- var enc = encoding || 'UTF-8';
-
- var totalSize = file.end - file.start;
- readSuccessCallback.bind(this)('readAsText', enc, file.start, totalSize, function (r) {
- if (this._progress === 0) {
- this._result = '';
- }
- this._result += r;
- }.bind(this));
- };
-
-
- FileReader.prototype.readAsDataURL = function (file) {
- if (initRead(this, file)) {
- return this._realReader.readAsDataURL(file);
- }
-
- var totalSize = file.end - file.start;
- readSuccessCallback.bind(this)('readAsDataURL', null, file.start, totalSize, function (r) {
- var commaIndex = r.indexOf(',');
- if (this._progress === 0) {
- this._result = r;
- } else {
- this._result += r.substring(commaIndex + 1);
- }
- }.bind(this));
- };
-
-
- FileReader.prototype.readAsBinaryString = function (file) {
- if (initRead(this, file)) {
- return this._realReader.readAsBinaryString(file);
- }
-
- var totalSize = file.end - file.start;
- readSuccessCallback.bind(this)('readAsBinaryString', null, file.start, totalSize, function (r) {
- if (this._progress === 0) {
- this._result = '';
- }
- this._result += r;
- }.bind(this));
- };
-
-
- FileReader.prototype.readAsArrayBuffer = function (file) {
- if (initRead(this, file)) {
- return this._realReader.readAsArrayBuffer(file);
- }
-
- var totalSize = file.end - file.start;
- readSuccessCallback.bind(this)('readAsArrayBuffer', null, file.start, totalSize, function (r) {
- var resultArray = (this._progress === 0 ? new Uint8Array(totalSize) : new Uint8Array(this._result));
- resultArray.set(new Uint8Array(r), this._progress);
- this._result = resultArray.buffer;
- }.bind(this));
- };
-
- module.exports = FileReader;
|