Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/android/FileTransfer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions src/ios/CDVFileTransfer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/ios/CDVFileTransfer.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
3 changes: 2 additions & 1 deletion www/FileTransfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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]);
};

/**
Expand Down
3 changes: 2 additions & 1 deletion www/browser/FileTransfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
};
Expand Down