You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by GitBox <gi...@apache.org> on 2017/11/21 22:28:03 UTC

[GitHub] maverickmishra closed pull request #116: CB-10044 FileTransfer plug in can upload parts of a file to implement resumablejs

maverickmishra closed pull request #116: CB-10044 FileTransfer plug in can upload parts of a file to implement resumablejs
URL: https://github.com/apache/cordova-plugin-file-transfer/pull/116
 
 
   

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 0efbd4e..06af303 100644
--- a/src/android/FileTransfer.java
+++ b/src/android/FileTransfer.java
@@ -291,6 +291,11 @@ private void upload(final String source, final String target, JSONArray args, Ca
         final JSONObject headers = args.optJSONObject(8) == null ? params.optJSONObject("headers") : args.optJSONObject(8);
         final String objectId = args.getString(9);
         final String httpMethod = getArgument(args, 10, "POST");
+        final String s_startByte = (args.isNull(11) ? "0" : getArgument(args, 11, "POST"));
+        final String s_endByte = (args.isNull(12) ? "-1" : getArgument(args, 12, "POST"));
+
+        final int startByte = Integer.parseInt(s_startByte);
+        final int endByte = Integer.parseInt(s_endByte);
         
         final CordovaResourceApi resourceApi = webView.getResourceApi();
 
@@ -416,7 +421,11 @@ public void run() {
                     
                     int stringLength = beforeDataBytes.length + tailParamsBytes.length;
                     if (readResult.length >= 0) {
-                        fixedLength = (int)readResult.length;
+                        if(endByte > 0){
+                          fixedLength = endByte-startByte;;
+                        }else{
+                          fixedLength = (int)readResult.length;
+                        }
                         if (multipartFormUpload)
                             fixedLength += stringLength;
                         progress.setLengthComputable(true);
@@ -455,33 +464,48 @@ public void run() {
                             sendStream.write(beforeDataBytes);
                             totalBytes += beforeDataBytes.length;
                         }
-                        
-                        // create a buffer of maximum size
-                        int bytesAvailable = readResult.inputStream.available();
-                        int bufferSize = Math.min(bytesAvailable, MAX_BUFFER_SIZE);
-                        byte[] buffer = new byte[bufferSize];
-    
-                        // read file and write it into form...
-                        int bytesRead = readResult.inputStream.read(buffer, 0, bufferSize);
-    
-                        long prevBytesRead = 0;
-                        while (bytesRead > 0) {
-                            result.setBytesSent(totalBytes);
-                            sendStream.write(buffer, 0, bytesRead);
-                            totalBytes += bytesRead;
-                            if (totalBytes > prevBytesRead + 102400) {
-                                prevBytesRead = totalBytes;
-                                Log.d(LOG_TAG, "Uploaded " + totalBytes + " of " + fixedLength + " bytes");
-                            }
-                            bytesAvailable = readResult.inputStream.available();
-                            bufferSize = Math.min(bytesAvailable, MAX_BUFFER_SIZE);
-                            bytesRead = readResult.inputStream.read(buffer, 0, bufferSize);
-
-                            // Send a progress event.
-                            progress.setLoaded(totalBytes);
-                            PluginResult progressResult = new PluginResult(PluginResult.Status.OK, progress.toJSONObject());
-                            progressResult.setKeepCallback(true);
-                            context.sendPluginResult(progressResult);
+                        if(endByte>0){ //This means we want to read just part of the file
+                          int byteRange = endByte-startByte;
+                          byte[] buffer = new byte[byteRange];
+                          if(startByte > 0){ //Start from offset
+                            readResult.inputStream.skip(startByte);
+                          }
+                          int bytesRead = readResult.inputStream.read(buffer, 0, byteRange);
+                          totalBytes += byteRange;
+                          result.setBytesSent(totalBytes);
+                          sendStream.write(buffer, 0, bytesRead);
+                          progress.setLoaded(totalBytes);
+                          PluginResult progressResult = new PluginResult(PluginResult.Status.OK, progress.toJSONObject());
+                          progressResult.setKeepCallback(true);
+                          context.sendPluginResult(progressResult);
+                        }else{
+                          // create a buffer of maximum size
+                          int bytesAvailable = readResult.inputStream.available();
+                          int bufferSize = Math.min(bytesAvailable, MAX_BUFFER_SIZE);
+                          byte[] buffer = new byte[bufferSize];
+      
+                          // read file and write it into form...
+                          int bytesRead = readResult.inputStream.read(buffer, 0, bufferSize);
+      
+                          long prevBytesRead = 0;
+                          while (bytesRead > 0) {
+                              result.setBytesSent(totalBytes);
+                              sendStream.write(buffer, 0, bytesRead);
+                              totalBytes += bytesRead;
+                              if (totalBytes > prevBytesRead + 102400) {
+                                  prevBytesRead = totalBytes;
+                                  Log.d(LOG_TAG, "Uploaded " + totalBytes + " of " + fixedLength + " bytes");
+                              }
+                              bytesAvailable = readResult.inputStream.available();
+                              bufferSize = Math.min(bytesAvailable, MAX_BUFFER_SIZE);
+                              bytesRead = readResult.inputStream.read(buffer, 0, bufferSize);
+
+                              // Send a progress event.
+                              progress.setLoaded(totalBytes);
+                              PluginResult progressResult = new PluginResult(PluginResult.Status.OK, progress.toJSONObject());
+                              progressResult.setKeepCallback(true);
+                              context.sendPluginResult(progressResult);
+                          }
                         }
     
                         if (multipartFormUpload) {
diff --git a/src/ios/CDVFileTransfer.m b/src/ios/CDVFileTransfer.m
index dca440a..7000570 100644
--- a/src/ios/CDVFileTransfer.m
+++ b/src/ios/CDVFileTransfer.m
@@ -259,12 +259,24 @@ - (NSURLRequest*)requestForUploadCommand:(CDVInvokedUrlCommand*)command fileData
             CFRelease(writeStream);
         }];
     } else {
+        NSNumber* startByte = (NSNumber*)[command argumentAtIndex:11 withDefault:0];
+        NSNumber* endByte = (NSNumber*)[command argumentAtIndex:12 withDefault:0];
+        
+        NSInteger i_startByte = [startByte integerValue];
+        NSInteger i_endByte = [endByte integerValue];
+        
+        if(i_endByte == 0){
+            i_endByte = -1;
+        }
+        NSRange chunkRange = NSMakeRange(i_startByte, i_endByte-i_startByte);
+        NSData* chunkData = [fileData subdataWithRange:chunkRange];
+        
         if (multipartFormUpload) {
-            [postBodyBeforeFile appendData:fileData];
+            [postBodyBeforeFile appendData:chunkData];
             [postBodyBeforeFile appendData:postBodyAfterFile];
             [req setHTTPBody:postBodyBeforeFile];
         } else {
-            [req setHTTPBody:fileData];
+            [req setHTTPBody:chunkData];
         }
     }
     return req;
@@ -296,6 +308,15 @@ - (void)fileDataForUploadCommand:(CDVInvokedUrlCommand*)command
     NSString* source = (NSString*)[command argumentAtIndex:0];
     NSString* server = [command argumentAtIndex:1];
     NSError* __autoreleasing err = nil;
+    NSNumber* startByte = (NSNumber*)[command argumentAtIndex:11 withDefault:0];
+    NSNumber* endByte = (NSNumber*)[command argumentAtIndex:12 withDefault:0];
+    
+    NSInteger i_startByte = [startByte integerValue];
+    NSInteger i_endByte = [endByte integerValue];
+    
+    if(i_endByte == 0){
+        i_endByte = -1;
+    }
 
     CDVFilesystemURL *sourceURL = [CDVFilesystemURL fileSystemURLWithString:source];
     NSObject<CDVFileSystem> *fs;
@@ -305,7 +326,7 @@ - (void)fileDataForUploadCommand:(CDVInvokedUrlCommand*)command
         fs = [[self.commandDelegate getCommandInstance:@"File"] filesystemForURL:sourceURL];
     }
     if (fs) {
-        [fs readFileAtURL:sourceURL start:0 end:-1 callback:^(NSData *fileData, NSString *mimeType, CDVFileError err) {
+        [fs readFileAtURL:sourceURL start:i_startByte end:i_endByte callback:^(NSData *fileData, NSString *mimeType, CDVFileError err) {
             if (err) {
                 // We couldn't find the asset.  Send the appropriate error.
                 CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:[self createFileTransferError:NOT_FOUND_ERR AndSource:source AndTarget:server]];
diff --git a/tests/tests.js b/tests/tests.js
index fff1eef..eadac18 100644
--- a/tests/tests.js
+++ b/tests/tests.js
@@ -946,6 +946,25 @@ exports.defineAutoTests = function () {
                     // NOTE: removing uploadOptions cause Android to timeout
                     transfer.upload(localFilePath, fileURL, uploadWin, unexpectedCallbacks.httpFail, uploadOptionsPut);
                 }, UPLOAD_TIMEOUT);
+                it("filetransfer.spec.32 should be able to upload a part of a file (first 5 bytes)", function (done) {
+
+                    var fileURL = SERVER + '/upload';
+
+                    var uploadWin = function (uploadResult) {
+
+                        verifyUpload(uploadResult);
+
+                        if (cordova.platformId === 'ios') {
+                            expect(uploadResult.headers).toBeDefined('Expected headers to be defined.');
+                            expect(uploadResult.headers['Content-Type']).toBeDefined('Expected content-type header to be defined.');
+                        }
+
+                        done();
+                    };
+
+                    // NOTE: removing uploadOptions cause Android to timeout
+                    transfer.upload(localFilePath, fileURL, uploadWin, unexpectedCallbacks.httpFail, uploadOptions,true,0,5);
+                }, UPLOAD_TIMEOUT);
             });
         });
     });
diff --git a/www/FileTransfer.js b/www/FileTransfer.js
index add97da..b7b8220 100644
--- a/www/FileTransfer.js
+++ b/www/FileTransfer.js
@@ -98,7 +98,7 @@ var FileTransfer = function() {
 * @param options {FileUploadOptions} Optional parameters such as file name and mimetype
 * @param trustAllHosts {Boolean} Optional trust all hosts (e.g. for self-signed certs), defaults to false
 */
-FileTransfer.prototype.upload = function(filePath, server, successCallback, errorCallback, options, trustAllHosts) {
+FileTransfer.prototype.upload = function(filePath, server, successCallback, errorCallback, options, trustAllHosts, startByte, endByte) {
     argscheck.checkArgs('ssFFO*', 'FileTransfer.upload', arguments);
     // check for options
     var fileKey = null;
@@ -109,6 +109,13 @@ FileTransfer.prototype.upload = function(filePath, server, successCallback, erro
     var headers = null;
     var httpMethod = null;
     var basicAuthHeader = getBasicAuthHeader(server);
+    if(typeof(startByte) == 'undefined'){
+        startByte = 0;
+    }
+    if(typeof(endByte) == 'undefined'){
+        endByte = -1;
+    }
+
     if (basicAuthHeader) {
         server = server.replace(getUrlCredentials(server) + '@', '');
 
@@ -159,7 +166,7 @@ FileTransfer.prototype.upload = function(filePath, server, successCallback, erro
             successCallback && successCallback(result);
         }
     };
-    exec(win, fail, 'FileTransfer', 'upload', [filePath, server, fileKey, fileName, mimeType, params, trustAllHosts, chunkedMode, headers, this._id, httpMethod]);
+    exec(win, fail, 'FileTransfer', 'upload', [filePath, server, fileKey, fileName, mimeType, params, trustAllHosts, chunkedMode, headers, this._id, httpMethod,startByte,endByte]);
 };
 
 /**


 

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