blob: ef70c4758cc4fbcd915aa93ad6c50531b6f0c759 [file] [log] [blame]
/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
var exec = require('cordova/exec');
var FileError = require('./FileError');
var ProgressEvent = require('./ProgressEvent');
function write (data) {
var that = this;
var supportsBinary = (typeof window.Blob !== 'undefined' && typeof window.ArrayBuffer !== 'undefined');
var isBinary;
// Check to see if the incoming data is a blob
/* eslint-disable no-undef */
if (data instanceof File || (supportsBinary && data instanceof Blob)) {
var fileReader = new FileReader();
fileReader.onload = function () {
// Call this method again, with the arraybuffer as argument
FileWriter.prototype.write.call(that, this.result);
};
if (supportsBinary) {
fileReader.readAsArrayBuffer(data);
} else {
fileReader.readAsText(data);
}
return;
}
// Mark data type for safer transport over the binary bridge
isBinary = supportsBinary && (data instanceof ArrayBuffer);
// Throw an exception if we are already writing a file
if (this.readyState === FileWriter.WRITING) {
throw new FileError(FileError.INVALID_STATE_ERR);
}
// WRITING state
this.readyState = FileWriter.WRITING;
/* eslint-enable no-undef */
var me = this;
// If onwritestart callback
if (typeof me.onwritestart === 'function') {
me.onwritestart(new ProgressEvent('writestart', {'target': me}));
}
if (data instanceof ArrayBuffer || data.buffer instanceof ArrayBuffer) {
data = new Uint8Array(data);
var binary = '';
for (var i = 0; i < data.byteLength; i++) {
binary += String.fromCharCode(data[i]);
}
data = binary;
}
var prefix = 'file://localhost';
var path = this.localURL;
if (path.substr(0, prefix.length) === prefix) {
path = path.substr(prefix.length);
}
// Write file
exec(
// Success callback
function (r) {
// If DONE (cancelled), then don't do anything
if (me.readyState === FileWriter.DONE) { // eslint-disable-line no-undef
return;
}
// position always increases by bytes written because file would be extended
me.position += r;
// The length of the file is now where we are done writing.
me.length = me.position;
// DONE state
me.readyState = FileWriter.DONE; // eslint-disable-line no-undef
// If onwrite callback
if (typeof me.onwrite === 'function') {
me.onwrite(new ProgressEvent('write', {'target': me}));
}
// If onwriteend callback
if (typeof me.onwriteend === 'function') {
me.onwriteend(new ProgressEvent('writeend', {'target': me}));
}
},
// Error callback
function (e) {
// If DONE (cancelled), then don't do anything
if (me.readyState === FileWriter.DONE) { // eslint-disable-line no-undef
return;
}
// DONE state
me.readyState = FileWriter.DONE; // eslint-disable-line no-undef
// Save error
me.error = new FileError(e);
// If onerror callback
if (typeof me.onerror === 'function') {
me.onerror(new ProgressEvent('error', {'target': me}));
}
// If onwriteend callback
if (typeof me.onwriteend === 'function') {
me.onwriteend(new ProgressEvent('writeend', {'target': me}));
}
}, 'File', 'write', [path, data, this.position, isBinary]);
}
module.exports = {
write: write
};
require('cordova/exec/proxy').add('FileWriter', module.exports);