Cordova plugin file und file-transfer geadded

This commit is contained in:
2016-01-08 00:06:37 +01:00
parent 23a833a31b
commit 36c7e2c8e2
234 changed files with 49402 additions and 81 deletions

View File

@@ -0,0 +1,235 @@
/*
*
* 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'),
exec = require('cordova/exec'),
FileTransferError = require('./FileTransferError'),
ProgressEvent = require('cordova-plugin-file.ProgressEvent');
function newProgressEvent(result) {
var pe = new ProgressEvent();
pe.lengthComputable = result.lengthComputable;
pe.loaded = result.loaded;
pe.total = result.total;
return pe;
}
function getUrlCredentials(urlString) {
var credentialsPattern = /^https?\:\/\/(?:(?:(([^:@\/]*)(?::([^@\/]*))?)?@)?([^:\/?#]*)(?::(\d*))?).*$/,
credentials = credentialsPattern.exec(urlString);
return credentials && credentials[1];
}
function getBasicAuthHeader(urlString) {
var header = null;
// This is changed due to MS Windows doesn't support credentials in http uris
// so we detect them by regexp and strip off from result url
// Proof: http://social.msdn.microsoft.com/Forums/windowsapps/en-US/a327cf3c-f033-4a54-8b7f-03c56ba3203f/windows-foundation-uri-security-problem
if (window.btoa) {
var credentials = getUrlCredentials(urlString);
if (credentials) {
var authHeader = "Authorization";
var authHeaderValue = "Basic " + window.btoa(credentials);
header = {
name : authHeader,
value : authHeaderValue
};
}
}
return header;
}
function convertHeadersToArray(headers) {
var result = [];
for (var header in headers) {
if (headers.hasOwnProperty(header)) {
var headerValue = headers[header];
result.push({
name: header,
value: headerValue.toString()
});
}
}
return result;
}
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) {
server = server.replace(getUrlCredentials(server) + '@', '');
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 = {};
}
}
if (cordova.platformId === "windowsphone") {
headers = headers && convertHeadersToArray(headers);
params = params && convertHeadersToArray(params);
}
var fail = errorCallback && function(e) {
var error = new FileTransferError(e.code, e.source, e.target, e.http_status, e.body, e.exception);
errorCallback(error);
};
var self = this;
var win = function(result) {
if (typeof result.lengthComputable != "undefined") {
if (self.onprogress) {
self.onprogress(newProgressEvent(result));
}
} else {
successCallback && successCallback(result);
}
};
exec(win, fail, 'FileTransfer', 'upload', [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) {
source = source.replace(getUrlCredentials(source) + '@', '');
options = options || {};
options.headers = options.headers || {};
options.headers[basicAuthHeader.name] = basicAuthHeader.value;
}
var headers = null;
if (options) {
headers = options.headers || null;
}
if (cordova.platformId === "windowsphone" && headers) {
headers = convertHeadersToArray(headers);
}
var win = function(result) {
if (typeof result.lengthComputable != "undefined") {
if (self.onprogress) {
return self.onprogress(newProgressEvent(result));
}
} else if (successCallback) {
var entry = null;
if (result.isDirectory) {
entry = new (require('cordova-plugin-file.DirectoryEntry'))();
}
else if (result.isFile) {
entry = new (require('cordova-plugin-file.FileEntry'))();
}
entry.isDirectory = result.isDirectory;
entry.isFile = result.isFile;
entry.name = result.name;
entry.fullPath = result.fullPath;
entry.filesystem = new FileSystem(result.filesystemName || (result.filesystem == window.PERSISTENT ? 'persistent' : 'temporary'));
entry.nativeURL = result.nativeURL;
successCallback(entry);
}
};
var fail = errorCallback && function(e) {
var error = new FileTransferError(e.code, e.source, e.target, e.http_status, e.body, e.exception);
errorCallback(error);
};
exec(win, fail, 'FileTransfer', 'download', [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() {
exec(null, null, 'FileTransfer', 'abort', [this._id]);
};
module.exports = FileTransfer;

View File

@@ -0,0 +1,41 @@
/*
*
* 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.
*
*/
/**
* FileTransferError
* @constructor
*/
var FileTransferError = function(code, source, target, status, body, exception) {
this.code = code || null;
this.source = source || null;
this.target = target || null;
this.http_status = status || null;
this.body = body || null;
this.exception = exception || null;
};
FileTransferError.FILE_NOT_FOUND_ERR = 1;
FileTransferError.INVALID_URL_ERR = 2;
FileTransferError.CONNECTION_ERR = 3;
FileTransferError.ABORT_ERR = 4;
FileTransferError.NOT_MODIFIED_ERR = 5;
module.exports = FileTransferError;

View File

@@ -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('./FileTransferError');
xhrImpl = require('./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;

View File

@@ -0,0 +1,36 @@
/*
*
* 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.
*
*/
/*
* FileTransferProxy
*
* Register all FileTransfer exec calls to be handled by proxy
*/
var xhrFileTransfer = require('cordova-plugin-file-transfer.xhrFileTransfer');
module.exports = {
abort: xhrFileTransfer.abort,
download: xhrFileTransfer.download,
upload: xhrFileTransfer.upload
};
require('cordova/exec/proxy').add('FileTransfer', module.exports);

View File

@@ -0,0 +1,259 @@
/*
*
* 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'),
resolve = cordova.require('cordova-plugin-file.resolveLocalFileSystemURIProxy'),
requestAnimationFrame = cordova.require('cordova-plugin-file.bb10RequestAnimationFrame'),
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 (win, fail, args) {
var id = args[0];
if (xhr[id]) {
xhr[id].abort();
if (typeof(win) === 'function') {
win();
}
} else if (typeof(fail) === 'function') {
fail();
}
},
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],
onSuccess = function (data) {
if (typeof(win) === 'function') {
win(data);
}
},
onFail = function (code) {
delete xhr[fileKey];
if (typeof(fail) === 'function') {
fail(code);
}
};
if (!checkURL(server)) {
onFail(new FileTransferError(FileTransferError.INVALID_URL_ERR, server, filePath));
}
xhr[fileKey] = new XMLHttpRequest();
xhr[fileKey].onabort = function () {
onFail(new FileTransferError(FileTransferError.ABORT_ERR, server, filePath, this.status, xhr[fileKey].response));
};
resolve(function(entry) {
requestAnimationFrame(function () {
entry.nativeEntry.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[fileKey].open("POST", server);
xhr[fileKey].onload = function(evt) {
if (xhr[fileKey].status === 200) {
var result = new FileUploadResult();
result.bytesSent = file.size;
result.responseCode = xhr[fileKey].status;
result.response = xhr[fileKey].response;
delete xhr[fileKey];
onSuccess(result);
} else if (xhr[fileKey].status === 404) {
onFail(new FileTransferError(FileTransferError.INVALID_URL_ERR, server, filePath, xhr[fileKey].status, xhr[fileKey].response));
} else {
onFail(new FileTransferError(FileTransferError.CONNECTION_ERR, server, filePath, xhr[fileKey].status, xhr[fileKey].response));
}
};
xhr[fileKey].ontimeout = function(evt) {
onFail(new FileTransferError(FileTransferError.CONNECTION_ERR, server, filePath, xhr[fileKey].status, xhr[fileKey].response));
};
xhr[fileKey].onerror = function () {
onFail(new FileTransferError(FileTransferError.CONNECTION_ERR, server, filePath, this.status, xhr[fileKey].response));
};
xhr[fileKey].upload.onprogress = function (evt) {
if (evt.loaded > 0) {
onSuccess(evt);
}
};
for (var header in headers) {
if (headers.hasOwnProperty(header)) {
xhr[fileKey].setRequestHeader(header, headers[header]);
}
}
requestAnimationFrame(function () {
xhr[fileKey].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) {
onFail(new FileTransferError(FileTransferError.FILE_NOT_FOUND_ERR, server, filePath));
});
});
}, function(error) {
onFail(new FileTransferError(FileTransferError.FILE_NOT_FOUND_ERR, server, filePath));
}, [filePath]);
},
download: function (win, fail, args) {
var source = args[0],
target = args[1],
id = args[3],
headers = args[4],
fileWriter,
onSuccess = function (entry) {
if (typeof(win) === 'function') {
win(entry);
}
},
onFail = function (error) {
var reader;
delete xhr[id];
if (typeof(fail) === 'function') {
if (error && error.body && typeof(error.body) === 'object') {
reader = new FileReader()._realReader;
reader.onloadend = function () {
error.body = this.result;
fail(error);
};
reader.onerror = function () {
fail(error);
};
reader.readAsText(error.body);
} else {
fail(error);
}
}
};
if (!checkURL(source)) {
onFail(new FileTransferError(FileTransferError.INVALID_URL_ERR, source, target));
}
xhr[id] = new XMLHttpRequest();
function writeFile(entry) {
entry.createWriter(function (writer) {
fileWriter = writer;
fileWriter.onwriteend = function (evt) {
if (!evt.target.error) {
entry.filesystemName = entry.filesystem.name;
delete xhr[id];
onSuccess(entry);
} else {
onFail(evt.target.error);
}
};
fileWriter.onerror = function (evt) {
onFail(evt.target.error);
};
fileWriter.write(new Blob([xhr[id].response]));
}, function (error) {
onFail(error);
});
}
xhr[id].onerror = function (e) {
onFail(new FileTransferError(FileTransferError.CONNECTION_ERR, source, target, xhr[id].status, xhr[id].response));
};
xhr[id].onabort = function (e) {
onFail(new FileTransferError(FileTransferError.ABORT_ERR, source, target, xhr[id].status, xhr[id].response));
}
xhr[id].onload = function () {
if (xhr[id].readyState === xhr[id].DONE) {
if (xhr[id].status === 200 && xhr[id].response) {
resolveLocalFileSystemURI(getParentPath(target), function (dir) {
dir.getFile(getFileName(target), {create: true}, writeFile, function (error) {
onFail(new FileTransferError(FileTransferError.FILE_NOT_FOUND_ERR, source, target, xhr[id].status, xhr[id].response));
});
}, function (error) {
onFail(new FileTransferError(FileTransferError.FILE_NOT_FOUND_ERR, source, target, xhr[id].status, xhr[id].response));
});
} else if (xhr[id].status === 404) {
onFail(new FileTransferError(FileTransferError.INVALID_URL_ERR, source, target, xhr[id].status, xhr[id].response));
} else {
onFail(new FileTransferError(FileTransferError.CONNECTION_ERR, source, target, xhr[id].status, xhr[id].response));
}
}
};
xhr[id].onprogress = function (evt) {
onSuccess(evt);
};
xhr[id].open("GET", source, true);
for (var header in headers) {
if (headers.hasOwnProperty(header)) {
xhr[id].setRequestHeader(header, headers[header]);
}
}
xhr[id].responseType = "blob";
requestAnimationFrame(function () {
if (xhr[id]) {
xhr[id].send();
}
});
}
};

View File

@@ -0,0 +1,328 @@
/*
*
* 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 module, require*/
var argscheck = require('cordova/argscheck'),
FileTransferError = require('./FileTransferError');
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 getUrlCredentials(urlString) {
var credentialsPattern = /^https?\:\/\/(?:(?:(([^:@\/]*)(?::([^@\/]*))?)?@)?([^:\/?#]*)(?::(\d*))?).*$/,
credentials = credentialsPattern.exec(urlString);
return credentials && credentials[1];
}
function getBasicAuthHeader(urlString) {
var header = null;
// This is changed due to MS Windows doesn't support credentials in http uris
// so we detect them by regexp and strip off from result url
// Proof: http://social.msdn.microsoft.com/Forums/windowsapps/en-US/a327cf3c-f033-4a54-8b7f-03c56ba3203f/windows-foundation-uri-security-problem
if (window.btoa) {
var credentials = getUrlCredentials(urlString);
if (credentials) {
var authHeader = "Authorization";
var authHeaderValue = "Basic " + window.btoa(credentials);
header = {
name : authHeader,
value : authHeaderValue
};
}
}
return header;
}
function checkURL(url) {
return url.indexOf(' ') === -1 ? true : false;
}
var idCounter = 0;
var transfers = {};
/**
* 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) {
// check for arguments
argscheck.checkArgs('ssFFO*', 'FileTransfer.upload', arguments);
// Check if target URL doesn't contain spaces. If contains, it should be escaped first
// (see https://github.com/apache/cordova-plugin-file-transfer/blob/master/doc/index.md#upload)
if (!checkURL(server)) {
errorCallback && errorCallback(new FileTransferError(FileTransferError.INVALID_URL_ERR, filePath, server));
return;
}
options = options || {};
var fileKey = options.fileKey || "file";
var fileName = options.fileName || "image.jpg";
var mimeType = options.mimeType || "image/jpeg";
var params = options.params || {};
var withCredentials = options.withCredentials || false;
// var chunkedMode = !!options.chunkedMode; // Not supported
var headers = options.headers || {};
var httpMethod = options.httpMethod && options.httpMethod.toUpperCase() === "PUT" ? "PUT" : "POST";
var basicAuthHeader = getBasicAuthHeader(server);
if (basicAuthHeader) {
server = server.replace(getUrlCredentials(server) + '@', '');
headers[basicAuthHeader.name] = basicAuthHeader.value;
}
var that = this;
var xhr = transfers[this._id] = new XMLHttpRequest();
xhr.withCredentials = withCredentials;
var fail = errorCallback && function(code, status, response) {
transfers[this._id] && delete transfers[this._id];
var error = new FileTransferError(code, filePath, server, status, response);
errorCallback && errorCallback(error);
};
window.resolveLocalFileSystemURL(filePath, function(entry) {
entry.file(function(file) {
var reader = new FileReader();
reader.onloadend = function() {
var blob = new Blob([this.result], {type: mimeType});
// Prepare form data to send to server
var fd = new FormData();
fd.append(fileKey, blob, fileName);
for (var prop in params) {
if (params.hasOwnProperty(prop)) {
fd.append(prop, params[prop]);
}
}
xhr.open(httpMethod, server);
// Fill XHR headers
for (var header in headers) {
if (headers.hasOwnProperty(header)) {
xhr.setRequestHeader(header, headers[header]);
}
}
xhr.onload = function() {
if (this.status === 200) {
var result = new FileUploadResult(); // jshint ignore:line
result.bytesSent = blob.size;
result.responseCode = this.status;
result.response = this.response;
delete transfers[that._id];
successCallback(result);
} else if (this.status === 404) {
fail(FileTransferError.INVALID_URL_ERR, this.status, this.response);
} else {
fail(FileTransferError.CONNECTION_ERR, this.status, this.response);
}
};
xhr.ontimeout = function() {
fail(FileTransferError.CONNECTION_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) {
that.onprogress && that.onprogress(e);
};
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 (!transfers[that._id]) {
xhr.abort();
}
};
reader.readAsArrayBuffer(file);
}, function() {
fail(FileTransferError.FILE_NOT_FOUND_ERR);
});
}, function() {
fail(FileTransferError.FILE_NOT_FOUND_ERR);
});
};
/**
* 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);
// Check if target URL doesn't contain spaces. If contains, it should be escaped first
// (see https://github.com/apache/cordova-plugin-file-transfer/blob/master/doc/index.md#download)
if (!checkURL(source)) {
errorCallback && errorCallback(new FileTransferError(FileTransferError.INVALID_URL_ERR, source, target));
return;
}
options = options || {};
var headers = options.headers || {};
var withCredentials = options.withCredentials || false;
var basicAuthHeader = getBasicAuthHeader(source);
if (basicAuthHeader) {
source = source.replace(getUrlCredentials(source) + '@', '');
headers[basicAuthHeader.name] = basicAuthHeader.value;
}
var that = this;
var xhr = transfers[this._id] = new XMLHttpRequest();
xhr.withCredentials = withCredentials;
var fail = errorCallback && function(code, status, response) {
transfers[that._id] && delete transfers[that._id];
// In XHR GET reqests we're setting response type to Blob
// but in case of error we need to raise event with plain text response
if (response instanceof Blob) {
var reader = new FileReader();
reader.readAsText(response);
reader.onloadend = function(e) {
var error = new FileTransferError(code, source, target, status, e.target.result);
errorCallback(error);
};
} else {
var error = new FileTransferError(code, source, target, status, response);
errorCallback(error);
}
};
xhr.onload = function (e) {
var fileNotFound = function () {
fail(FileTransferError.FILE_NOT_FOUND_ERR);
};
var req = e.target;
// req.status === 0 is special case for local files with file:// URI scheme
if ((req.status === 200 || req.status === 0) && req.response) {
window.resolveLocalFileSystemURL(getParentPath(target), function (dir) {
dir.getFile(getFileName(target), {create: true}, function writeFile(entry) {
entry.createWriter(function (fileWriter) {
fileWriter.onwriteend = function (evt) {
if (!evt.target.error) {
entry.filesystemName = entry.filesystem.name;
delete transfers[that._id];
successCallback && successCallback(entry);
} else {
fail(FileTransferError.FILE_NOT_FOUND_ERR);
}
};
fileWriter.onerror = function () {
fail(FileTransferError.FILE_NOT_FOUND_ERR);
};
fileWriter.write(req.response);
}, fileNotFound);
}, fileNotFound);
}, fileNotFound);
} else if (req.status === 404) {
fail(FileTransferError.INVALID_URL_ERR, req.status, req.response);
} else {
fail(FileTransferError.CONNECTION_ERR, req.status, req.response);
}
};
xhr.onprogress = function (e) {
that.onprogress && that.onprogress(e);
};
xhr.onerror = function () {
fail(FileTransferError.CONNECTION_ERR, this.status, this.response);
};
xhr.onabort = function () {
fail(FileTransferError.ABORT_ERR, this.status, this.response);
};
xhr.open("GET", source, true);
for (var header in headers) {
if (headers.hasOwnProperty(header)) {
xhr.setRequestHeader(header, headers[header]);
}
}
xhr.responseType = "blob";
xhr.send();
};
/**
* 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() {
if (this instanceof FileTransfer) {
if (transfers[this._id]) {
transfers[this._id].abort();
delete transfers[this._id];
}
}
};
module.exports = FileTransfer;

View File

@@ -0,0 +1,223 @@
/*
*
* 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 cordova = require('cordova'),
FileTransferError = require('./FileTransferError'),
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);
}
module.exports = {
abort: function (successCallback, errorCallback, args) {
var id = args[0];
if (xhr[id]) {
xhr[id].abort();
if (typeof(successCallback) === 'function') {
successCallback();
}
} else if (typeof(errorCallback) === 'function') {
errorCallback();
}
},
upload: function(successCallback, errorCallback, 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];
xhr[fileKey] = new XMLHttpRequest({mozSystem: true});
xhr[fileKey].onabort = function() {
onFail(new FileTransferError(FileTransferError.ABORT_ERR, server, filePath, this.status, xhr[fileKey].response));
};
window.resolveLocalFileSystemURL(filePath, function(entry) {
entry.file(function(file) {
var reader = new FileReader();
reader.onloadend = function() {
var blob = new Blob([this.result], {type: mimeType});
var fd = new FormData();
fd.append(fileKey, blob, fileName);
for (var prop in params) {
if (params.hasOwnProperty(prop)) {
fd.append(prop, params[prop]);
}
}
xhr[fileKey].open("POST", server);
xhr[fileKey].onload = function(evt) {
if (xhr[fileKey].status === 200) {
var result = new FileUploadResult();
result.bytesSent = blob.size;
result.responseCode = xhr[fileKey].status;
result.response = xhr[fileKey].response;
delete xhr[fileKey];
onSuccess(result);
} else if (xhr[fileKey].status === 404) {
onFail(new FileTransferError(FileTransferError.INVALID_URL_ERR, server, filePath, xhr[fileKey].status, xhr[fileKey].response));
} else {
onFail(new FileTransferError(FileTransferError.CONNECTION_ERR, server, filePath, xhr[fileKey].status, xhr[fileKey].response));
}
};
xhr[fileKey].ontimeout = function() {
onFail(new FileTransferError(FileTransferError.CONNECTION_ERR, server, filePath, xhr[fileKey].status, xhr[fileKey].response));
};
xhr[fileKey].onerror = function() {
onFail(new FileTransferError(FileTransferError.CONNECTION_ERR, server, filePath, this.status, xhr[fileKey].response));
};
for (var header in headers) {
if (headers.hasOwnProperty(header)) {
xhr[fileKey].setRequestHeader(header, headers[header]);
}
}
xhr[fileKey].send(fd);
};
reader.readAsArrayBuffer(file);
}, function() {
onFail(new FileTransferError(FileTransferError.FILE_NOT_FOUND_ERR, server, filePath));
});
}, function() {
onFail(new FileTransferError(FileTransferError.FILE_NOT_FOUND_ERR, server, filePath));
});
function onSuccess(data) {
if (typeof(successCallback) === 'function') {
successCallback(data);
}
}
function onFail(code) {
delete xhr[fileKey];
if (typeof(errorCallback) === 'function') {
errorCallback(code);
}
}
},
download: function (successCallback, errorCallback, args) {
var source = args[0],
target = args[1],
id = args[3],
headers = args[4];
xhr[id] = new XMLHttpRequest({mozSystem: true});
xhr[id].onload = function () {
if (xhr[id].readyState === xhr[id].DONE) {
if (xhr[id].status === 200 && xhr[id].response) {
window.resolveLocalFileSystemURL(getParentPath(target), function (dir) {
dir.getFile(getFileName(target), {create: true}, writeFile, function (error) {
onFail(new FileTransferError(FileTransferError.FILE_NOT_FOUND_ERR, source, target, xhr[id].status, xhr[id].response));
});
}, function () {
onFail(new FileTransferError(FileTransferError.FILE_NOT_FOUND_ERR, source, target, xhr[id].status, xhr[id].response));
});
} else if (xhr[id].status === 404) {
onFail(new FileTransferError(FileTransferError.INVALID_URL_ERR, source, target, xhr[id].status, xhr[id].response));
} else {
onFail(new FileTransferError(FileTransferError.CONNECTION_ERR, source, target, xhr[id].status, xhr[id].response));
}
}
};
function writeFile(entry) {
entry.createWriter(function (fileWriter) {
fileWriter.onwriteend = function (evt) {
if (!evt.target.error) {
entry.filesystemName = entry.filesystem.name;
delete xhr[id];
onSuccess(entry);
} else {
onFail(evt.target.error);
}
};
fileWriter.onerror = function (evt) {
onFail(evt.target.error);
};
fileWriter.write(new Blob([xhr[id].response]));
}, function (error) {
onFail(error);
});
}
xhr[id].onerror = function (e) {
onFail(new FileTransferError(FileTransferError.CONNECTION_ERR, source, target, xhr[id].status, xhr[id].response));
};
xhr[id].onabort = function (e) {
onFail(new FileTransferError(FileTransferError.ABORT_ERR, source, target, xhr[id].status, xhr[id].response));
};
xhr[id].open("GET", source, true);
for (var header in headers) {
if (headers.hasOwnProperty(header)) {
xhr[id].setRequestHeader(header, headers[header]);
}
}
xhr[id].responseType = "blob";
setTimeout(function () {
if (xhr[id]) {
xhr[id].send();
}
}, 0);
function onSuccess(entry) {
if (typeof(successCallback) === 'function') {
successCallback(entry);
}
}
function onFail(error) {
delete xhr[id];
if (typeof(errorCallback) === 'function') {
errorCallback(error);
}
}
}
};
require('cordova/exec/proxy').add('FileTransfer', module.exports);

View File

@@ -0,0 +1,71 @@
/*
*
* 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 chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
INVALID_CHARACTER_ERR = (function () {
// fabricate a suitable error object
try { document.createElement('$'); }
catch (error) { return error; }
}());
// encoder
// [https://gist.github.com/999166] by [https://github.com/nignag]
window.btoa || (
window.btoa = function (input) {
for (
// initialize result and counter
var block, charCode, idx = 0, map = chars, output = '';
// if the next input index does not exist:
// change the mapping table to "="
// check if d has no fractional digits
input.charAt(idx | 0) || (map = '=', idx % 1) ;
// "8 - idx % 1 * 8" generates the sequence 2, 4, 6, 8
output += map.charAt(63 & block >> 8 - idx % 1 * 8)
) {
charCode = input.charCodeAt(idx += 3 / 4);
if (charCode > 0xFF) throw INVALID_CHARACTER_ERR;
block = block << 8 | charCode;
}
return output;
});
// decoder
// [https://gist.github.com/1020396] by [https://github.com/atk]
window.atob || (
window.atob = function (input) {
input = input.replace(/=+$/, '')
if (input.length % 4 == 1) throw INVALID_CHARACTER_ERR;
for (
// initialize result and counters
var bc = 0, bs, buffer, idx = 0, output = '';
// get next character
buffer = input.charAt(idx++) ;
// character found in table? initialize bit storage and add its ascii value;
~buffer && (bs = bc % 4 ? bs * 64 + buffer : buffer,
// and if not first of each 4 characters,
// convert the first 8 bits to one ascii character
bc++ % 4) ? output += String.fromCharCode(255 & bs >> (-2 * bc & 6)) : 0
) {
// try to find character in table (0-63, not found => -1)
buffer = chars.indexOf(buffer);
}
return output;
});