You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
4.3 KiB

  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. var exec = require('cordova/exec'),
  22. FileError = require('./FileError'),
  23. ProgressEvent = require('./ProgressEvent');
  24. function write(data) {
  25. var that=this;
  26. var supportsBinary = (typeof window.Blob !== 'undefined' && typeof window.ArrayBuffer !== 'undefined');
  27. var isBinary;
  28. // Check to see if the incoming data is a blob
  29. if (data instanceof File || (supportsBinary && data instanceof Blob)) {
  30. var fileReader = new FileReader();
  31. fileReader.onload = function() {
  32. // Call this method again, with the arraybuffer as argument
  33. FileWriter.prototype.write.call(that, this.result);
  34. };
  35. if (supportsBinary) {
  36. fileReader.readAsArrayBuffer(data);
  37. } else {
  38. fileReader.readAsText(data);
  39. }
  40. return;
  41. }
  42. // Mark data type for safer transport over the binary bridge
  43. isBinary = supportsBinary && (data instanceof ArrayBuffer);
  44. // Throw an exception if we are already writing a file
  45. if (this.readyState === FileWriter.WRITING) {
  46. throw new FileError(FileError.INVALID_STATE_ERR);
  47. }
  48. // WRITING state
  49. this.readyState = FileWriter.WRITING;
  50. var me = this;
  51. // If onwritestart callback
  52. if (typeof me.onwritestart === "function") {
  53. me.onwritestart(new ProgressEvent("writestart", {"target":me}));
  54. }
  55. if (data instanceof ArrayBuffer || data.buffer instanceof ArrayBuffer) {
  56. data = new Uint8Array(data);
  57. var binary = "";
  58. for (var i = 0; i < data.byteLength; i++) {
  59. binary += String.fromCharCode(data[i]);
  60. }
  61. data = binary;
  62. }
  63. var prefix = "file://localhost";
  64. var path = this.localURL;
  65. if (path.substr(0, prefix.length) == prefix) {
  66. path = path.substr(prefix.length);
  67. }
  68. // Write file
  69. exec(
  70. // Success callback
  71. function(r) {
  72. // If DONE (cancelled), then don't do anything
  73. if (me.readyState === FileWriter.DONE) {
  74. return;
  75. }
  76. // position always increases by bytes written because file would be extended
  77. me.position += r;
  78. // The length of the file is now where we are done writing.
  79. me.length = me.position;
  80. // DONE state
  81. me.readyState = FileWriter.DONE;
  82. // If onwrite callback
  83. if (typeof me.onwrite === "function") {
  84. me.onwrite(new ProgressEvent("write", {"target":me}));
  85. }
  86. // If onwriteend callback
  87. if (typeof me.onwriteend === "function") {
  88. me.onwriteend(new ProgressEvent("writeend", {"target":me}));
  89. }
  90. },
  91. // Error callback
  92. function(e) {
  93. // If DONE (cancelled), then don't do anything
  94. if (me.readyState === FileWriter.DONE) {
  95. return;
  96. }
  97. // DONE state
  98. me.readyState = FileWriter.DONE;
  99. // Save error
  100. me.error = new FileError(e);
  101. // If onerror callback
  102. if (typeof me.onerror === "function") {
  103. me.onerror(new ProgressEvent("error", {"target":me}));
  104. }
  105. // If onwriteend callback
  106. if (typeof me.onwriteend === "function") {
  107. me.onwriteend(new ProgressEvent("writeend", {"target":me}));
  108. }
  109. }, "File", "write", [path, data, this.position, isBinary]);
  110. }
  111. module.exports = {
  112. write: write
  113. };
  114. require("cordova/exec/proxy").add("FileWriter", module.exports);