FileTransferProxy.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 FileTransferError = require('./FileTransferError'),
  22. xhr = {};
  23. function getParentPath(filePath) {
  24. var pos = filePath.lastIndexOf('/');
  25. return filePath.substring(0, pos + 1);
  26. }
  27. function getFileName(filePath) {
  28. var pos = filePath.lastIndexOf('/');
  29. return filePath.substring(pos + 1);
  30. }
  31. module.exports = {
  32. abort: function (successCallback, errorCallback, args) {
  33. var id = args[0];
  34. if (xhr[id]) {
  35. xhr[id].abort();
  36. if (typeof(successCallback) === 'function') {
  37. successCallback();
  38. }
  39. } else if (typeof(errorCallback) === 'function') {
  40. errorCallback();
  41. }
  42. },
  43. upload: function(successCallback, errorCallback, args) {
  44. var filePath = args[0],
  45. server = args[1],
  46. fileKey = args[2],
  47. fileName = args[3],
  48. mimeType = args[4],
  49. params = args[5],
  50. /*trustAllHosts = args[6],*/
  51. /*chunkedMode = args[7],*/
  52. headers = args[8];
  53. xhr[fileKey] = new XMLHttpRequest({mozSystem: true});
  54. xhr[fileKey].onabort = function() {
  55. onFail(new FileTransferError(FileTransferError.ABORT_ERR, server, filePath, this.status, xhr[fileKey].response));
  56. };
  57. window.resolveLocalFileSystemURL(filePath, function(entry) {
  58. entry.file(function(file) {
  59. var reader = new FileReader();
  60. reader.onloadend = function() {
  61. var blob = new Blob([this.result], {type: mimeType});
  62. var fd = new FormData();
  63. fd.append(fileKey, blob, fileName);
  64. for (var prop in params) {
  65. if (params.hasOwnProperty(prop)) {
  66. fd.append(prop, params[prop]);
  67. }
  68. }
  69. xhr[fileKey].open("POST", server);
  70. xhr[fileKey].onload = function(evt) {
  71. if (xhr[fileKey].status === 200) {
  72. var result = new FileUploadResult();
  73. result.bytesSent = blob.size;
  74. result.responseCode = xhr[fileKey].status;
  75. result.response = xhr[fileKey].response;
  76. delete xhr[fileKey];
  77. onSuccess(result);
  78. } else if (xhr[fileKey].status === 404) {
  79. onFail(new FileTransferError(FileTransferError.INVALID_URL_ERR, server, filePath, xhr[fileKey].status, xhr[fileKey].response));
  80. } else {
  81. onFail(new FileTransferError(FileTransferError.CONNECTION_ERR, server, filePath, xhr[fileKey].status, xhr[fileKey].response));
  82. }
  83. };
  84. xhr[fileKey].ontimeout = function() {
  85. onFail(new FileTransferError(FileTransferError.CONNECTION_ERR, server, filePath, xhr[fileKey].status, xhr[fileKey].response));
  86. };
  87. xhr[fileKey].onerror = function() {
  88. onFail(new FileTransferError(FileTransferError.CONNECTION_ERR, server, filePath, this.status, xhr[fileKey].response));
  89. };
  90. for (var header in headers) {
  91. if (headers.hasOwnProperty(header)) {
  92. xhr[fileKey].setRequestHeader(header, headers[header]);
  93. }
  94. }
  95. xhr[fileKey].send(fd);
  96. };
  97. reader.readAsArrayBuffer(file);
  98. }, function() {
  99. onFail(new FileTransferError(FileTransferError.FILE_NOT_FOUND_ERR, server, filePath));
  100. });
  101. }, function() {
  102. onFail(new FileTransferError(FileTransferError.FILE_NOT_FOUND_ERR, server, filePath));
  103. });
  104. function onSuccess(data) {
  105. if (typeof(successCallback) === 'function') {
  106. successCallback(data);
  107. }
  108. }
  109. function onFail(code) {
  110. delete xhr[fileKey];
  111. if (typeof(errorCallback) === 'function') {
  112. errorCallback(code);
  113. }
  114. }
  115. },
  116. download: function (successCallback, errorCallback, args) {
  117. var source = args[0],
  118. target = args[1],
  119. id = args[3],
  120. headers = args[4];
  121. xhr[id] = new XMLHttpRequest({mozSystem: true});
  122. xhr[id].onload = function () {
  123. if (xhr[id].readyState === xhr[id].DONE) {
  124. if (xhr[id].status === 200 && xhr[id].response) {
  125. window.resolveLocalFileSystemURL(getParentPath(target), function (dir) {
  126. dir.getFile(getFileName(target), {create: true}, writeFile, function (error) {
  127. onFail(new FileTransferError(FileTransferError.FILE_NOT_FOUND_ERR, source, target, xhr[id].status, xhr[id].response));
  128. });
  129. }, function () {
  130. onFail(new FileTransferError(FileTransferError.FILE_NOT_FOUND_ERR, source, target, xhr[id].status, xhr[id].response));
  131. });
  132. } else if (xhr[id].status === 404) {
  133. onFail(new FileTransferError(FileTransferError.INVALID_URL_ERR, source, target, xhr[id].status, xhr[id].response));
  134. } else {
  135. onFail(new FileTransferError(FileTransferError.CONNECTION_ERR, source, target, xhr[id].status, xhr[id].response));
  136. }
  137. }
  138. };
  139. function writeFile(entry) {
  140. entry.createWriter(function (fileWriter) {
  141. fileWriter.onwriteend = function (evt) {
  142. if (!evt.target.error) {
  143. entry.filesystemName = entry.filesystem.name;
  144. delete xhr[id];
  145. onSuccess(entry);
  146. } else {
  147. onFail(evt.target.error);
  148. }
  149. };
  150. fileWriter.onerror = function (evt) {
  151. onFail(evt.target.error);
  152. };
  153. fileWriter.write(new Blob([xhr[id].response]));
  154. }, function (error) {
  155. onFail(error);
  156. });
  157. }
  158. xhr[id].onerror = function (e) {
  159. onFail(new FileTransferError(FileTransferError.CONNECTION_ERR, source, target, xhr[id].status, xhr[id].response));
  160. };
  161. xhr[id].onabort = function (e) {
  162. onFail(new FileTransferError(FileTransferError.ABORT_ERR, source, target, xhr[id].status, xhr[id].response));
  163. };
  164. xhr[id].open("GET", source, true);
  165. for (var header in headers) {
  166. if (headers.hasOwnProperty(header)) {
  167. xhr[id].setRequestHeader(header, headers[header]);
  168. }
  169. }
  170. xhr[id].responseType = "blob";
  171. setTimeout(function () {
  172. if (xhr[id]) {
  173. xhr[id].send();
  174. }
  175. }, 0);
  176. function onSuccess(entry) {
  177. if (typeof(successCallback) === 'function') {
  178. successCallback(entry);
  179. }
  180. }
  181. function onFail(error) {
  182. delete xhr[id];
  183. if (typeof(errorCallback) === 'function') {
  184. errorCallback(error);
  185. }
  186. }
  187. }
  188. };
  189. require('cordova/exec/proxy').add('FileTransfer', module.exports);