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 #178: CB-12745: Adding support for non base64 encoded data-URIs
Date Thu, 01 Jan 1970 00:00:00 GMT
maverickmishra closed pull request #178: CB-12745: Adding support for non base64 encoded data-URIs
URL: https://github.com/apache/cordova-plugin-file-transfer/pull/178
 
 
   

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/ios/CDVFileTransfer.m b/src/ios/CDVFileTransfer.m
index ab59012..46efce9 100644
--- a/src/ios/CDVFileTransfer.m
+++ b/src/ios/CDVFileTransfer.m
@@ -308,7 +308,7 @@ - (void)fileDataForUploadCommand:(CDVInvokedUrlCommand*)command
     NSString* server = [command argumentAtIndex:1];
     NSError* __autoreleasing err = nil;
 
-    if ([source hasPrefix:@"data:"] && [source rangeOfString:@"base64"].location
!= NSNotFound) {
+    if ([source hasPrefix:@"data:"]) {
         NSRange commaRange = [source rangeOfString: @","];
         if (commaRange.location == NSNotFound) {
             // Return error is there is no comma
@@ -325,8 +325,17 @@ - (void)fileDataForUploadCommand:(CDVInvokedUrlCommand*)command
             return;
         }
 
-        NSData *fileData = [[NSData alloc] initWithBase64EncodedString:[source substringFromIndex:(commaRange.location
+ 1)] options:NSDataBase64DecodingIgnoreUnknownCharacters];
-        [self uploadData:fileData command:command];
+        // check if source is base64 encoded
+        if ([source rangeOfString:@"base64"].location != NSNotFound) {
+            NSData *fileData = [[NSData alloc] initWithBase64EncodedString:[source substringFromIndex:(commaRange.location
+ 1)] options:NSDataBase64DecodingIgnoreUnknownCharacters];
+            [self uploadData:fileData command:command];
+        }
+        // if not, apply urldecode and encode it to NSData using UTF8
+        else {
+            NSData *fileData = [[[source substringFromIndex:(commaRange.location + 1)] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
dataUsingEncoding:NSUTF8StringEncoding];
+            [self uploadData:fileData command:command];
+        }
+
         return;
     }
 
diff --git a/src/windows/FileTransferProxy.js b/src/windows/FileTransferProxy.js
index 5d1363f..24866ff 100644
--- a/src/windows/FileTransferProxy.js
+++ b/src/windows/FileTransferProxy.js
@@ -191,7 +191,13 @@ exec(win, fail, 'FileTransfer', 'upload',
             return;
         }
 
-        if (filePath.indexOf("data:") === 0 && filePath.indexOf("base64") !== -1)
{
+        if (filePath.indexOf("data:") === 0) {
+            // check if we have base64 encoded data
+            var isBase64 = false;
+            if (filePath.indexOf("base64") !== -1) {
+                isBase64 = true;
+            }
+
             // First a DataWriter object is created, backed by an in-memory stream where

             // the data will be stored.
             var writer = Windows.Storage.Streams.DataWriter(new Windows.Storage.Streams.InMemoryRandomAccessStream());
@@ -241,10 +247,18 @@ exec(win, fail, 'FileTransfer', 'upload',
                 uploader.setRequestHeader("Content-Type", "multipart/form-data; boundary="
+ BOUNDARY);
                 writer.writeString(multipartParams);
                 writer.writeString(multipartFile);
-                writer.writeBytes(stringToByteArray(fileDataString));
+                if (isBase64) {
+                    writer.writeBytes(stringToByteArray(fileDataString));
+                } else {
+                    writer.writeString(decodeURIComponent(fileDataString));
+                }
                 writer.writeString(bound);
             } else {
-                writer.writeBytes(stringToByteArray(fileDataString));
+                if (isBase64) {
+                    writer.writeBytes(stringToByteArray(fileDataString));
+                } else {
+                    writer.writeString(decodeURIComponent(fileDataString));
+                }
             }
 
             var stream;


 

----------------------------------------------------------------
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