FileTransfer.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 argscheck = require('cordova/argscheck'),
  22. FileTransferError = require('./FileTransferError'),
  23. xhrImpl = require('./BB10XHRImplementation');
  24. function getBasicAuthHeader(urlString) {
  25. var header = null;
  26. if (window.btoa) {
  27. // parse the url using the Location object
  28. var url = document.createElement('a');
  29. url.href = urlString;
  30. var credentials = null;
  31. var protocol = url.protocol + "//";
  32. var origin = protocol + url.host;
  33. // check whether there are the username:password credentials in the url
  34. if (url.href.indexOf(origin) !== 0) { // credentials found
  35. var atIndex = url.href.indexOf("@");
  36. credentials = url.href.substring(protocol.length, atIndex);
  37. }
  38. if (credentials) {
  39. var authHeader = "Authorization";
  40. var authHeaderValue = "Basic " + window.btoa(credentials);
  41. header = {
  42. name : authHeader,
  43. value : authHeaderValue
  44. };
  45. }
  46. }
  47. return header;
  48. }
  49. var idCounter = 0;
  50. /**
  51. * FileTransfer uploads a file to a remote server.
  52. * @constructor
  53. */
  54. var FileTransfer = function() {
  55. this._id = ++idCounter;
  56. this.onprogress = null; // optional callback
  57. };
  58. /**
  59. * Given an absolute file path, uploads a file on the device to a remote server
  60. * using a multipart HTTP request.
  61. * @param filePath {String} Full path of the file on the device
  62. * @param server {String} URL of the server to receive the file
  63. * @param successCallback (Function} Callback to be invoked when upload has completed
  64. * @param errorCallback {Function} Callback to be invoked upon error
  65. * @param options {FileUploadOptions} Optional parameters such as file name and mimetype
  66. * @param trustAllHosts {Boolean} Optional trust all hosts (e.g. for self-signed certs), defaults to false
  67. */
  68. FileTransfer.prototype.upload = function(filePath, server, successCallback, errorCallback, options, trustAllHosts) {
  69. argscheck.checkArgs('ssFFO*', 'FileTransfer.upload', arguments);
  70. // check for options
  71. var fileKey = null;
  72. var fileName = null;
  73. var mimeType = null;
  74. var params = null;
  75. var chunkedMode = true;
  76. var headers = null;
  77. var httpMethod = null;
  78. var basicAuthHeader = getBasicAuthHeader(server);
  79. if (basicAuthHeader) {
  80. options = options || {};
  81. options.headers = options.headers || {};
  82. options.headers[basicAuthHeader.name] = basicAuthHeader.value;
  83. }
  84. if (options) {
  85. fileKey = options.fileKey;
  86. fileName = options.fileName;
  87. mimeType = options.mimeType;
  88. headers = options.headers;
  89. httpMethod = options.httpMethod || "POST";
  90. if (httpMethod.toUpperCase() == "PUT"){
  91. httpMethod = "PUT";
  92. } else {
  93. httpMethod = "POST";
  94. }
  95. if (options.chunkedMode !== null || typeof options.chunkedMode != "undefined") {
  96. chunkedMode = options.chunkedMode;
  97. }
  98. if (options.params) {
  99. params = options.params;
  100. }
  101. else {
  102. params = {};
  103. }
  104. }
  105. var fail = errorCallback && function(e) {
  106. var error = new FileTransferError(e.code, e.source, e.target, e.http_status, e.body);
  107. errorCallback(error);
  108. };
  109. var self = this;
  110. var win = function(result) {
  111. if (typeof result.lengthComputable != "undefined") {
  112. if (self.onprogress) {
  113. self.onprogress(result);
  114. }
  115. } else {
  116. if (successCallback) {
  117. successCallback(result);
  118. }
  119. }
  120. };
  121. xhrImpl.upload(win, fail, [filePath, server, fileKey, fileName, mimeType, params, trustAllHosts, chunkedMode, headers, this._id, httpMethod]);
  122. };
  123. /**
  124. * Downloads a file form a given URL and saves it to the specified directory.
  125. * @param source {String} URL of the server to receive the file
  126. * @param target {String} Full path of the file on the device
  127. * @param successCallback (Function} Callback to be invoked when upload has completed
  128. * @param errorCallback {Function} Callback to be invoked upon error
  129. * @param trustAllHosts {Boolean} Optional trust all hosts (e.g. for self-signed certs), defaults to false
  130. * @param options {FileDownloadOptions} Optional parameters such as headers
  131. */
  132. FileTransfer.prototype.download = function(source, target, successCallback, errorCallback, trustAllHosts, options) {
  133. argscheck.checkArgs('ssFF*', 'FileTransfer.download', arguments);
  134. var self = this;
  135. var basicAuthHeader = getBasicAuthHeader(source);
  136. if (basicAuthHeader) {
  137. options = options || {};
  138. options.headers = options.headers || {};
  139. options.headers[basicAuthHeader.name] = basicAuthHeader.value;
  140. }
  141. var headers = null;
  142. if (options) {
  143. headers = options.headers || null;
  144. }
  145. var win = function(result) {
  146. if (typeof result.lengthComputable != "undefined") {
  147. if (self.onprogress) {
  148. return self.onprogress(result);
  149. }
  150. } else if (successCallback) {
  151. successCallback(result);
  152. }
  153. };
  154. var fail = errorCallback && function(e) {
  155. var error = new FileTransferError(e.code, e.source, e.target, e.http_status, e.body);
  156. errorCallback(error);
  157. };
  158. xhrImpl.download(win, fail, [source, target, trustAllHosts, this._id, headers]);
  159. };
  160. /**
  161. * Aborts the ongoing file transfer on this object. The original error
  162. * callback for the file transfer will be called if necessary.
  163. */
  164. FileTransfer.prototype.abort = function() {
  165. xhrImpl.abort(null, null, [this._id]);
  166. };
  167. module.exports = FileTransfer;