Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implement FileTransfer with XHR for qnx

  • Loading branch information...
commit 7cb3604b21ce77c442ce469ae7de85f8ccb4b818 1 parent f50d20a
@haahmad haahmad authored gtanner committed
View
191 lib/blackberry/plugin/qnx/fileTransfer.js
@@ -19,29 +19,186 @@
*
*/
-var cordova = require('cordova');
+var cordova = require('cordova'),
+ FileEntry = require('cordova/plugin/FileEntry'),
+ FileTransferError = require('cordova/plugin/FileTransferError'),
+ FileUploadResult = require('cordova/plugin/FileUploadResult'),
+ ProgressEvent = require('cordova/plugin/ProgressEvent'),
+ nativeResolveLocalFileSystemURI = function(uri, success, fail) {
+ if (uri.substring(0,11) !== "filesystem:") {
+ uri = "filesystem:" + uri;
+ }
+ window.webkitResolveLocalFileSystemURL(uri, success, fail);
+ },
+ xhr;
-module.exports = {
- download: function (args, win, fail) {
- var source = args[0],
- target = args[1];
+function getParentPath(filePath) {
+ var pos = filePath.lastIndexOf('/');
+ return filePath.substring(0, pos + 1);
+}
+
+function getFileName(filePath) {
+ var pos = filePath.lastIndexOf('/');
+ return filePath.substring(pos + 1);
+}
+
+function cleanUpPath(filePath) {
+ var pos = filePath.lastIndexOf('/');
+ return filePath.substring(0, pos) + filePath.substring(pos + 1, filePath.length);
+}
+
+function checkURL(url) {
+ return url.indexOf(' ') === -1 ? true : false;
+}
- blackberry.io.filetransfer.download(source, target, win, fail);
+module.exports = {
+ abort: function () {
return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "async"};
},
- upload: function (args, win, fail) {
- var path = args[0],
+ upload: function(args, win, fail) {
+ var filePath = args[0],
server = args[1],
- options = {
- fileKey: args[2],
- fileName: args[3],
- mimeType: args[4],
- params: args[5],
- chunkedMode: args[6]
- };
-
- blackberry.io.filetransfer.upload(path, server, win, fail, options);
+ fileKey = args[2],
+ fileName = args[3],
+ mimeType = args[4],
+ params = args[5],
+ /*trustAllHosts = args[6],*/
+ chunkedMode = args[7],
+ headers = args[8];
+
+ if (!checkURL(server)) {
+ fail(new FileTransferError(FileTransferError.INVALID_URL_ERR));
+ }
+
+ nativeResolveLocalFileSystemURI(filePath, function(entry) {
+ entry.file(function(file) {
+ function uploadFile(blobFile) {
+ var fd = new FormData();
+
+ fd.append(fileKey, blobFile, fileName);
+ for (var prop in params) {
+ if(params.hasOwnProperty(prop)) {
+ fd.append(prop, params[prop]);
+ }
+ }
+
+ xhr = new XMLHttpRequest();
+ xhr.open("POST", server);
+ xhr.onload = function(evt) {
+ if (xhr.status == 200) {
+ var result = new FileUploadResult();
+ result.bytesSent = file.size;
+ result.responseCode = xhr.status;
+ result.response = xhr.response;
+ win(result);
+ } else if (xhr.status == 404) {
+ fail(new FileTransferError(FileTransferError.INVALID_URL_ERR, server, filePath, xhr.status));
+ } else {
+ fail(new FileTransferError(FileTransferError.CONNECTION_ERR, server, filePath, xhr.status));
+ }
+ };
+ xhr.ontimeout = function(evt) {
+ fail(new FileTransferError(FileTransferError.CONNECTION_ERR, server, filePath, xhr.status));
+ };
+ xhr.onerror = function () {
+ fail(new FileTransferError(FileTransferError.CONNECTION_ERR, server, filePath, this.status));
+ };
+ xhr.onprogress = function (evt) {
+ win(evt);
+ };
+
+ for (var header in headers) {
+ if (headers.hasOwnProperty(header)) {
+ xhr.setRequestHeader(header, headers[header]);
+ }
+ }
+
+ xhr.send(fd);
+ }
+
+ var bytesPerChunk;
+ if (chunkedMode === true) {
+ bytesPerChunk = 1024 * 1024; // 1MB chunk sizes.
+ } else {
+ bytesPerChunk = file.size;
+ }
+ var start = 0;
+ var end = bytesPerChunk;
+ while (start < file.size) {
+ var chunk = file.slice(start, end, mimeType);
+ uploadFile(chunk);
+ start = end;
+ end = start + bytesPerChunk;
+ }
+ }, function(error) {
+ fail(new FileTransferError(FileTransferError.FILE_NOT_FOUND_ERR));
+ });
+ }, function(error) {
+ fail(new FileTransferError(FileTransferError.FILE_NOT_FOUND_ERR));
+ });
+
+ return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "async"};
+ },
+
+ download: function (args, win, fail) {
+ var source = args[0],
+ target = cleanUpPath(args[1]),
+ fileWriter;
+
+ if (!checkURL(source)) {
+ fail(new FileTransferError(FileTransferError.INVALID_URL_ERR));
+ }
+
+ xhr = new XMLHttpRequest();
+
+ function writeFile(entry) {
+ entry.createWriter(function (writer) {
+ fileWriter = writer;
+ fileWriter.onwriteend = function (evt) {
+ if (!evt.target.error) {
+ win(new FileEntry(entry.name, entry.toURL()));
+ } else {
+ fail(evt.target.error);
+ }
+ };
+ fileWriter.onerror = function (evt) {
+ fail(evt.target.error);
+ };
+ fileWriter.write(new Blob([xhr.response]));
+ }, function (error) {
+ fail(error);
+ });
+ }
+
+ xhr.onerror = function (e) {
+ fail(new FileTransferError(FileTransferError.CONNECTION_ERR, source, target, xhr.status));
+ };
+
+ xhr.onload = function () {
+ if (xhr.readyState === xhr.DONE) {
+ if (xhr.status === 200 && xhr.response) {
+ nativeResolveLocalFileSystemURI(getParentPath(target), function (dir) {
+ dir.getFile(getFileName(target), {create: true}, writeFile, function (error) {
+ fail(new FileTransferError(FileTransferError.FILE_NOT_FOUND_ERR));
+ });
+ }, function (error) {
+ fail(new FileTransferError(FileTransferError.FILE_NOT_FOUND_ERR));
+ });
+ } else if (xhr.status === 404) {
+ fail(new FileTransferError(FileTransferError.INVALID_URL_ERR, source, target, xhr.status));
+ } else {
+ fail(new FileTransferError(FileTransferError.CONNECTION_ERR, source, target, xhr.status));
+ }
+ }
+ };
+ xhr.onprogress = function (evt) {
+ win(evt);
+ };
+
+ xhr.responseType = "blob";
+ xhr.open("GET", source, true);
+ xhr.send();
return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "async"};
}
};
View
32 test/blackberry/qnx/test.fileTransfer.js
@@ -20,10 +20,12 @@
*/
describe("blackberry qnx fileTransfer", function () {
- var fileTransfer = require('cordova/plugin/qnx/fileTransfer'),
- cordova = require('cordova'),
+ var fileTransfer = require('cordova/plugin/qnx/fileTransfer');
+ var cordova = require('cordova'),
win = jasmine.createSpy('win'),
- fail = jasmine.createSpy('fail');
+ fail = jasmine.createSpy('fail')
+ xhrSend = jasmine.createSpy('xhr send');
+ xhrOpen = jasmine.createSpy('xhr open');
beforeEach(function () {
global.blackberry = {
@@ -33,17 +35,29 @@ describe("blackberry qnx fileTransfer", function () {
upload: jasmine.createSpy('upload')
}
}
- }
+ };
+ XMLHttpRequest = function () {
+ var xhr = {
+ send: xhrSend,
+ open: xhrOpen
+ };
+ return xhr;
+ };
+ window.webkitResolveLocalFileSystemURL = jasmine.createSpy("resolveFS")
});
afterEach(function () {
delete global.blackberry;
+ delete XMLHttpRequest;
+ delete webkitResolveLocalFileSystemURL;
+ delete window.webkitResolveLocalFileSystemURL;
});
describe("download", function(){
- it('should call the blackberry download', function(){
+ it('should call the blackberry download', function () {
fileTransfer.download(["source/file", "target/file"], win, fail);
- expect(blackberry.io.filetransfer.download).toHaveBeenCalledWith("source/file", "target/file", win, fail);
+ expect(xhrOpen).toHaveBeenCalled();
+ expect(xhrSend).toHaveBeenCalled();
});
it('should return No Result', function(){
@@ -54,11 +68,11 @@ describe("blackberry qnx fileTransfer", function () {
});
});
- describe('uplaod', function(){
+ describe('upload', function(){
it('should call the blackberry upload', function(){
fileTransfer.upload(["source", "target", "fileKey", "fileName", "mimeType", "params", "chunkedMode"], win, fail);
-
- expect(blackberry.io.filetransfer.upload).toHaveBeenCalledWith("source", "target", win, fail, {fileKey: "fileKey", fileName: "fileName", mimeType: "mimeType", params: "params", chunkedMode: "chunkedMode"});
+ expect(xhrOpen).toHaveBeenCalled();
+ expect(xhrSend).toHaveBeenCalled();
});
it('should return No Result', function(){
Please sign in to comment.
Something went wrong with that request. Please try again.