FileTransfer.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. /* global cordova, FileSystem */
  22. var argscheck = require('cordova/argscheck'),
  23. exec = require('cordova/exec'),
  24. FileTransferError = require('./FileTransferError'),
  25. ProgressEvent = require('cordova-plugin-file.ProgressEvent');
  26. function newProgressEvent(result) {
  27. var pe = new ProgressEvent();
  28. pe.lengthComputable = result.lengthComputable;
  29. pe.loaded = result.loaded;
  30. pe.total = result.total;
  31. return pe;
  32. }
  33. function getUrlCredentials(urlString) {
  34. var credentialsPattern = /^https?\:\/\/(?:(?:(([^:@\/]*)(?::([^@\/]*))?)?@)?([^:\/?#]*)(?::(\d*))?).*$/,
  35. credentials = credentialsPattern.exec(urlString);
  36. return credentials && credentials[1];
  37. }
  38. function getBasicAuthHeader(urlString) {
  39. var header = null;
  40. // This is changed due to MS Windows doesn't support credentials in http uris
  41. // so we detect them by regexp and strip off from result url
  42. // Proof: http://social.msdn.microsoft.com/Forums/windowsapps/en-US/a327cf3c-f033-4a54-8b7f-03c56ba3203f/windows-foundation-uri-security-problem
  43. if (window.btoa) {
  44. var credentials = getUrlCredentials(urlString);
  45. if (credentials) {
  46. var authHeader = "Authorization";
  47. var authHeaderValue = "Basic " + window.btoa(credentials);
  48. header = {
  49. name : authHeader,
  50. value : authHeaderValue
  51. };
  52. }
  53. }
  54. return header;
  55. }
  56. function convertHeadersToArray(headers) {
  57. var result = [];
  58. for (var header in headers) {
  59. if (headers.hasOwnProperty(header)) {
  60. var headerValue = headers[header];
  61. result.push({
  62. name: header,
  63. value: headerValue.toString()
  64. });
  65. }
  66. }
  67. return result;
  68. }
  69. var idCounter = 0;
  70. /**
  71. * FileTransfer uploads a file to a remote server.
  72. * @constructor
  73. */
  74. var FileTransfer = function() {
  75. this._id = ++idCounter;
  76. this.onprogress = null; // optional callback
  77. };
  78. /**
  79. * Given an absolute file path, uploads a file on the device to a remote server
  80. * using a multipart HTTP request.
  81. * @param filePath {String} Full path of the file on the device
  82. * @param server {String} URL of the server to receive the file
  83. * @param successCallback (Function} Callback to be invoked when upload has completed
  84. * @param errorCallback {Function} Callback to be invoked upon error
  85. * @param options {FileUploadOptions} Optional parameters such as file name and mimetype
  86. * @param trustAllHosts {Boolean} Optional trust all hosts (e.g. for self-signed certs), defaults to false
  87. */
  88. FileTransfer.prototype.upload = function(filePath, server, successCallback, errorCallback, options, trustAllHosts) {
  89. argscheck.checkArgs('ssFFO*', 'FileTransfer.upload', arguments);
  90. // check for options
  91. var fileKey = null;
  92. var fileName = null;
  93. var mimeType = null;
  94. var params = null;
  95. var chunkedMode = true;
  96. var headers = null;
  97. var httpMethod = null;
  98. var basicAuthHeader = getBasicAuthHeader(server);
  99. if (basicAuthHeader) {
  100. server = server.replace(getUrlCredentials(server) + '@', '');
  101. options = options || {};
  102. options.headers = options.headers || {};
  103. options.headers[basicAuthHeader.name] = basicAuthHeader.value;
  104. }
  105. if (options) {
  106. fileKey = options.fileKey;
  107. fileName = options.fileName;
  108. mimeType = options.mimeType;
  109. headers = options.headers;
  110. httpMethod = options.httpMethod || "POST";
  111. if (httpMethod.toUpperCase() == "PUT"){
  112. httpMethod = "PUT";
  113. } else {
  114. httpMethod = "POST";
  115. }
  116. if (options.chunkedMode !== null || typeof options.chunkedMode != "undefined") {
  117. chunkedMode = options.chunkedMode;
  118. }
  119. if (options.params) {
  120. params = options.params;
  121. }
  122. else {
  123. params = {};
  124. }
  125. }
  126. if (cordova.platformId === "windowsphone") {
  127. headers = headers && convertHeadersToArray(headers);
  128. params = params && convertHeadersToArray(params);
  129. }
  130. var fail = errorCallback && function(e) {
  131. var error = new FileTransferError(e.code, e.source, e.target, e.http_status, e.body, e.exception);
  132. errorCallback(error);
  133. };
  134. var self = this;
  135. var win = function(result) {
  136. if (typeof result.lengthComputable != "undefined") {
  137. if (self.onprogress) {
  138. self.onprogress(newProgressEvent(result));
  139. }
  140. } else {
  141. if (successCallback) {
  142. successCallback(result);
  143. }
  144. }
  145. };
  146. exec(win, fail, 'FileTransfer', 'upload', [filePath, server, fileKey, fileName, mimeType, params, trustAllHosts, chunkedMode, headers, this._id, httpMethod]);
  147. };
  148. /**
  149. * Downloads a file form a given URL and saves it to the specified directory.
  150. * @param source {String} URL of the server to receive the file
  151. * @param target {String} Full path of the file on the device
  152. * @param successCallback (Function} Callback to be invoked when upload has completed
  153. * @param errorCallback {Function} Callback to be invoked upon error
  154. * @param trustAllHosts {Boolean} Optional trust all hosts (e.g. for self-signed certs), defaults to false
  155. * @param options {FileDownloadOptions} Optional parameters such as headers
  156. */
  157. FileTransfer.prototype.download = function(source, target, successCallback, errorCallback, trustAllHosts, options) {
  158. argscheck.checkArgs('ssFF*', 'FileTransfer.download', arguments);
  159. var self = this;
  160. var basicAuthHeader = getBasicAuthHeader(source);
  161. if (basicAuthHeader) {
  162. source = source.replace(getUrlCredentials(source) + '@', '');
  163. options = options || {};
  164. options.headers = options.headers || {};
  165. options.headers[basicAuthHeader.name] = basicAuthHeader.value;
  166. }
  167. var headers = null;
  168. if (options) {
  169. headers = options.headers || null;
  170. }
  171. if (cordova.platformId === "windowsphone" && headers) {
  172. headers = convertHeadersToArray(headers);
  173. }
  174. var win = function(result) {
  175. if (typeof result.lengthComputable != "undefined") {
  176. if (self.onprogress) {
  177. return self.onprogress(newProgressEvent(result));
  178. }
  179. } else if (successCallback) {
  180. var entry = null;
  181. if (result.isDirectory) {
  182. entry = new (require('cordova-plugin-file.DirectoryEntry'))();
  183. }
  184. else if (result.isFile) {
  185. entry = new (require('cordova-plugin-file.FileEntry'))();
  186. }
  187. entry.isDirectory = result.isDirectory;
  188. entry.isFile = result.isFile;
  189. entry.name = result.name;
  190. entry.fullPath = result.fullPath;
  191. entry.filesystem = new FileSystem(result.filesystemName || (result.filesystem == window.PERSISTENT ? 'persistent' : 'temporary'));
  192. entry.nativeURL = result.nativeURL;
  193. successCallback(entry);
  194. }
  195. };
  196. var fail = errorCallback && function(e) {
  197. var error = new FileTransferError(e.code, e.source, e.target, e.http_status, e.body, e.exception);
  198. errorCallback(error);
  199. };
  200. exec(win, fail, 'FileTransfer', 'download', [source, target, trustAllHosts, this._id, headers]);
  201. };
  202. /**
  203. * Aborts the ongoing file transfer on this object. The original error
  204. * callback for the file transfer will be called if necessary.
  205. */
  206. FileTransfer.prototype.abort = function() {
  207. exec(null, null, 'FileTransfer', 'abort', [this._id]);
  208. };
  209. module.exports = FileTransfer;