cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] maverickmishra closed pull request #147: Master
Date Thu, 01 Jan 1970 00:00:00 GMT
maverickmishra closed pull request #147: Master
URL: https://github.com/apache/cordova-plugin-file-transfer/pull/147
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/src/android/FileTransfer.java b/src/android/FileTransfer.java
index b9b99dc..71f3c88 100644
--- a/src/android/FileTransfer.java
+++ b/src/android/FileTransfer.java
@@ -735,6 +735,7 @@ private void download(final String source, final String target, JSONArray
args,
         final boolean trustEveryone = args.optBoolean(2);
         final String objectId = args.getString(3);
         final JSONObject headers = args.optJSONObject(4);
+        final JSONObject postData = args.optJSONObject(5);
 
         final Uri sourceUri = resourceApi.remapUri(Uri.parse(source));
         // Accept a path or a URI for the source.
@@ -841,8 +842,6 @@ public void run() {
                             https.setHostnameVerifier(DO_NOT_VERIFY);
                         }
 
-                        connection.setRequestMethod("GET");
-
                         // TODO: Make OkHttp use this CookieManager by default.
                         String cookie = getCookies(sourceUri.toString());
 
@@ -858,6 +857,17 @@ public void run() {
                         if (headers != null) {
                             addHeadersToRequest(connection, headers);
                         }
+                        
+                         if(postData != null){
+                            connection.setRequestMethod("POST");
+                            String str =  postData.toString();
+                            byte[] outputInBytes = str.getBytes("UTF-8");
+                            OutputStream os = connection.getOutputStream();
+                            os.write( outputInBytes );
+                            os.close();
+                        } else {
+                            connection.setRequestMethod("GET");
+                        }
 
                         connection.connect();
                         if (connection.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED)
{
diff --git a/src/ios/CDVFileTransfer.m b/src/ios/CDVFileTransfer.m
index 4775a6d..413b1c9 100644
--- a/src/ios/CDVFileTransfer.m
+++ b/src/ios/CDVFileTransfer.m
@@ -420,6 +420,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];
+    NSDictionary* postData = [command argumentAtIndex:5 withDefault:nil];
 
     CDVPluginResult* result = nil;
     CDVFileTransferError errorCode = 0;
@@ -457,6 +458,19 @@ - (void)download:(CDVInvokedUrlCommand*)command
 
     NSMutableURLRequest* req = [NSMutableURLRequest requestWithURL:sourceURL];
     [self applyRequestHeaders:headers toRequest:req];
+    
+    if(postData){
+        NSError *error;
+        
+        NSData* jsondata = [NSJSONSerialization dataWithJSONObject:postData
+                                                           options:NSJSONWritingPrettyPrinted
+                                                             error:&error];
+        
+        //[req setValue:@"application/json" forHTTPHeaderField:@"Content-type"];
+        //[req setValue:@"application/json" forHTTPHeaderField:@"Accept"];
+        [req setHTTPMethod:@"POST"];
+        [req setHTTPBody:jsondata];
+    }
 
     CDVFileTransferDelegate* delegate = [[CDVFileTransferDelegate alloc] init];
     delegate.command = self;
diff --git a/src/windows/FileTransferProxy.js b/src/windows/FileTransferProxy.js
index 56df315..7122501 100644
--- a/src/windows/FileTransferProxy.js
+++ b/src/windows/FileTransferProxy.js
@@ -400,6 +400,7 @@ exec(win, fail, 'FileTransfer', 'upload',
         var target = options[1];
         var downloadId = options[3];
         var headers = options[4] || {};
+        var postData = options[5];
 
         if (!target) {
             errorCallback(new FTErr(FTErr.FILE_NOT_FOUND_ERR));
@@ -432,9 +433,27 @@ exec(win, fail, 'FileTransfer', 'upload',
         // Create internal download operation object
         fileTransferOps[downloadId] = new FileTransferOperation(FileTransferOperation.PENDING,
null);
 
-        var downloadCallback = function(storageFolder) {
-            storageFolder.createFileAsync(tempFileName, Windows.Storage.CreationCollisionOption.replaceExisting).then(function
(storageFile) {
+        var downloadCallback = function (storageFolder) {
 
+            var applicationData = Windows.Storage.ApplicationData.current;
+            var localFolder = applicationData.localFolder;
+            var filename = "jsonToPost";
+
+            var whenEverythingIsReady;
+            var whenFileTargetIsCreated = storageFolder.createFileAsync(tempFileName, Windows.Storage.CreationCollisionOption.replaceExisting);
+            if (postData) {
+                var whenFilePostIsOpen = localFolder.getFileAsync(filename);
+                whenEverythingIsReady = WinJS.Promise.join([whenFileTargetIsCreated, whenFilePostIsOpen]);
+            } else {
+                whenEverythingIsReady = WinJS.Promise.join([whenFileTargetIsCreated]);
+            }
+            
+
+            whenEverythingIsReady.then(function (args) {
+                var storageFile = args[0];
+                var postFile;
+                if(postData) postFile = args[1];
+                
                 if (alreadyCancelled(downloadId)) {
                     errorCallback(new FTErr(FTErr.ABORT_ERR, source, target));
                     return;
@@ -448,10 +467,19 @@ exec(win, fail, 'FileTransfer', 'upload',
                     }
                 }
 
+                if (postData) {
+                    downloader.method = "POST";
+                }
+
                 // create download object. This will throw an exception if URL is malformed
                 try {
                     var uri = Windows.Foundation.Uri(source);
-                    download = downloader.createDownload(uri, storageFile);
+                    if (postData) {
+                        download = downloader.createDownload(uri, storageFile, postFile);
+                    } else {
+                        download = downloader.createDownload(uri, storageFile);
+                    }
+                        
                 } catch (e) {
                     // so we handle this and call errorCallback
                     errorCallback(new FTErr(FTErr.INVALID_URL_ERR));
@@ -535,7 +563,8 @@ exec(win, fail, 'FileTransfer', 'upload',
 
                     successCallback(progressEvent, { keepCallback: true });
                 });
-            }, function(error) {
+            }, function (error) {
+                
                 errorCallback(new FTErr(FTErr.FILE_NOT_FOUND_ERR, source, target, null, null,
error));
             });
         };
@@ -544,19 +573,35 @@ exec(win, fail, 'FileTransfer', 'upload',
             errorCallback(new FTErr(FTErr.FILE_NOT_FOUND_ERR, source, target, null, null,
error));
         };
 
-        Windows.Storage.StorageFolder.getFolderFromPathAsync(path).then(downloadCallback,
function (error) {
+        var dirNotFoundDirectoryCallback = function (error) {
             // Handle non-existent directory
             if (error.number === -2147024894) {
                 var parent = path.substr(0, path.lastIndexOf('\\')),
                     folderNameToCreate = path.substr(path.lastIndexOf('\\') + 1);
 
-                Windows.Storage.StorageFolder.getFolderFromPathAsync(parent).then(function(parentFolder)
{
+                Windows.Storage.StorageFolder.getFolderFromPathAsync(parent).then(function
(parentFolder) {
                     parentFolder.createFolderAsync(folderNameToCreate).then(downloadCallback,
fileNotFoundErrorCallback);
                 }, fileNotFoundErrorCallback);
             } else {
                 fileNotFoundErrorCallback();
             }
-        });
+        };
+
+        if (postData) {
+            var applicationData = Windows.Storage.ApplicationData.current;
+            var localFolder = applicationData.localFolder;
+            var filename = "jsonToPost";
+
+            localFolder.createFileAsync(filename, Windows.Storage.CreationCollisionOption.replaceExisting)
+                .then(function (file) {
+                    Windows.Storage.FileIO.writeTextAsync(file,
+                                JSON.stringify(postData));
+                }).then(function () {
+                    Windows.Storage.StorageFolder.getFolderFromPathAsync(path).then(downloadCallback,
dirNotFoundDirectoryCallback);
+                });  
+        } else {
+            Windows.Storage.StorageFolder.getFolderFromPathAsync(path).then(downloadCallback,
dirNotFoundDirectoryCallback);
+        }
     },
 
     abort: function (successCallback, error, options) {
@@ -575,4 +620,4 @@ exec(win, fail, 'FileTransfer', 'upload',
 
 };
 
-require("cordova/exec/proxy").add("FileTransfer",module.exports);
+require("cordova/exec/proxy").add("FileTransfer",module.exports);
\ No newline at end of file
diff --git a/www/FileTransfer.js b/www/FileTransfer.js
index 80cf91c..ad7ad0a 100644
--- a/www/FileTransfer.js
+++ b/www/FileTransfer.js
@@ -225,7 +225,7 @@ FileTransfer.prototype.download = function(source, target, successCallback,
erro
         errorCallback(error);
     };
 
-    exec(win, fail, 'FileTransfer', 'download', [source, target, trustAllHosts, this._id,
headers]);
+    exec(win, fail, 'FileTransfer', 'download', [source, target, trustAllHosts, this._id,
headers, options.postData]);
 };
 
 /**


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org


Mime
View raw message