Updated Branches: refs/heads/master 141109d9c -> a9db8e3d8
[all] Add FileTransfer object id, onprogress event, abort method. Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/commit/a9db8e3d Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/a9db8e3d Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/a9db8e3d Branch: refs/heads/master Commit: a9db8e3d85a08cab6ccf86f29cc476c1178d2d57 Parents: 141109d Author: Brion Vibber <br...@pobox.com> Authored: Sun Aug 26 16:24:03 2012 -0700 Committer: Andrew Grieve <agri...@chromium.org> Committed: Thu Sep 20 23:13:27 2012 -0400 ---------------------------------------------------------------------- lib/common/plugin/FileTransfer.js | 70 +++++++++++++++++++++------ lib/common/plugin/FileTransferError.js | 1 + 2 files changed, 56 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/a9db8e3d/lib/common/plugin/FileTransfer.js ---------------------------------------------------------------------- diff --git a/lib/common/plugin/FileTransfer.js b/lib/common/plugin/FileTransfer.js index 23430ee..1ace9ba 100644 --- a/lib/common/plugin/FileTransfer.js +++ b/lib/common/plugin/FileTransfer.js @@ -20,13 +20,27 @@ */ var exec = require('cordova/exec'), - FileTransferError = require('cordova/plugin/FileTransferError'); + FileTransferError = require('cordova/plugin/FileTransferError'), + ProgressEvent = require('cordova/plugin/ProgressEvent'); + +function newProgressEvent(result) { + var pe = new ProgressEvent(); + pe.lengthComputable = result.lengthComputable; + pe.loaded = result.loaded; + pe.total = result.total; + return pe; +} + +var idCounter = 0; /** * FileTransfer uploads a file to a remote server. * @constructor */ -var FileTransfer = function() {}; +var FileTransfer = function() { + this._id = ++idCounter; + this.onprogress = null; // optional callback +}; /** * Given an absolute file path, uploads a file on the device to a remote server @@ -69,7 +83,17 @@ FileTransfer.prototype.upload = function(filePath, server, successCallback, erro errorCallback(error); }; - exec(successCallback, fail, 'FileTransfer', 'upload', [filePath, server, fileKey, fileName, mimeType, params, trustAllHosts, chunkedMode, headers]); + var self = this; + var win = function(result) { + if (typeof result.lengthComputable != "undefined") { + if (self.onprogress) { + return self.onprogress(newProgressEvent(result)); + } + } else { + return successCallback(result); + } + }; + exec(win, fail, 'FileTransfer', 'upload', [filePath, server, fileKey, fileName, mimeType, params, trustAllHosts, chunkedMode, headers, this._id]); }; /** @@ -83,19 +107,26 @@ FileTransfer.prototype.upload = function(filePath, server, successCallback, erro FileTransfer.prototype.download = function(source, target, successCallback, errorCallback, trustAllHosts) { // sanity parameter checking if (!source || !target) throw new Error("FileTransfer.download requires source URI and target URI parameters at the minimum."); + var self = this; var win = function(result) { - var entry = null; - if (result.isDirectory) { - entry = new (require('cordova/plugin/DirectoryEntry'))(); + if (typeof result.lengthComputable != "undefined") { + if (self.onprogress) { + return self.onprogress(newProgressEvent(result)); + } + } else { + var entry = null; + if (result.isDirectory) { + entry = new (require('cordova/plugin/DirectoryEntry'))(); + } + else if (result.isFile) { + entry = new (require('cordova/plugin/FileEntry'))(); + } + entry.isDirectory = result.isDirectory; + entry.isFile = result.isFile; + entry.name = result.name; + entry.fullPath = result.fullPath; + successCallback(entry); } - else if (result.isFile) { - entry = new (require('cordova/plugin/FileEntry'))(); - } - entry.isDirectory = result.isDirectory; - entry.isFile = result.isFile; - entry.name = result.name; - entry.fullPath = result.fullPath; - successCallback(entry); }; var fail = function(e) { @@ -103,7 +134,16 @@ FileTransfer.prototype.download = function(source, target, successCallback, erro errorCallback(error); }; - exec(win, errorCallback, 'FileTransfer', 'download', [source, target, trustAllHosts]); + exec(win, errorCallback, 'FileTransfer', 'download', [source, target, trustAllHosts, this._id]); }; +/** + * Aborts the ongoing file transfer on this object + * @param successCallback {Function} Callback to be invoked upon success + * @param errorCallback {Function} Callback to be invoked upon error + */ +FileTransfer.prototype.abort = function(successCallback, errorCallback) { + exec(successCallback, errorCallback, 'FileTransfer', 'abort', [this._id]); +} + module.exports = FileTransfer; http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/a9db8e3d/lib/common/plugin/FileTransferError.js ---------------------------------------------------------------------- diff --git a/lib/common/plugin/FileTransferError.js b/lib/common/plugin/FileTransferError.js index 4b638fb..7e6edb1 100644 --- a/lib/common/plugin/FileTransferError.js +++ b/lib/common/plugin/FileTransferError.js @@ -33,5 +33,6 @@ var FileTransferError = function(code, source, target, status) { FileTransferError.FILE_NOT_FOUND_ERR = 1; FileTransferError.INVALID_URL_ERR = 2; FileTransferError.CONNECTION_ERR = 3; +FileTransferError.ABORT_ERR = 4; module.exports = FileTransferError;