diff --git a/src/android/FileTransfer.java b/src/android/FileTransfer.java index 5a3c5d62..18114e9d 100644 --- a/src/android/FileTransfer.java +++ b/src/android/FileTransfer.java @@ -664,11 +664,13 @@ private void download(final String source, final String target, JSONArray args, final String objectId = args.getString(3); final JSONObject headers = args.optJSONObject(4); + final boolean suppressProgress = args.optBoolean(5) || false; final Uri sourceUri = resourceApi.remapUri(Uri.parse(source)); int uriType = CordovaResourceApi.getUriType(sourceUri); final boolean useHttps = uriType == CordovaResourceApi.URI_TYPE_HTTPS; final boolean isLocalTransfer = !useHttps && uriType != CordovaResourceApi.URI_TYPE_HTTP; + if (uriType == CordovaResourceApi.URI_TYPE_UNKNOWN) { JSONObject error = createFileTransferError(INVALID_URL_ERR, source, target, null, 0, null); LOG.e(LOG_TAG, "Unsupported URI: " + sourceUri); @@ -812,10 +814,12 @@ public void run() { while ((bytesRead = inputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, bytesRead); // Send a progress event. - progress.setLoaded(inputStream.getTotalRawBytesRead()); - PluginResult progressResult = new PluginResult(PluginResult.Status.OK, progress.toJSONObject()); - progressResult.setKeepCallback(true); - context.sendPluginResult(progressResult); + if (!suppressProgress) { + progress.setLoaded(inputStream.getTotalRawBytesRead()); + PluginResult progressResult = new PluginResult(PluginResult.Status.OK, progress.toJSONObject()); + progressResult.setKeepCallback(true); + context.sendPluginResult(progressResult); + } } } finally { synchronized (context) { diff --git a/src/ios/CDVFileTransfer.h b/src/ios/CDVFileTransfer.h index b7301efb..00ba33b7 100644 --- a/src/ios/CDVFileTransfer.h +++ b/src/ios/CDVFileTransfer.h @@ -81,6 +81,7 @@ extern NSString* const kOptionsKeyCookie; @property (nonatomic, assign) long long bytesTransfered; @property (nonatomic, assign) long long bytesExpected; @property (nonatomic, assign) BOOL trustAllHosts; +@property (nonatomic, assign) BOOL suppressProgress; @property (strong) NSFileHandle* targetFileHandle; @property (nonatomic, strong) CDVFileTransferEntityLengthRequest* entityLengthRequest; @property (nonatomic, strong) CDVFile *filePlugin; diff --git a/src/ios/CDVFileTransfer.m b/src/ios/CDVFileTransfer.m index f1bb44d9..c7f01e90 100644 --- a/src/ios/CDVFileTransfer.m +++ b/src/ios/CDVFileTransfer.m @@ -424,6 +424,7 @@ - (void)download:(CDVInvokedUrlCommand*)command BOOL trustAllHosts = [[command argumentAtIndex:2 withDefault:[NSNumber numberWithBool:NO]] boolValue]; // allow self-signed certs NSString* objectId = [command argumentAtIndex:3]; NSDictionary* headers = [command argumentAtIndex:4 withDefault:nil]; + BOOL suppressProgress = [[command argumentAtIndex:5 withDefault:[NSNumber numberWithBool:NO]] boolValue]; // prevent progress events CDVPluginResult* result = nil; CDVFileTransferError errorCode = 0; @@ -481,6 +482,7 @@ - (void)download:(CDVInvokedUrlCommand*)command delegate.target = [targetURL absoluteString]; delegate.targetURL = targetURL; delegate.trustAllHosts = trustAllHosts; + delegate.suppressProgress = suppressProgress; delegate.filePlugin = [self.commandDelegate getCommandInstance:@"File"]; delegate.backgroundTaskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ [delegate cancelTransfer:delegate.connection]; @@ -801,7 +803,7 @@ - (void)updateBytesExpected:(long long)newBytesExpected - (void)updateProgress { - if (self.direction == CDV_TRANSFER_DOWNLOAD) { + if (self.direction == CDV_TRANSFER_DOWNLOAD && !(self.suppressProgress)) { BOOL lengthComputable = (self.bytesExpected != NSURLResponseUnknownLength); // If the response is GZipped, and we have an outstanding HEAD request to get // the length, then hold off on sending progress events. diff --git a/types/index.d.ts b/types/index.d.ts index ad8d9945..b59b66c4 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -100,6 +100,9 @@ interface FileUploadOptions { interface FileDownloadOptions { /** A map of header name/header values. */ headers?: {}; + /** Suppress progress events generation; can help to reduce cordova communication volume (and prevent some stack overflows). */ + suppressProgress?: boolean; + } /** A FileTransferError object is passed to an error callback when an error occurs. */ diff --git a/www/FileTransfer.js b/www/FileTransfer.js index 99389717..59af1778 100644 --- a/www/FileTransfer.js +++ b/www/FileTransfer.js @@ -190,6 +190,7 @@ FileTransfer.prototype.upload = function (filePath, server, successCallback, err FileTransfer.prototype.download = function (source, target, successCallback, errorCallback, trustAllHosts, options) { argscheck.checkArgs('ssFF*', 'FileTransfer.download', arguments); var self = this; + var suppressProgress = !!((options || {}).suppressProgress); var basicAuthHeader = getBasicAuthHeader(source); if (basicAuthHeader) { @@ -240,7 +241,7 @@ FileTransfer.prototype.download = function (source, target, successCallback, err errorCallback(error); }; - exec(win, fail, 'FileTransfer', 'download', [source, target, trustAllHosts, this._id, headers]); + exec(win, fail, 'FileTransfer', 'download', [source, target, trustAllHosts, this._id, headers, suppressProgress]); }; /** diff --git a/www/browser/FileTransfer.js b/www/browser/FileTransfer.js index 01e178e8..f9f08b0f 100644 --- a/www/browser/FileTransfer.js +++ b/www/browser/FileTransfer.js @@ -230,6 +230,7 @@ FileTransfer.prototype.upload = function (filePath, server, successCallback, err */ FileTransfer.prototype.download = function (source, target, successCallback, errorCallback, trustAllHosts, options) { argscheck.checkArgs('ssFF*', 'FileTransfer.download', arguments); + var suppressProgress = !!((options || {}).suppressProgress); // Check if target URL doesn't contain spaces. If contains, it should be escaped first // (see https://github.com/apache/cordova-plugin-file-transfer/blob/master/doc/index.md#download) @@ -321,7 +322,7 @@ FileTransfer.prototype.download = function (source, target, successCallback, err }; xhr.onprogress = function (e) { - if (that.onprogress) { + if (that.onprogress && !suppressProgress) { that.onprogress(e); } };