arnaucube
arnaucube/openEventsPlatformAppPublic Self-hosted repository
openEventsPlatformApp/plugins/cordova-plugin-file/www/ubuntu/FileWriter.js
plugins/cordova-plugin-file/www/ubuntu/FileWriter.jsRaw
  1/*
  2 *
  3 * Licensed to the Apache Software Foundation (ASF) under one
  4 * or more contributor license agreements.  See the NOTICE file
  5 * distributed with this work for additional information
  6 * regarding copyright ownership.  The ASF licenses this file
  7 * to you under the Apache License, Version 2.0 (the
  8 * "License"); you may not use this file except in compliance
  9 * with the License.  You may obtain a copy of the License at
 10 *
 11 *   http://www.apache.org/licenses/LICENSE-2.0
 12 *
 13 * Unless required by applicable law or agreed to in writing,
 14 * software distributed under the License is distributed on an
 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 16 * KIND, either express or implied.  See the License for the
 17 * specific language governing permissions and limitations
 18 * under the License.
 19 *
 20*/
 21
 22var exec = require('cordova/exec'),
 23    FileError = require('./FileError'),
 24    ProgressEvent = require('./ProgressEvent');
 25
 26function write(data) {
 27    var that=this;
 28    var supportsBinary = (typeof window.Blob !== 'undefined' && typeof window.ArrayBuffer !== 'undefined');
 29    var isBinary;
 30
 31    // Check to see if the incoming data is a blob
 32    if (data instanceof File || (supportsBinary && data instanceof Blob)) {
 33        var fileReader = new FileReader();
 34        fileReader.onload = function() {
 35            // Call this method again, with the arraybuffer as argument
 36            FileWriter.prototype.write.call(that, this.result);
 37        };
 38        if (supportsBinary) {
 39            fileReader.readAsArrayBuffer(data);
 40        } else {
 41            fileReader.readAsText(data);
 42        }
 43        return;
 44    }
 45
 46    // Mark data type for safer transport over the binary bridge
 47    isBinary = supportsBinary && (data instanceof ArrayBuffer);
 48
 49    // Throw an exception if we are already writing a file
 50    if (this.readyState === FileWriter.WRITING) {
 51        throw new FileError(FileError.INVALID_STATE_ERR);
 52    }
 53
 54    // WRITING state
 55    this.readyState = FileWriter.WRITING;
 56
 57    var me = this;
 58
 59    // If onwritestart callback
 60    if (typeof me.onwritestart === "function") {
 61        me.onwritestart(new ProgressEvent("writestart", {"target":me}));
 62    }
 63
 64    if (data instanceof ArrayBuffer || data.buffer instanceof ArrayBuffer) {
 65        data = new Uint8Array(data);
 66    var binary = "";
 67    for (var i = 0; i < data.byteLength; i++) {
 68            binary += String.fromCharCode(data[i]);
 69    }
 70        data = binary;
 71    }
 72
 73    var prefix = "file://localhost";
 74    var path = this.localURL;
 75    if (path.substr(0, prefix.length) == prefix) {
 76        path = path.substr(prefix.length);
 77    }
 78    // Write file
 79    exec(
 80        // Success callback
 81        function(r) {
 82            // If DONE (cancelled), then don't do anything
 83            if (me.readyState === FileWriter.DONE) {
 84                return;
 85            }
 86
 87            // position always increases by bytes written because file would be extended
 88            me.position += r;
 89            // The length of the file is now where we are done writing.
 90
 91            me.length = me.position;
 92
 93            // DONE state
 94            me.readyState = FileWriter.DONE;
 95
 96            // If onwrite callback
 97            if (typeof me.onwrite === "function") {
 98                me.onwrite(new ProgressEvent("write", {"target":me}));
 99            }
100
101            // If onwriteend callback
102            if (typeof me.onwriteend === "function") {
103                me.onwriteend(new ProgressEvent("writeend", {"target":me}));
104            }
105        },
106        // Error callback
107        function(e) {
108            // If DONE (cancelled), then don't do anything
109            if (me.readyState === FileWriter.DONE) {
110                return;
111            }
112
113            // DONE state
114            me.readyState = FileWriter.DONE;
115
116            // Save error
117            me.error = new FileError(e);
118
119            // If onerror callback
120            if (typeof me.onerror === "function") {
121                me.onerror(new ProgressEvent("error", {"target":me}));
122            }
123
124            // If onwriteend callback
125            if (typeof me.onwriteend === "function") {
126                me.onwriteend(new ProgressEvent("writeend", {"target":me}));
127            }
128        }, "File", "write", [path, data, this.position, isBinary]);
129}
130
131module.exports = {
132    write: write
133};
134
135require("cordova/exec/proxy").add("FileWriter", module.exports);