mirror of
https://github.com/apache/cordova-plugin-file-transfer.git
synced 2026-04-28 00:02:49 +08:00
[CB-3732] blackberry10 support
Reviewed by Bryan Higgins <bhiggins@blackberry.com> Tested by Tracy Li <tli@blackberry.com>
This commit is contained in:
committed by
Bryan Higgins
parent
fd6ef121ae
commit
d39016ecb2
+10
@@ -32,6 +32,16 @@
|
|||||||
<source-file src="src/android/FileUploadResult.java" target-dir="src/org/apache/cordova/core" />
|
<source-file src="src/android/FileUploadResult.java" target-dir="src/org/apache/cordova/core" />
|
||||||
</platform>
|
</platform>
|
||||||
|
|
||||||
|
<platform name="blackberry10">
|
||||||
|
<config-file target="www/config.xml" parent="/widget">
|
||||||
|
<feature name="FileTransfer" value="FileTransfer"></feature>
|
||||||
|
</config-file>
|
||||||
|
<js-module src="www/blackberry10/FileTransfer.js" name="BB10FileTransfer">
|
||||||
|
<clobbers target="window.FileTransfer"></clobbers>
|
||||||
|
</js-module>
|
||||||
|
<js-module src="www/blackberry10/XHRImplementation.js" name="BB10XHRImplementation"></js-module>
|
||||||
|
</platform>
|
||||||
|
|
||||||
<!-- ios -->
|
<!-- ios -->
|
||||||
<platform name="ios">
|
<platform name="ios">
|
||||||
<config-file target="config.xml" parent="/*">
|
<config-file target="config.xml" parent="/*">
|
||||||
|
|||||||
+1
-1
@@ -21,7 +21,7 @@
|
|||||||
|
|
||||||
var argscheck = require('cordova/argscheck'),
|
var argscheck = require('cordova/argscheck'),
|
||||||
exec = require('cordova/exec'),
|
exec = require('cordova/exec'),
|
||||||
FileTransferError = require('./FileTransferError'),
|
FileTransferError = require('org.apache.cordova.core.FileTransfer.FileTransferError'),
|
||||||
ProgressEvent = require('org.apache.cordova.core.file.ProgressEvent');
|
ProgressEvent = require('org.apache.cordova.core.file.ProgressEvent');
|
||||||
|
|
||||||
function newProgressEvent(result) {
|
function newProgressEvent(result) {
|
||||||
|
|||||||
@@ -0,0 +1,188 @@
|
|||||||
|
/*
|
||||||
|
*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one
|
||||||
|
* or more contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. The ASF licenses this file
|
||||||
|
* to you under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
var argscheck = require('cordova/argscheck'),
|
||||||
|
FileTransferError = require('org.apache.cordova.core.FileTransfer.FileTransferError');
|
||||||
|
xhrImpl = require('org.apache.cordova.core.FileTransfer.BB10XHRImplementation');
|
||||||
|
|
||||||
|
|
||||||
|
function getBasicAuthHeader(urlString) {
|
||||||
|
var header = null;
|
||||||
|
|
||||||
|
if (window.btoa) {
|
||||||
|
// parse the url using the Location object
|
||||||
|
var url = document.createElement('a');
|
||||||
|
url.href = urlString;
|
||||||
|
|
||||||
|
var credentials = null;
|
||||||
|
var protocol = url.protocol + "//";
|
||||||
|
var origin = protocol + url.host;
|
||||||
|
|
||||||
|
// check whether there are the username:password credentials in the url
|
||||||
|
if (url.href.indexOf(origin) !== 0) { // credentials found
|
||||||
|
var atIndex = url.href.indexOf("@");
|
||||||
|
credentials = url.href.substring(protocol.length, atIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (credentials) {
|
||||||
|
var authHeader = "Authorization";
|
||||||
|
var authHeaderValue = "Basic " + window.btoa(credentials);
|
||||||
|
|
||||||
|
header = {
|
||||||
|
name : authHeader,
|
||||||
|
value : authHeaderValue
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return header;
|
||||||
|
}
|
||||||
|
|
||||||
|
var idCounter = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* FileTransfer uploads a file to a remote server.
|
||||||
|
* @constructor
|
||||||
|
*/
|
||||||
|
var FileTransfer = function() {
|
||||||
|
this._id = ++idCounter;
|
||||||
|
this.onprogress = null; // optional callback
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Given an absolute file path, uploads a file on the device to a remote server
|
||||||
|
* using a multipart HTTP request.
|
||||||
|
* @param filePath {String} Full path of the file on the device
|
||||||
|
* @param server {String} URL of the server to receive the file
|
||||||
|
* @param successCallback (Function} Callback to be invoked when upload has completed
|
||||||
|
* @param errorCallback {Function} Callback to be invoked upon error
|
||||||
|
* @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) {
|
||||||
|
argscheck.checkArgs('ssFFO*', 'FileTransfer.upload', arguments);
|
||||||
|
// check for options
|
||||||
|
var fileKey = null;
|
||||||
|
var fileName = null;
|
||||||
|
var mimeType = null;
|
||||||
|
var params = null;
|
||||||
|
var chunkedMode = true;
|
||||||
|
var headers = null;
|
||||||
|
var httpMethod = null;
|
||||||
|
var basicAuthHeader = getBasicAuthHeader(server);
|
||||||
|
if (basicAuthHeader) {
|
||||||
|
options = options || {};
|
||||||
|
options.headers = options.headers || {};
|
||||||
|
options.headers[basicAuthHeader.name] = basicAuthHeader.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options) {
|
||||||
|
fileKey = options.fileKey;
|
||||||
|
fileName = options.fileName;
|
||||||
|
mimeType = options.mimeType;
|
||||||
|
headers = options.headers;
|
||||||
|
httpMethod = options.httpMethod || "POST";
|
||||||
|
if (httpMethod.toUpperCase() == "PUT"){
|
||||||
|
httpMethod = "PUT";
|
||||||
|
} else {
|
||||||
|
httpMethod = "POST";
|
||||||
|
}
|
||||||
|
if (options.chunkedMode !== null || typeof options.chunkedMode != "undefined") {
|
||||||
|
chunkedMode = options.chunkedMode;
|
||||||
|
}
|
||||||
|
if (options.params) {
|
||||||
|
params = options.params;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
params = {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var fail = errorCallback && function(e) {
|
||||||
|
var error = new FileTransferError(e.code, e.source, e.target, e.http_status, e.body);
|
||||||
|
errorCallback(error);
|
||||||
|
};
|
||||||
|
|
||||||
|
var self = this;
|
||||||
|
var win = function(result) {
|
||||||
|
if (typeof result.lengthComputable != "undefined") {
|
||||||
|
if (self.onprogress) {
|
||||||
|
self.onprogress(result);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
successCallback && successCallback(result);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
xhrImpl.upload(win, fail, [filePath, server, fileKey, fileName, mimeType, params, trustAllHosts, chunkedMode, headers, this._id, httpMethod]);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Downloads a file form a given URL and saves it to the specified directory.
|
||||||
|
* @param source {String} URL of the server to receive the file
|
||||||
|
* @param target {String} Full path of the file on the device
|
||||||
|
* @param successCallback (Function} Callback to be invoked when upload has completed
|
||||||
|
* @param errorCallback {Function} Callback to be invoked upon error
|
||||||
|
* @param trustAllHosts {Boolean} Optional trust all hosts (e.g. for self-signed certs), defaults to false
|
||||||
|
* @param options {FileDownloadOptions} Optional parameters such as headers
|
||||||
|
*/
|
||||||
|
FileTransfer.prototype.download = function(source, target, successCallback, errorCallback, trustAllHosts, options) {
|
||||||
|
argscheck.checkArgs('ssFF*', 'FileTransfer.download', arguments);
|
||||||
|
var self = this;
|
||||||
|
|
||||||
|
var basicAuthHeader = getBasicAuthHeader(source);
|
||||||
|
if (basicAuthHeader) {
|
||||||
|
options = options || {};
|
||||||
|
options.headers = options.headers || {};
|
||||||
|
options.headers[basicAuthHeader.name] = basicAuthHeader.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
var headers = null;
|
||||||
|
if (options) {
|
||||||
|
headers = options.headers || null;
|
||||||
|
}
|
||||||
|
|
||||||
|
var win = function(result) {
|
||||||
|
if (typeof result.lengthComputable != "undefined") {
|
||||||
|
if (self.onprogress) {
|
||||||
|
return self.onprogress(result);
|
||||||
|
}
|
||||||
|
} else if (successCallback) {
|
||||||
|
successCallback(result);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var fail = errorCallback && function(e) {
|
||||||
|
var error = new FileTransferError(e.code, e.source, e.target, e.http_status, e.body);
|
||||||
|
errorCallback(error);
|
||||||
|
};
|
||||||
|
|
||||||
|
xhrImpl.download(win, fail, [source, target, trustAllHosts, this._id, headers]);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Aborts the ongoing file transfer on this object. The original error
|
||||||
|
* callback for the file transfer will be called if necessary.
|
||||||
|
*/
|
||||||
|
FileTransfer.prototype.abort = function() {
|
||||||
|
xhrImpl.abort(null, null, [this._id]);
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = FileTransfer;
|
||||||
@@ -0,0 +1,201 @@
|
|||||||
|
/*
|
||||||
|
*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one
|
||||||
|
* or more contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. The ASF licenses this file
|
||||||
|
* to you under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*global Blob:false */
|
||||||
|
var cordova = require('cordova'),
|
||||||
|
nativeResolveLocalFileSystemURI = function(uri, success, fail) {
|
||||||
|
if (uri.substring(0,11) !== "filesystem:") {
|
||||||
|
uri = "filesystem:" + uri;
|
||||||
|
}
|
||||||
|
resolveLocalFileSystemURI(uri, success, fail);
|
||||||
|
},
|
||||||
|
xhr;
|
||||||
|
|
||||||
|
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 checkURL(url) {
|
||||||
|
return url.indexOf(' ') === -1 ? true : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
abort: function () {
|
||||||
|
return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "async"};
|
||||||
|
},
|
||||||
|
|
||||||
|
upload: function(win, fail, args) {
|
||||||
|
var filePath = args[0],
|
||||||
|
server = args[1],
|
||||||
|
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, server, filePath));
|
||||||
|
}
|
||||||
|
|
||||||
|
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, xhr.response));
|
||||||
|
} else {
|
||||||
|
fail(new FileTransferError(FileTransferError.CONNECTION_ERR, server, filePath, xhr.status, xhr.response));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
xhr.ontimeout = function(evt) {
|
||||||
|
fail(new FileTransferError(FileTransferError.CONNECTION_ERR, server, filePath, xhr.status, xhr.response));
|
||||||
|
};
|
||||||
|
xhr.onerror = function () {
|
||||||
|
fail(new FileTransferError(FileTransferError.CONNECTION_ERR, server, filePath, this.status, xhr.response));
|
||||||
|
};
|
||||||
|
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, server, filePath));
|
||||||
|
});
|
||||||
|
}, function(error) {
|
||||||
|
fail(new FileTransferError(FileTransferError.FILE_NOT_FOUND_ERR, server, filePath));
|
||||||
|
});
|
||||||
|
|
||||||
|
return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "async"};
|
||||||
|
},
|
||||||
|
|
||||||
|
download: function (win, fail, args) {
|
||||||
|
var source = args[0],
|
||||||
|
target = args[1],
|
||||||
|
headers = args[4],
|
||||||
|
fileWriter;
|
||||||
|
|
||||||
|
if (!checkURL(source)) {
|
||||||
|
fail(new FileTransferError(FileTransferError.INVALID_URL_ERR, source, target));
|
||||||
|
}
|
||||||
|
|
||||||
|
xhr = new XMLHttpRequest();
|
||||||
|
|
||||||
|
function writeFile(entry) {
|
||||||
|
entry.createWriter(function (writer) {
|
||||||
|
fileWriter = writer;
|
||||||
|
fileWriter.onwriteend = function (evt) {
|
||||||
|
if (!evt.target.error) {
|
||||||
|
win(entry);
|
||||||
|
} 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.response));
|
||||||
|
};
|
||||||
|
|
||||||
|
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, source, target, xhr.status, xhr.response));
|
||||||
|
});
|
||||||
|
}, function (error) {
|
||||||
|
fail(new FileTransferError(FileTransferError.FILE_NOT_FOUND_ERR, source, target, xhr.status, xhr.response));
|
||||||
|
});
|
||||||
|
} else if (xhr.status === 404) {
|
||||||
|
fail(new FileTransferError(FileTransferError.INVALID_URL_ERR, source, target, xhr.status, xhr.response));
|
||||||
|
} else {
|
||||||
|
fail(new FileTransferError(FileTransferError.CONNECTION_ERR, source, target, xhr.status, xhr.response));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
xhr.onprogress = function (evt) {
|
||||||
|
win(evt);
|
||||||
|
};
|
||||||
|
|
||||||
|
xhr.open("GET", source, true);
|
||||||
|
for (var header in headers) {
|
||||||
|
if (headers.hasOwnProperty(header)) {
|
||||||
|
xhr.setRequestHeader(header, headers[header]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
xhr.send();
|
||||||
|
return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "async"};
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user