From e9d93c0a542e1b952fb742a94e711ca514148934 Mon Sep 17 00:00:00 2001 From: Nikita Matrosov Date: Thu, 6 Apr 2017 16:24:26 +0300 Subject: [PATCH] CB-10696 iOS: Encode target path with spaces --- src/ios/CDVFileTransfer.m | 10 ++++++++++ tests/tests.js | 41 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/ios/CDVFileTransfer.m b/src/ios/CDVFileTransfer.m index 9b01049..ab59012 100644 --- a/src/ios/CDVFileTransfer.m +++ b/src/ios/CDVFileTransfer.m @@ -440,6 +440,13 @@ static CFIndex WriteDataToStream(NSData* data, CFWriteStreamRef stream) targetURL = [[self.commandDelegate getCommandInstance:@"File"] fileSystemURLforLocalPath:target].url; } else { targetURL = [NSURL URLWithString:target]; + + if (targetURL == nil) { + NSString* targetUrlTextEscaped = [target stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]]; + if (targetUrlTextEscaped) { + targetURL = [NSURL URLWithString:targetUrlTextEscaped]; + } + } } NSURL* sourceURL = [NSURL URLWithString:source]; @@ -447,6 +454,9 @@ static CFIndex WriteDataToStream(NSData* data, CFWriteStreamRef stream) if (!sourceURL) { errorCode = INVALID_URL_ERR; NSLog(@"File Transfer Error: Invalid server URL %@", source); + } else if (!targetURL) { + errorCode = INVALID_URL_ERR; + NSLog(@"File Tranfer Error: Invalid target URL %@", target); } else if (![targetURL isFileURL]) { CDVFilesystemURL *fsURL = [CDVFilesystemURL fileSystemURLWithString:target]; if (!fsURL) { diff --git a/tests/tests.js b/tests/tests.js index 98acb4c..0efc64e 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -398,6 +398,47 @@ exports.defineAutoTests = function () { specContext.transfer.download(fileURL, specContext.localFilePath, downloadWin, downloadFail); }, DOWNLOAD_TIMEOUT * 10); // to give Heroku server some time to wake up + it("filetransfer.spec.4.1 should download a file using target name with space", function (done) { + + var fileURL = SERVER + "/robots.txt"; + this.fileName = "test file.txt"; + this.localFilePath = this.root.toURL() + this.fileName; + + var specContext = this; + + var fileWin = function (blob) { + + if (specContext.transfer.onprogress.calls.any()) { + var lastProgressEvent = specContext.transfer.onprogress.calls.mostRecent().args[0]; + expect(lastProgressEvent.loaded).not.toBeGreaterThan(blob.size); + } else { + console.log("no progress events were emitted"); + } + + done(); + }; + + var fileSystemFail = function() { + unexpectedCallbacks.fileSystemFail(); + done(); + }; + + var downloadFail = function() { + unexpectedCallbacks.httpFail(); + done(); + }; + + var downloadWin = function (entry) { + + verifyDownload(entry, specContext); + + // verify the FileEntry representing this file + entry.file(fileWin, fileSystemFail); + }; + + specContext.transfer.download(fileURL, specContext.localFilePath, downloadWin, downloadFail); + }, DOWNLOAD_TIMEOUT); + it("filetransfer.spec.5 should download a file using http basic auth", function (done) { var fileURL = SERVER_WITH_CREDENTIALS + "/download_basic_auth"; var specContext = this;