[FileTransfer] Fixed file upload for server 22/249522/4 tizen_5.0
authorPiotr Kosko <p.kosko@samsung.com>
Fri, 11 Dec 2020 13:21:18 +0000 (14:21 +0100)
committerPiotr Kosko <p.kosko@samsung.com>
Mon, 14 Dec 2020 11:18:03 +0000 (12:18 +0100)
[Issue] File was not properly sent to the server

[Verification] Described in JIRA task:
https://code.sec.samsung.net/jira/browse/XWALK-2197
TCT passrate 100%

Change-Id: Ic3716c1e2eaa9bc799120adb86aa71c4146064a1

src/lib/plugins/cordova-plugin-file-transfer/tizen/FileTransfer.js

index e84fb2a..f4342a4 100755 (executable)
@@ -98,74 +98,95 @@ exports = {
 
     function successCB(entry) {
       if (entry.isFile) {
-        entry.file(function(file) {
-          function uploadFile(blobFile) {
-            var fd = new FormData();
+        // initialize XMLHTTPRequest (without starting it)
+        var xhr = uploads[id] = new XMLHttpRequest();
 
-            fd.append(fileKey, blobFile, fileName);
+        xhr.open(httpMethod, server);
 
-            for (var prop in params) {
-              if(params.hasOwnProperty(prop)) {
-                 fd.append(prop, params[prop]);
-              }
-            }
-            var xhr = uploads[id] = new XMLHttpRequest();
+        // Fill XHR headers
+        for (var header in headers) {
+          if (headers.hasOwnProperty(header)) {
+            xhr.setRequestHeader(header, headers[header]);
+          }
+        }
 
-            xhr.open(httpMethod, server);
+        xhr.ontimeout = function(evt) {
+          fail(FileTransferError.CONNECTION_ERR, this.status, this.response);
+        };
 
-            // Fill XHR headers
-            for (var header in headers) {
-              if (headers.hasOwnProperty(header)) {
-                xhr.setRequestHeader(header, headers[header]);
-              }
-            }
+        xhr.onerror = function() {
+          fail(FileTransferError.CONNECTION_ERR, this.status, this.response);
+        };
 
-            xhr.onload = function(evt) {
-              if (xhr.status === 200) {
-                uploads[id] && delete uploads[id];
-                successCallback({
-                  bytesSent: file.size,
-                  responseCode: xhr.status,
-                  response: xhr.response
-                });
-              } else if (xhr.status === 404) {
-                fail(FileTransferError.INVALID_URL_ERR, this.status, this.response);
-              } else {
-                fail(FileTransferError.CONNECTION_ERR, this.status, this.response);
-              }
-            };
-
-            xhr.ontimeout = function(evt) {
-              fail(FileTransferError.CONNECTION_ERR, this.status, this.response);
-            };
+        xhr.onabort = function () {
+          fail(FileTransferError.ABORT_ERR, this.status, this.response);
+        };
 
-            xhr.onerror = function() {
-              fail(FileTransferError.CONNECTION_ERR, this.status, this.response);
-            };
-
-            xhr.onabort = function () {
-              fail(FileTransferError.ABORT_ERR, this.status, this.response);
-            };
+        xhr.upload.onprogress = function (e) {
+          successCallback(e);
+        };
+        // end of XMLHTTPRequest initialization
 
-            xhr.upload.onprogress = function (e) {
-              successCallback(e);
-            };
+        var fullPath = entry.toURL();
+        var fileHandle;
 
-            xhr.send(fd);
+        function closeHandle() {
+          if (fileHandle) {
+            fileHandle.close();
+          }
+        }
 
-            // Special case when transfer already aborted, but XHR isn't sent.
-            // In this case XHR won't fire an abort event, so we need to check if transfers record
-            // isn't deleted by filetransfer.abort and if so, call XHR's abort method again
-            if (!uploads[id]) {
-              xhr.abort();
+        function uploadFile(blobFile) {
+          closeHandle();
+          // create FormData
+          var fd = new FormData();
+          fd.append(fileKey, blobFile, fileName);
+          for (var prop in params) {
+            if(params.hasOwnProperty(prop)) {
+              fd.append(prop, params[prop]);
             }
           }
 
-          uploadFile(file);
+          // sending already initialized request
+          // 'onload' needs to be defined here because it needs blobFile.size
+          xhr.onload = function(evt) {
+            // 2xx codes are valid
+            if (xhr.status >= 200 && xhr.status < 300) {
+              uploads[id] && delete uploads[id];
+              successCallback({
+                bytesSent: blobFile.size,
+                responseCode: xhr.status,
+                response: xhr.response
+              });
+            } else if (xhr.status === 404) {
+              fail(FileTransferError.INVALID_URL_ERR, this.status, this.response);
+            } else {
+              fail(FileTransferError.CONNECTION_ERR, this.status, this.response);
+            }
+          };
+          xhr.send(fd);
+
+          // Special case when transfer already aborted, but XHR isn't sent.
+          // In this case XHR won't fire an abort event, so we need to check if transfers record
+          // isn't deleted by filetransfer.abort and if so, call XHR's abort method again
+          if (!uploads[id]) {
+            xhr.abort();
+          }
+        }
 
-        }, function(error) {
-          fail(FileTransferError.CONNECTION_ERR);
-        });
+        try {
+          fileHandle = tizen.filesystem.openFile(fullPath, 'r');
+          var fileBlob = fileHandle.readBlobNonBlocking(
+            uploadFile,
+            function(error) {
+              closeHandle();
+              fail(FileTransferError.ABORT_ERR, 'Could not read file ' + e);
+            }
+          );
+        } catch (e) {
+          closeHandle();
+          fail(FileTransferError.ABORT_ERR, 'Could not read file ' + e);
+        }
       }
     }