mirror of
https://github.com/apache/cordova-plugin-file-transfer.git
synced 2026-02-02 00:00:05 +08:00
refactor(eslint): use cordova-eslint /w fix (#275)
This commit is contained in:
23
.eslintrc.yml
Normal file
23
.eslintrc.yml
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
# 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.
|
||||||
|
|
||||||
|
root: true
|
||||||
|
extends: '@cordova/eslint-config/browser'
|
||||||
|
|
||||||
|
overrides:
|
||||||
|
- files: [tests/**/*.js]
|
||||||
|
extends: '@cordova/eslint-config/node-tests'
|
||||||
23
.jscsrc
23
.jscsrc
@@ -1,23 +0,0 @@
|
|||||||
{
|
|
||||||
"disallowMixedSpacesAndTabs": true,
|
|
||||||
"disallowTrailingWhitespace": true,
|
|
||||||
"validateIndentation": 4,
|
|
||||||
"requireLineFeedAtFileEnd": true,
|
|
||||||
|
|
||||||
"disallowSpaceAfterPrefixUnaryOperators": true,
|
|
||||||
"disallowSpaceBeforePostfixUnaryOperators": true,
|
|
||||||
"requireSpaceAfterLineComment": true,
|
|
||||||
"requireCapitalizedConstructors": true,
|
|
||||||
|
|
||||||
"disallowSpacesInNamedFunctionExpression": {
|
|
||||||
"beforeOpeningRoundBrace": true
|
|
||||||
},
|
|
||||||
|
|
||||||
"requireSpaceAfterKeywords": [
|
|
||||||
"if",
|
|
||||||
"else",
|
|
||||||
"for",
|
|
||||||
"while",
|
|
||||||
"do"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
19
.jshintrc
19
.jshintrc
@@ -1,19 +0,0 @@
|
|||||||
{
|
|
||||||
"browser": true
|
|
||||||
, "devel": true
|
|
||||||
, "bitwise": true
|
|
||||||
, "undef": true
|
|
||||||
, "trailing": true
|
|
||||||
, "quotmark": false
|
|
||||||
, "indent": 4
|
|
||||||
, "unused": "vars"
|
|
||||||
, "latedef": "nofunc"
|
|
||||||
, "globals": {
|
|
||||||
"module": false,
|
|
||||||
"exports": false,
|
|
||||||
"require": false,
|
|
||||||
"FileTransferError": true,
|
|
||||||
"FileUploadResult": true,
|
|
||||||
"resolveLocalFileSystemURI": false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -13,9 +13,8 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "npm run lint && npm run style",
|
"test": "npm run lint",
|
||||||
"lint": "jshint www && jshint src && jshint tests",
|
"lint": "eslint ."
|
||||||
"style": "jscs tests/tests.js"
|
|
||||||
},
|
},
|
||||||
"repository": "github:apache/cordova-plugin-file-transfer",
|
"repository": "github:apache/cordova-plugin-file-transfer",
|
||||||
"bugs": "https://github.com/apache/cordova-plugin-file-transfer/issues",
|
"bugs": "https://github.com/apache/cordova-plugin-file-transfer/issues",
|
||||||
@@ -39,7 +38,6 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"jscs": "^2.6.0",
|
"@cordova/eslint-config": "^3.0.0"
|
||||||
"jshint": "^2.8.0"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,28 +17,25 @@
|
|||||||
* specific language governing permissions and limitations
|
* specific language governing permissions and limitations
|
||||||
* under the License.
|
* under the License.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*jshint -W030 */
|
/* global Windows, WinJS */
|
||||||
/*global Windows, WinJS*/
|
|
||||||
/*global module, require*/
|
|
||||||
|
|
||||||
var FTErr = require('./FileTransferError'),
|
var FTErr = require('./FileTransferError');
|
||||||
ProgressEvent = require('cordova-plugin-file.ProgressEvent'),
|
var ProgressEvent = require('cordova-plugin-file.ProgressEvent');
|
||||||
FileUploadResult = require('cordova-plugin-file.FileUploadResult'),
|
var FileUploadResult = require('cordova-plugin-file.FileUploadResult');
|
||||||
FileProxy = require('cordova-plugin-file.FileProxy');
|
var FileProxy = require('cordova-plugin-file.FileProxy');
|
||||||
|
|
||||||
var appData = Windows.Storage.ApplicationData.current;
|
var appData = Windows.Storage.ApplicationData.current;
|
||||||
|
|
||||||
var LINE_START = "--";
|
var LINE_START = '--';
|
||||||
var LINE_END = "\r\n";
|
var LINE_END = '\r\n';
|
||||||
var BOUNDARY = '+++++';
|
var BOUNDARY = '+++++';
|
||||||
|
|
||||||
var fileTransferOps = [];
|
var fileTransferOps = [];
|
||||||
|
|
||||||
// Some private helper functions, hidden by the module
|
// Some private helper functions, hidden by the module
|
||||||
function cordovaPathToNative(path) {
|
function cordovaPathToNative (path) {
|
||||||
|
|
||||||
var cleanPath = String(path);
|
var cleanPath = String(path);
|
||||||
// turn / into \\
|
// turn / into \\
|
||||||
cleanPath = cleanPath.replace(/\//g, '\\');
|
cleanPath = cleanPath.replace(/\//g, '\\');
|
||||||
@@ -49,11 +46,11 @@ function cordovaPathToNative(path) {
|
|||||||
return cleanPath;
|
return cleanPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
function nativePathToCordova(path) {
|
function nativePathToCordova (path) {
|
||||||
return String(path).replace(/\\/g, '/');
|
return String(path).replace(/\\/g, '/');
|
||||||
}
|
}
|
||||||
|
|
||||||
function alreadyCancelled(opId) {
|
function alreadyCancelled (opId) {
|
||||||
var op = fileTransferOps[opId];
|
var op = fileTransferOps[opId];
|
||||||
return op && op.state === FileTransferOperation.CANCELLED;
|
return op && op.state === FileTransferOperation.CANCELLED;
|
||||||
}
|
}
|
||||||
@@ -145,7 +142,7 @@ function doUpload (upload, uploadId, filePath, server, successCallback, errorCal
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function FileTransferOperation(state, promise) {
|
function FileTransferOperation (state, promise) {
|
||||||
this.state = state;
|
this.state = state;
|
||||||
this.promise = promise;
|
this.promise = promise;
|
||||||
}
|
}
|
||||||
@@ -157,9 +154,8 @@ FileTransferOperation.CANCELLED = 2;
|
|||||||
var HTTP_E_STATUS_NOT_MODIFIED = -2145844944;
|
var HTTP_E_STATUS_NOT_MODIFIED = -2145844944;
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
/*
|
||||||
/*
|
exec(win, fail, 'FileTransfer', 'upload',
|
||||||
exec(win, fail, 'FileTransfer', 'upload',
|
|
||||||
[filePath, server, fileKey, fileName, mimeType, params, trustAllHosts, chunkedMode, headers, this._id, httpMethod]);
|
[filePath, server, fileKey, fileName, mimeType, params, trustAllHosts, chunkedMode, headers, this._id, httpMethod]);
|
||||||
*/
|
*/
|
||||||
upload: function (successCallback, errorCallback, options) {
|
upload: function (successCallback, errorCallback, options) {
|
||||||
@@ -170,14 +166,14 @@ exec(win, fail, 'FileTransfer', 'upload',
|
|||||||
var mimeType = options[4];
|
var mimeType = options[4];
|
||||||
var params = options[5];
|
var params = options[5];
|
||||||
// var trustAllHosts = options[6]; // todo
|
// var trustAllHosts = options[6]; // todo
|
||||||
// var chunkedMode = options[7]; // todo
|
// var chunkedMode = options[7]; // todo
|
||||||
var headers = options[8] || {};
|
var headers = options[8] || {};
|
||||||
var uploadId = options[9];
|
var uploadId = options[9];
|
||||||
var httpMethod = options[10];
|
var httpMethod = options[10];
|
||||||
|
|
||||||
var isMultipart = typeof headers["Content-Type"] === 'undefined';
|
var isMultipart = typeof headers['Content-Type'] === 'undefined';
|
||||||
|
|
||||||
function stringToByteArray(str) {
|
function stringToByteArray (str) {
|
||||||
var byteCharacters = atob(str);
|
var byteCharacters = atob(str);
|
||||||
var byteNumbers = new Array(byteCharacters.length);
|
var byteNumbers = new Array(byteCharacters.length);
|
||||||
for (var i = 0; i < byteCharacters.length; i++) {
|
for (var i = 0; i < byteCharacters.length; i++) {
|
||||||
@@ -186,21 +182,21 @@ exec(win, fail, 'FileTransfer', 'upload',
|
|||||||
return new Uint8Array(byteNumbers);
|
return new Uint8Array(byteNumbers);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!filePath || (typeof filePath !== 'string')) {
|
if (!filePath || typeof filePath !== 'string') {
|
||||||
errorCallback(new FTErr(FTErr.FILE_NOT_FOUND_ERR, null, server));
|
errorCallback(new FTErr(FTErr.FILE_NOT_FOUND_ERR, null, server));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (filePath.indexOf("data:") === 0 && filePath.indexOf("base64") !== -1) {
|
if (filePath.indexOf('data:') === 0 && filePath.indexOf('base64') !== -1) {
|
||||||
// First a DataWriter object is created, backed by an in-memory stream where
|
// First a DataWriter object is created, backed by an in-memory stream where
|
||||||
// the data will be stored.
|
// the data will be stored.
|
||||||
var writer = Windows.Storage.Streams.DataWriter(new Windows.Storage.Streams.InMemoryRandomAccessStream());
|
var writer = Windows.Storage.Streams.DataWriter(new Windows.Storage.Streams.InMemoryRandomAccessStream());
|
||||||
writer.unicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.utf8;
|
writer.unicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.utf8;
|
||||||
writer.byteOrder = Windows.Storage.Streams.ByteOrder.littleEndian;
|
writer.byteOrder = Windows.Storage.Streams.ByteOrder.littleEndian;
|
||||||
|
|
||||||
var commaIndex = filePath.indexOf(",");
|
var commaIndex = filePath.indexOf(',');
|
||||||
if (commaIndex === -1) {
|
if (commaIndex === -1) {
|
||||||
errorCallback(new FTErr(FTErr.INVALID_URL_ERR, fileName, server, null, null, "No comma in data: URI"));
|
errorCallback(new FTErr(FTErr.INVALID_URL_ERR, fileName, server, null, null, 'No comma in data: URI'));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -213,7 +209,7 @@ exec(win, fail, 'FileTransfer', 'upload',
|
|||||||
var uploader = new Windows.Networking.BackgroundTransfer.BackgroundUploader();
|
var uploader = new Windows.Networking.BackgroundTransfer.BackgroundUploader();
|
||||||
uploader.method = httpMethod;
|
uploader.method = httpMethod;
|
||||||
for (var header in headers) {
|
for (var header in headers) {
|
||||||
if (headers.hasOwnProperty(header)) {
|
if (Object.prototype.hasOwnProperty.call(headers, header)) {
|
||||||
uploader.setRequestHeader(header, headers[header]);
|
uploader.setRequestHeader(header, headers[header]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -222,9 +218,9 @@ exec(win, fail, 'FileTransfer', 'upload',
|
|||||||
// adding params supplied to request payload
|
// adding params supplied to request payload
|
||||||
var multipartParams = '';
|
var multipartParams = '';
|
||||||
for (var key in params) {
|
for (var key in params) {
|
||||||
if (params.hasOwnProperty(key)) {
|
if (Object.prototype.hasOwnProperty.call(params, key)) {
|
||||||
multipartParams += LINE_START + BOUNDARY + LINE_END;
|
multipartParams += LINE_START + BOUNDARY + LINE_END;
|
||||||
multipartParams += "Content-Disposition: form-data; name=\"" + key + "\"";
|
multipartParams += 'Content-Disposition: form-data; name="' + key + '"';
|
||||||
multipartParams += LINE_END + LINE_END;
|
multipartParams += LINE_END + LINE_END;
|
||||||
multipartParams += params[key];
|
multipartParams += params[key];
|
||||||
multipartParams += LINE_END;
|
multipartParams += LINE_END;
|
||||||
@@ -232,13 +228,13 @@ exec(win, fail, 'FileTransfer', 'upload',
|
|||||||
}
|
}
|
||||||
|
|
||||||
var multipartFile = LINE_START + BOUNDARY + LINE_END;
|
var multipartFile = LINE_START + BOUNDARY + LINE_END;
|
||||||
multipartFile += "Content-Disposition: form-data; name=\"file\";";
|
multipartFile += 'Content-Disposition: form-data; name="file";';
|
||||||
multipartFile += " filename=\"" + fileName + "\"" + LINE_END;
|
multipartFile += ' filename="' + fileName + '"' + LINE_END;
|
||||||
multipartFile += "Content-Type: " + mimeType + LINE_END + LINE_END;
|
multipartFile += 'Content-Type: ' + mimeType + LINE_END + LINE_END;
|
||||||
|
|
||||||
var bound = LINE_END + LINE_START + BOUNDARY + LINE_START + LINE_END;
|
var bound = LINE_END + LINE_START + BOUNDARY + LINE_START + LINE_END;
|
||||||
|
|
||||||
uploader.setRequestHeader("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
|
uploader.setRequestHeader('Content-Type', 'multipart/form-data; boundary=' + BOUNDARY);
|
||||||
writer.writeString(multipartParams);
|
writer.writeString(multipartParams);
|
||||||
writer.writeString(multipartFile);
|
writer.writeString(multipartFile);
|
||||||
writer.writeBytes(stringToByteArray(fileDataString));
|
writer.writeBytes(stringToByteArray(fileDataString));
|
||||||
@@ -249,42 +245,142 @@ exec(win, fail, 'FileTransfer', 'upload',
|
|||||||
|
|
||||||
var stream;
|
var stream;
|
||||||
|
|
||||||
// The call to store async sends the actual contents of the writer
|
// The call to store async sends the actual contents of the writer
|
||||||
// to the backing stream.
|
// to the backing stream.
|
||||||
writer.storeAsync().then(function () {
|
writer
|
||||||
// For the in-memory stream implementation we are using, the flushAsync call
|
.storeAsync()
|
||||||
// is superfluous, but other types of streams may require it.
|
.then(function () {
|
||||||
return writer.flushAsync();
|
// For the in-memory stream implementation we are using, the flushAsync call
|
||||||
}).then(function () {
|
// is superfluous, but other types of streams may require it.
|
||||||
// We detach the stream to prolong its useful lifetime. Were we to fail
|
return writer.flushAsync();
|
||||||
// to detach the stream, the call to writer.close() would close the underlying
|
})
|
||||||
// stream, preventing its subsequent use by the DataReader below. Most clients
|
.then(function () {
|
||||||
// of DataWriter will have no reason to use the underlying stream after
|
// We detach the stream to prolong its useful lifetime. Were we to fail
|
||||||
// writer.close() is called, and will therefore have no reason to call
|
// to detach the stream, the call to writer.close() would close the underlying
|
||||||
// writer.detachStream(). Note that once we detach the stream, we assume
|
// stream, preventing its subsequent use by the DataReader below. Most clients
|
||||||
// responsibility for closing the stream subsequently; after the stream
|
// of DataWriter will have no reason to use the underlying stream after
|
||||||
// has been detached, a call to writer.close() will have no effect on the stream.
|
// writer.close() is called, and will therefore have no reason to call
|
||||||
stream = writer.detachStream();
|
// writer.detachStream(). Note that once we detach the stream, we assume
|
||||||
// Make sure the stream is read from the beginning in the reader
|
// responsibility for closing the stream subsequently; after the stream
|
||||||
// we are creating below.
|
// has been detached, a call to writer.close() will have no effect on the stream.
|
||||||
stream.seek(0);
|
stream = writer.detachStream();
|
||||||
// Most DataWriter clients will not call writer.detachStream(),
|
// Make sure the stream is read from the beginning in the reader
|
||||||
// and furthermore will be working with a file-backed or network-backed stream,
|
// we are creating below.
|
||||||
// rather than an in-memory-stream. In such cases, it would be particularly
|
stream.seek(0);
|
||||||
// important to call writer.close(). Doing so is always a best practice.
|
// Most DataWriter clients will not call writer.detachStream(),
|
||||||
writer.close();
|
// and furthermore will be working with a file-backed or network-backed stream,
|
||||||
|
// rather than an in-memory-stream. In such cases, it would be particularly
|
||||||
|
// important to call writer.close(). Doing so is always a best practice.
|
||||||
|
writer.close();
|
||||||
|
|
||||||
|
if (alreadyCancelled(uploadId)) {
|
||||||
|
errorCallback(new FTErr(FTErr.ABORT_ERR, nativePathToCordova(filePath), server));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// create download object. This will throw an exception if URL is malformed
|
||||||
|
var uri = new Windows.Foundation.Uri(server);
|
||||||
|
|
||||||
|
var createUploadOperation;
|
||||||
|
try {
|
||||||
|
createUploadOperation = uploader.createUploadFromStreamAsync(uri, stream);
|
||||||
|
} catch (e) {
|
||||||
|
errorCallback(new FTErr(FTErr.INVALID_URL_ERR));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
createUploadOperation.then(
|
||||||
|
function (upload) {
|
||||||
|
doUpload(upload, uploadId, filePath, server, successCallback, errorCallback);
|
||||||
|
},
|
||||||
|
function (err) {
|
||||||
|
var errorObj = new FTErr(FTErr.INVALID_URL_ERR);
|
||||||
|
errorObj.exception = err;
|
||||||
|
errorCallback(errorObj);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (filePath.substr(0, 8) === 'file:///') {
|
||||||
|
filePath = appData.localFolder.path + filePath.substr(8).split('/').join('\\');
|
||||||
|
} else if (filePath.indexOf('ms-appdata:///') === 0) {
|
||||||
|
// Handle 'ms-appdata' scheme
|
||||||
|
filePath = filePath
|
||||||
|
.replace('ms-appdata:///local', appData.localFolder.path)
|
||||||
|
.replace('ms-appdata:///temp', appData.temporaryFolder.path);
|
||||||
|
} else if (filePath.indexOf('cdvfile://') === 0) {
|
||||||
|
filePath = filePath
|
||||||
|
.replace('cdvfile://localhost/persistent', appData.localFolder.path)
|
||||||
|
.replace('cdvfile://localhost/temporary', appData.temporaryFolder.path);
|
||||||
|
}
|
||||||
|
|
||||||
|
// normalize path separators
|
||||||
|
filePath = cordovaPathToNative(filePath);
|
||||||
|
|
||||||
|
// Create internal download operation object
|
||||||
|
fileTransferOps[uploadId] = new FileTransferOperation(FileTransferOperation.PENDING, null);
|
||||||
|
|
||||||
|
Windows.Storage.StorageFile.getFileFromPathAsync(filePath).then(
|
||||||
|
function (storageFile) {
|
||||||
|
if (!fileName) {
|
||||||
|
fileName = storageFile.name;
|
||||||
|
}
|
||||||
|
if (!mimeType) {
|
||||||
|
// use the actual content type of the file, probably this should be the default way.
|
||||||
|
// other platforms probably can't look this up.
|
||||||
|
mimeType = storageFile.contentType;
|
||||||
|
}
|
||||||
|
|
||||||
if (alreadyCancelled(uploadId)) {
|
if (alreadyCancelled(uploadId)) {
|
||||||
errorCallback(new FTErr(FTErr.ABORT_ERR, nativePathToCordova(filePath), server));
|
errorCallback(new FTErr(FTErr.ABORT_ERR, nativePathToCordova(filePath), server));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// setting request headers for uploader
|
||||||
|
var uploader = new Windows.Networking.BackgroundTransfer.BackgroundUploader();
|
||||||
|
uploader.method = httpMethod;
|
||||||
|
for (var header in headers) {
|
||||||
|
if (Object.prototype.hasOwnProperty.call(headers, header)) {
|
||||||
|
uploader.setRequestHeader(header, headers[header]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// create download object. This will throw an exception if URL is malformed
|
// create download object. This will throw an exception if URL is malformed
|
||||||
var uri = new Windows.Foundation.Uri(server);
|
var uri = new Windows.Foundation.Uri(server);
|
||||||
|
|
||||||
var createUploadOperation;
|
var createUploadOperation;
|
||||||
try {
|
try {
|
||||||
createUploadOperation = uploader.createUploadFromStreamAsync(uri, stream);
|
if (isMultipart) {
|
||||||
|
// adding params supplied to request payload
|
||||||
|
var transferParts = [];
|
||||||
|
for (var key in params) {
|
||||||
|
// Create content part for params only if value is specified because CreateUploadAsync fails otherwise
|
||||||
|
if (
|
||||||
|
Object.prototype.hasOwnProperty.call(params, key) &&
|
||||||
|
params[key] !== null &&
|
||||||
|
params[key] !== undefined &&
|
||||||
|
params[key].toString() !== ''
|
||||||
|
) {
|
||||||
|
var contentPart = new Windows.Networking.BackgroundTransfer.BackgroundTransferContentPart();
|
||||||
|
contentPart.setHeader('Content-Disposition', 'form-data; name="' + key + '"');
|
||||||
|
contentPart.setText(params[key]);
|
||||||
|
transferParts.push(contentPart);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Adding file to upload to request payload
|
||||||
|
var fileToUploadPart = new Windows.Networking.BackgroundTransfer.BackgroundTransferContentPart(fileKey, fileName);
|
||||||
|
fileToUploadPart.setHeader('Content-Type', mimeType);
|
||||||
|
fileToUploadPart.setFile(storageFile);
|
||||||
|
transferParts.push(fileToUploadPart);
|
||||||
|
|
||||||
|
createUploadOperation = uploader.createUploadAsync(uri, transferParts);
|
||||||
|
} else {
|
||||||
|
createUploadOperation = WinJS.Promise.wrap(uploader.createUpload(uri, storageFile));
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
errorCallback(new FTErr(FTErr.INVALID_URL_ERR));
|
errorCallback(new FTErr(FTErr.INVALID_URL_ERR));
|
||||||
return;
|
return;
|
||||||
@@ -298,105 +394,17 @@ exec(win, fail, 'FileTransfer', 'upload',
|
|||||||
var errorObj = new FTErr(FTErr.INVALID_URL_ERR);
|
var errorObj = new FTErr(FTErr.INVALID_URL_ERR);
|
||||||
errorObj.exception = err;
|
errorObj.exception = err;
|
||||||
errorCallback(errorObj);
|
errorCallback(errorObj);
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (filePath.substr(0, 8) === "file:///") {
|
|
||||||
filePath = appData.localFolder.path + filePath.substr(8).split("/").join("\\");
|
|
||||||
} else if (filePath.indexOf('ms-appdata:///') === 0) {
|
|
||||||
// Handle 'ms-appdata' scheme
|
|
||||||
filePath = filePath.replace('ms-appdata:///local', appData.localFolder.path)
|
|
||||||
.replace('ms-appdata:///temp', appData.temporaryFolder.path);
|
|
||||||
} else if (filePath.indexOf('cdvfile://') === 0) {
|
|
||||||
filePath = filePath.replace('cdvfile://localhost/persistent', appData.localFolder.path)
|
|
||||||
.replace('cdvfile://localhost/temporary', appData.temporaryFolder.path);
|
|
||||||
}
|
|
||||||
|
|
||||||
// normalize path separators
|
|
||||||
filePath = cordovaPathToNative(filePath);
|
|
||||||
|
|
||||||
// Create internal download operation object
|
|
||||||
fileTransferOps[uploadId] = new FileTransferOperation(FileTransferOperation.PENDING, null);
|
|
||||||
|
|
||||||
Windows.Storage.StorageFile.getFileFromPathAsync(filePath)
|
|
||||||
.then(function (storageFile) {
|
|
||||||
|
|
||||||
if (!fileName) {
|
|
||||||
fileName = storageFile.name;
|
|
||||||
}
|
|
||||||
if (!mimeType) {
|
|
||||||
// use the actual content type of the file, probably this should be the default way.
|
|
||||||
// other platforms probably can't look this up.
|
|
||||||
mimeType = storageFile.contentType;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (alreadyCancelled(uploadId)) {
|
|
||||||
errorCallback(new FTErr(FTErr.ABORT_ERR, nativePathToCordova(filePath), server));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// setting request headers for uploader
|
|
||||||
var uploader = new Windows.Networking.BackgroundTransfer.BackgroundUploader();
|
|
||||||
uploader.method = httpMethod;
|
|
||||||
for (var header in headers) {
|
|
||||||
if (headers.hasOwnProperty(header)) {
|
|
||||||
uploader.setRequestHeader(header, headers[header]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// create download object. This will throw an exception if URL is malformed
|
|
||||||
var uri = new Windows.Foundation.Uri(server);
|
|
||||||
|
|
||||||
var createUploadOperation;
|
|
||||||
try {
|
|
||||||
if (isMultipart) {
|
|
||||||
// adding params supplied to request payload
|
|
||||||
var transferParts = [];
|
|
||||||
for (var key in params) {
|
|
||||||
// Create content part for params only if value is specified because CreateUploadAsync fails otherwise
|
|
||||||
if (params.hasOwnProperty(key) && params[key] !== null && params[key] !== undefined && params[key].toString() !== "") {
|
|
||||||
var contentPart = new Windows.Networking.BackgroundTransfer.BackgroundTransferContentPart();
|
|
||||||
contentPart.setHeader("Content-Disposition", "form-data; name=\"" + key + "\"");
|
|
||||||
contentPart.setText(params[key]);
|
|
||||||
transferParts.push(contentPart);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
);
|
||||||
// Adding file to upload to request payload
|
},
|
||||||
var fileToUploadPart = new Windows.Networking.BackgroundTransfer.BackgroundTransferContentPart(fileKey, fileName);
|
function (err) {
|
||||||
fileToUploadPart.setHeader("Content-Type", mimeType);
|
errorCallback(new FTErr(FTErr.FILE_NOT_FOUND_ERR, fileName, server, null, null, err));
|
||||||
fileToUploadPart.setFile(storageFile);
|
|
||||||
transferParts.push(fileToUploadPart);
|
|
||||||
|
|
||||||
createUploadOperation = uploader.createUploadAsync(uri, transferParts);
|
|
||||||
} else {
|
|
||||||
createUploadOperation = WinJS.Promise.wrap(uploader.createUpload(uri, storageFile));
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
errorCallback(new FTErr(FTErr.INVALID_URL_ERR));
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
);
|
||||||
createUploadOperation.then(
|
|
||||||
function (upload) {
|
|
||||||
doUpload(upload, uploadId, filePath, server, successCallback, errorCallback);
|
|
||||||
},
|
|
||||||
function (err) {
|
|
||||||
var errorObj = new FTErr(FTErr.INVALID_URL_ERR);
|
|
||||||
errorObj.exception = err;
|
|
||||||
errorCallback(errorObj);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}, function (err) {
|
|
||||||
errorCallback(new FTErr(FTErr.FILE_NOT_FOUND_ERR, fileName, server, null, null, err));
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
|
|
||||||
// [source, target, trustAllHosts, id, headers]
|
// [source, target, trustAllHosts, id, headers]
|
||||||
download:function(successCallback, errorCallback, options) {
|
download: function (successCallback, errorCallback, options) {
|
||||||
var source = options[0];
|
var source = options[0];
|
||||||
var target = options[1];
|
var target = options[1];
|
||||||
var downloadId = options[3];
|
var downloadId = options[3];
|
||||||
@@ -406,25 +414,27 @@ exec(win, fail, 'FileTransfer', 'upload',
|
|||||||
errorCallback(new FTErr(FTErr.FILE_NOT_FOUND_ERR));
|
errorCallback(new FTErr(FTErr.FILE_NOT_FOUND_ERR));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (target.substr(0, 8) === "file:///") {
|
if (target.substr(0, 8) === 'file:///') {
|
||||||
target = appData.localFolder.path + target.substr(8).split("/").join("\\");
|
target = appData.localFolder.path + target.substr(8).split('/').join('\\');
|
||||||
} else if (target.indexOf('ms-appdata:///') === 0) {
|
} else if (target.indexOf('ms-appdata:///') === 0) {
|
||||||
// Handle 'ms-appdata' scheme
|
// Handle 'ms-appdata' scheme
|
||||||
target = target.replace('ms-appdata:///local', appData.localFolder.path)
|
target = target
|
||||||
.replace('ms-appdata:///temp', appData.temporaryFolder.path);
|
.replace('ms-appdata:///local', appData.localFolder.path)
|
||||||
|
.replace('ms-appdata:///temp', appData.temporaryFolder.path);
|
||||||
} else if (target.indexOf('cdvfile://') === 0) {
|
} else if (target.indexOf('cdvfile://') === 0) {
|
||||||
target = target.replace('cdvfile://localhost/persistent', appData.localFolder.path)
|
target = target
|
||||||
.replace('cdvfile://localhost/temporary', appData.temporaryFolder.path);
|
.replace('cdvfile://localhost/persistent', appData.localFolder.path)
|
||||||
|
.replace('cdvfile://localhost/temporary', appData.temporaryFolder.path);
|
||||||
}
|
}
|
||||||
target = cordovaPathToNative(target);
|
target = cordovaPathToNative(target);
|
||||||
|
|
||||||
var path = target.substr(0, target.lastIndexOf("\\"));
|
var path = target.substr(0, target.lastIndexOf('\\'));
|
||||||
var fileName = target.substr(target.lastIndexOf("\\") + 1);
|
var fileName = target.substr(target.lastIndexOf('\\') + 1);
|
||||||
if (path === null || fileName === null) {
|
if (path === null || fileName === null) {
|
||||||
errorCallback(new FTErr(FTErr.FILE_NOT_FOUND_ERR));
|
errorCallback(new FTErr(FTErr.FILE_NOT_FOUND_ERR));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Download to a temp file to avoid the file deletion on 304
|
// Download to a temp file to avoid the file deletion on 304
|
||||||
// CB-7006 Empty file is created on file transfer if server response is 304
|
// CB-7006 Empty file is created on file transfer if server response is 304
|
||||||
var tempFileName = '~' + fileName;
|
var tempFileName = '~' + fileName;
|
||||||
|
|
||||||
@@ -433,129 +443,136 @@ exec(win, fail, 'FileTransfer', 'upload',
|
|||||||
// Create internal download operation object
|
// Create internal download operation object
|
||||||
fileTransferOps[downloadId] = new FileTransferOperation(FileTransferOperation.PENDING, null);
|
fileTransferOps[downloadId] = new FileTransferOperation(FileTransferOperation.PENDING, null);
|
||||||
|
|
||||||
var downloadCallback = function(storageFolder) {
|
var downloadCallback = function (storageFolder) {
|
||||||
storageFolder.createFileAsync(tempFileName, Windows.Storage.CreationCollisionOption.replaceExisting).then(function (storageFile) {
|
storageFolder.createFileAsync(tempFileName, Windows.Storage.CreationCollisionOption.replaceExisting).then(
|
||||||
|
function (storageFile) {
|
||||||
if (alreadyCancelled(downloadId)) {
|
if (alreadyCancelled(downloadId)) {
|
||||||
errorCallback(new FTErr(FTErr.ABORT_ERR, source, target));
|
errorCallback(new FTErr(FTErr.ABORT_ERR, source, target));
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
// if download isn't cancelled, contunue with creating and preparing download operation
|
|
||||||
var downloader = new Windows.Networking.BackgroundTransfer.BackgroundDownloader();
|
|
||||||
for (var header in headers) {
|
|
||||||
if (headers.hasOwnProperty(header)) {
|
|
||||||
downloader.setRequestHeader(header, headers[header]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// create download object. This will throw an exception if URL is malformed
|
|
||||||
try {
|
|
||||||
var uri = Windows.Foundation.Uri(source);
|
|
||||||
download = downloader.createDownload(uri, storageFile);
|
|
||||||
} catch (e) {
|
|
||||||
// so we handle this and call errorCallback
|
|
||||||
errorCallback(new FTErr(FTErr.INVALID_URL_ERR));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var downloadOperation = download.startAsync();
|
|
||||||
// update internal TransferOperation object with newly created promise
|
|
||||||
fileTransferOps[downloadId].promise = downloadOperation;
|
|
||||||
|
|
||||||
downloadOperation.then(function () {
|
|
||||||
|
|
||||||
// Update TransferOperation object with new state, delete promise property
|
|
||||||
// since it is not actual anymore
|
|
||||||
var currentDownloadOp = fileTransferOps[downloadId];
|
|
||||||
if (currentDownloadOp) {
|
|
||||||
currentDownloadOp.state = FileTransferOperation.DONE;
|
|
||||||
currentDownloadOp.promise = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
storageFile.renameAsync(fileName, Windows.Storage.CreationCollisionOption.replaceExisting).done(function () {
|
// if download isn't cancelled, contunue with creating and preparing download operation
|
||||||
var nativeURI = storageFile.path.replace(appData.localFolder.path, 'ms-appdata:///local')
|
var downloader = new Windows.Networking.BackgroundTransfer.BackgroundDownloader();
|
||||||
.replace(appData.temporaryFolder.path, 'ms-appdata:///temp')
|
for (var header in headers) {
|
||||||
.replace(/\\/g, '/');
|
if (Object.prototype.hasOwnProperty.call(headers, header)) {
|
||||||
|
downloader.setRequestHeader(header, headers[header]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Passing null as error callback here because downloaded file should exist in any case
|
// create download object. This will throw an exception if URL is malformed
|
||||||
// otherwise the error callback will be hit during file creation in another place
|
try {
|
||||||
FileProxy.resolveLocalFileSystemURI(successCallback, null, [nativeURI]);
|
var uri = Windows.Foundation.Uri(source);
|
||||||
}, function(error) {
|
download = downloader.createDownload(uri, storageFile);
|
||||||
errorCallback(new FTErr(FTErr.FILE_NOT_FOUND_ERR, source, target, null, null, error));
|
} catch (e) {
|
||||||
});
|
// so we handle this and call errorCallback
|
||||||
}, function(error) {
|
errorCallback(new FTErr(FTErr.INVALID_URL_ERR));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var getTransferError = new WinJS.Promise(function (resolve) {
|
var downloadOperation = download.startAsync();
|
||||||
// Handle download error here. If download was cancelled,
|
// update internal TransferOperation object with newly created promise
|
||||||
// message property will be specified
|
fileTransferOps[downloadId].promise = downloadOperation;
|
||||||
if (error.message === 'Canceled') {
|
|
||||||
resolve(new FTErr(FTErr.ABORT_ERR, source, target, null, null, error));
|
downloadOperation.then(
|
||||||
} else if (error && error.number === HTTP_E_STATUS_NOT_MODIFIED) {
|
function () {
|
||||||
resolve(new FTErr(FTErr.NOT_MODIFIED_ERR, source, target, 304, null, error));
|
// Update TransferOperation object with new state, delete promise property
|
||||||
} else {
|
// since it is not actual anymore
|
||||||
// in the other way, try to get response property
|
var currentDownloadOp = fileTransferOps[downloadId];
|
||||||
var response = download.getResponseInformation();
|
if (currentDownloadOp) {
|
||||||
if (!response) {
|
currentDownloadOp.state = FileTransferOperation.DONE;
|
||||||
resolve(new FTErr(FTErr.CONNECTION_ERR, source, target));
|
currentDownloadOp.promise = null;
|
||||||
} else {
|
|
||||||
if (download.progress.bytesReceived === 0) {
|
|
||||||
resolve(new FTErr(FTErr.FILE_NOT_FOUND_ERR, source, target, response.statusCode, null, error));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var reader = new Windows.Storage.Streams.DataReader(download.getResultStreamAt(0));
|
|
||||||
reader.loadAsync(download.progress.bytesReceived).then(function (bytesLoaded) {
|
|
||||||
var payload = reader.readString(bytesLoaded);
|
|
||||||
resolve(new FTErr(FTErr.FILE_NOT_FOUND_ERR, source, target, response.statusCode, payload, error));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
storageFile.renameAsync(fileName, Windows.Storage.CreationCollisionOption.replaceExisting).done(
|
||||||
|
function () {
|
||||||
|
var nativeURI = storageFile.path
|
||||||
|
.replace(appData.localFolder.path, 'ms-appdata:///local')
|
||||||
|
.replace(appData.temporaryFolder.path, 'ms-appdata:///temp')
|
||||||
|
.replace(/\\/g, '/');
|
||||||
|
|
||||||
|
// Passing null as error callback here because downloaded file should exist in any case
|
||||||
|
// otherwise the error callback will be hit during file creation in another place
|
||||||
|
FileProxy.resolveLocalFileSystemURI(successCallback, null, [nativeURI]);
|
||||||
|
},
|
||||||
|
function (error) {
|
||||||
|
errorCallback(new FTErr(FTErr.FILE_NOT_FOUND_ERR, source, target, null, null, error));
|
||||||
|
}
|
||||||
|
);
|
||||||
|
},
|
||||||
|
function (error) {
|
||||||
|
var getTransferError = new WinJS.Promise(function (resolve) {
|
||||||
|
// Handle download error here. If download was cancelled,
|
||||||
|
// message property will be specified
|
||||||
|
if (error.message === 'Canceled') {
|
||||||
|
resolve(new FTErr(FTErr.ABORT_ERR, source, target, null, null, error));
|
||||||
|
} else if (error && error.number === HTTP_E_STATUS_NOT_MODIFIED) {
|
||||||
|
resolve(new FTErr(FTErr.NOT_MODIFIED_ERR, source, target, 304, null, error));
|
||||||
|
} else {
|
||||||
|
// in the other way, try to get response property
|
||||||
|
var response = download.getResponseInformation();
|
||||||
|
if (!response) {
|
||||||
|
resolve(new FTErr(FTErr.CONNECTION_ERR, source, target));
|
||||||
|
} else {
|
||||||
|
if (download.progress.bytesReceived === 0) {
|
||||||
|
resolve(new FTErr(FTErr.FILE_NOT_FOUND_ERR, source, target, response.statusCode, null, error));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var reader = new Windows.Storage.Streams.DataReader(download.getResultStreamAt(0));
|
||||||
|
reader.loadAsync(download.progress.bytesReceived).then(function (bytesLoaded) {
|
||||||
|
var payload = reader.readString(bytesLoaded);
|
||||||
|
resolve(
|
||||||
|
new FTErr(FTErr.FILE_NOT_FOUND_ERR, source, target, response.statusCode, payload, error)
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
getTransferError.then(function (fileTransferError) {
|
||||||
|
// Update TransferOperation object with new state, delete promise property
|
||||||
|
// since it is not actual anymore
|
||||||
|
var currentDownloadOp = fileTransferOps[downloadId];
|
||||||
|
if (currentDownloadOp) {
|
||||||
|
currentDownloadOp.state = FileTransferOperation.CANCELLED;
|
||||||
|
currentDownloadOp.promise = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cleanup, remove incompleted file
|
||||||
|
storageFile.deleteAsync().then(function () {
|
||||||
|
errorCallback(fileTransferError);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
function (evt) {
|
||||||
|
var progressEvent = new ProgressEvent('progress', {
|
||||||
|
loaded: evt.progress.bytesReceived,
|
||||||
|
total: evt.progress.totalBytesToReceive,
|
||||||
|
target: evt.resultFile
|
||||||
|
});
|
||||||
|
// when bytesReceived == 0, BackgroundDownloader has not yet differentiated whether it could get file length or not,
|
||||||
|
// when totalBytesToReceive == 0, BackgroundDownloader is unable to get file length
|
||||||
|
progressEvent.lengthComputable = evt.progress.bytesReceived > 0 && evt.progress.totalBytesToReceive > 0;
|
||||||
|
|
||||||
|
successCallback(progressEvent, { keepCallback: true });
|
||||||
}
|
}
|
||||||
});
|
);
|
||||||
getTransferError.then(function (fileTransferError) {
|
},
|
||||||
|
function (error) {
|
||||||
// Update TransferOperation object with new state, delete promise property
|
errorCallback(new FTErr(FTErr.FILE_NOT_FOUND_ERR, source, target, null, null, error));
|
||||||
// since it is not actual anymore
|
}
|
||||||
var currentDownloadOp = fileTransferOps[downloadId];
|
);
|
||||||
if (currentDownloadOp) {
|
|
||||||
currentDownloadOp.state = FileTransferOperation.CANCELLED;
|
|
||||||
currentDownloadOp.promise = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Cleanup, remove incompleted file
|
|
||||||
storageFile.deleteAsync().then(function() {
|
|
||||||
errorCallback(fileTransferError);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
}, function(evt) {
|
|
||||||
|
|
||||||
var progressEvent = new ProgressEvent('progress', {
|
|
||||||
loaded: evt.progress.bytesReceived,
|
|
||||||
total: evt.progress.totalBytesToReceive,
|
|
||||||
target: evt.resultFile
|
|
||||||
});
|
|
||||||
// when bytesReceived == 0, BackgroundDownloader has not yet differentiated whether it could get file length or not,
|
|
||||||
// when totalBytesToReceive == 0, BackgroundDownloader is unable to get file length
|
|
||||||
progressEvent.lengthComputable = (evt.progress.bytesReceived > 0) && (evt.progress.totalBytesToReceive > 0);
|
|
||||||
|
|
||||||
successCallback(progressEvent, { keepCallback: true });
|
|
||||||
});
|
|
||||||
}, function(error) {
|
|
||||||
errorCallback(new FTErr(FTErr.FILE_NOT_FOUND_ERR, source, target, null, null, error));
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
var fileNotFoundErrorCallback = function(error) {
|
var fileNotFoundErrorCallback = function (error) {
|
||||||
errorCallback(new FTErr(FTErr.FILE_NOT_FOUND_ERR, source, target, null, null, error));
|
errorCallback(new FTErr(FTErr.FILE_NOT_FOUND_ERR, source, target, null, null, error));
|
||||||
};
|
};
|
||||||
|
|
||||||
Windows.Storage.StorageFolder.getFolderFromPathAsync(path).then(downloadCallback, function (error) {
|
Windows.Storage.StorageFolder.getFolderFromPathAsync(path).then(downloadCallback, function (error) {
|
||||||
// Handle non-existent directory
|
// Handle non-existent directory
|
||||||
if (error.number === -2147024894) {
|
if (error.number === -2147024894) {
|
||||||
var parent = path.substr(0, path.lastIndexOf('\\')),
|
var parent = path.substr(0, path.lastIndexOf('\\'));
|
||||||
folderNameToCreate = path.substr(path.lastIndexOf('\\') + 1);
|
var folderNameToCreate = path.substr(path.lastIndexOf('\\') + 1);
|
||||||
|
|
||||||
Windows.Storage.StorageFolder.getFolderFromPathAsync(parent).then(function(parentFolder) {
|
Windows.Storage.StorageFolder.getFolderFromPathAsync(parent).then(function (parentFolder) {
|
||||||
parentFolder.createFolderAsync(folderNameToCreate).then(downloadCallback, fileNotFoundErrorCallback);
|
parentFolder.createFolderAsync(folderNameToCreate).then(downloadCallback, fileNotFoundErrorCallback);
|
||||||
}, fileNotFoundErrorCallback);
|
}, fileNotFoundErrorCallback);
|
||||||
} else {
|
} else {
|
||||||
@@ -577,7 +594,6 @@ exec(win, fail, 'FileTransfer', 'upload',
|
|||||||
fileTransferOps[fileTransferOpId] = new FileTransferOperation(FileTransferOperation.CANCELLED, null);
|
fileTransferOps[fileTransferOpId] = new FileTransferOperation(FileTransferOperation.CANCELLED, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
require("cordova/exec/proxy").add("FileTransfer",module.exports);
|
require('cordova/exec/proxy').add('FileTransfer', module.exports);
|
||||||
|
|||||||
@@ -1,46 +1,48 @@
|
|||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
/*
|
/*
|
||||||
*
|
*
|
||||||
* Licensed to the Apache Software Foundation (ASF) under one
|
* Licensed to the Apache Software Foundation (ASF) under one
|
||||||
* or more contributor license agreements. See the NOTICE file
|
* or more contributor license agreements. See the NOTICE file
|
||||||
* distributed with this work for additional information
|
* distributed with this work for additional information
|
||||||
* regarding copyright ownership. The ASF licenses this file
|
* regarding copyright ownership. The ASF licenses this file
|
||||||
* to you under the Apache License, Version 2.0 (the
|
* to you under the Apache License, Version 2.0 (the
|
||||||
* "License"); you may not use this file except in compliance
|
* "License"); you may not use this file except in compliance
|
||||||
* with the License. You may obtain a copy of the License at
|
* with the License. You may obtain a copy of the License at
|
||||||
*
|
*
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
*
|
*
|
||||||
* Unless required by applicable law or agreed to in writing,
|
* Unless required by applicable law or agreed to in writing,
|
||||||
* software distributed under the License is distributed on an
|
* software distributed under the License is distributed on an
|
||||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
* KIND, either express or implied. See the License for the
|
* KIND, either express or implied. See the License for the
|
||||||
* specific language governing permissions and limitations
|
* specific language governing permissions and limitations
|
||||||
* under the License.
|
* under the License.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
|
|
||||||
module.exports = function(context) {
|
module.exports = function (context) {
|
||||||
function main() {
|
function main () {
|
||||||
// get the file transfer server address from the specified variables
|
// get the file transfer server address from the specified variables
|
||||||
var fileTransferServerAddress = getFileTransferServerAddress(context) || getDefaultFileTransferServerAddress(context);
|
var fileTransferServerAddress = getFileTransferServerAddress(context) || getDefaultFileTransferServerAddress(context);
|
||||||
console.log('Tests will use the following file transfer server address: ' + fileTransferServerAddress);
|
console.log('Tests will use the following file transfer server address: ' + fileTransferServerAddress);
|
||||||
console.log('If you\'re using cordova@6.3.1 and the above address is wrong at "platform add", don\'t worry, it\'ll fix itself on "cordova run" or "cordova prepare".');
|
console.log(
|
||||||
|
'If you\'re using cordova@6.3.1 and the above address is wrong at "platform add", don\'t worry, it\'ll fix itself on "cordova run" or "cordova prepare".'
|
||||||
|
);
|
||||||
|
|
||||||
// pass it to the tests
|
// pass it to the tests
|
||||||
writeFileTransferOptions(fileTransferServerAddress, context);
|
writeFileTransferOptions(fileTransferServerAddress, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getDefaultFileTransferServerAddress(context) {
|
function getDefaultFileTransferServerAddress (context) {
|
||||||
var address = null;
|
var address = null;
|
||||||
var configNodes = context.opts.plugin.pluginInfo._et._root._children;
|
var configNodes = context.opts.plugin.pluginInfo._et._root._children;
|
||||||
|
|
||||||
for (var node in configNodes) {
|
for (var node in configNodes) {
|
||||||
if (configNodes[node].attrib.name == 'FILETRANSFER_SERVER_ADDRESS') {
|
if (configNodes[node].attrib.name === 'FILETRANSFER_SERVER_ADDRESS') {
|
||||||
address = configNodes[node].attrib.default;
|
address = configNodes[node].attrib.default;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -48,18 +50,28 @@ module.exports = function(context) {
|
|||||||
return address;
|
return address;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getFileTransferServerAddress(context) {
|
function getFileTransferServerAddress (context) {
|
||||||
var platformJsonFile = path.join(context.opts.projectRoot, 'platforms', context.opts.platforms[0], context.opts.platforms[0] + '.json');
|
var platformJsonFile = path.join(
|
||||||
|
context.opts.projectRoot,
|
||||||
|
'platforms',
|
||||||
|
context.opts.platforms[0],
|
||||||
|
context.opts.platforms[0] + '.json'
|
||||||
|
);
|
||||||
var platformJson = JSON.parse(fs.readFileSync(platformJsonFile, 'utf8'));
|
var platformJson = JSON.parse(fs.readFileSync(platformJsonFile, 'utf8'));
|
||||||
|
|
||||||
if (platformJson && platformJson.installed_plugins && platformJson.installed_plugins['cordova-plugin-file-transfer-tests'] && platformJson.installed_plugins['cordova-plugin-file-transfer-tests'].FILETRANSFER_SERVER_ADDRESS) {
|
if (
|
||||||
|
platformJson &&
|
||||||
|
platformJson.installed_plugins &&
|
||||||
|
platformJson.installed_plugins['cordova-plugin-file-transfer-tests'] &&
|
||||||
|
platformJson.installed_plugins['cordova-plugin-file-transfer-tests'].FILETRANSFER_SERVER_ADDRESS
|
||||||
|
) {
|
||||||
return platformJson.installed_plugins['cordova-plugin-file-transfer-tests'].FILETRANSFER_SERVER_ADDRESS;
|
return platformJson.installed_plugins['cordova-plugin-file-transfer-tests'].FILETRANSFER_SERVER_ADDRESS;
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function writeFileTransferOptions(address, context) {
|
function writeFileTransferOptions (address, context) {
|
||||||
for (var p in context.opts.paths) {
|
for (var p in context.opts.paths) {
|
||||||
var ftOpts = {
|
var ftOpts = {
|
||||||
serverAddress: address
|
serverAddress: address
|
||||||
@@ -71,5 +83,4 @@ module.exports = function(context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
main();
|
main();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
2777
tests/tests.js
2777
tests/tests.js
File diff suppressed because it is too large
Load Diff
@@ -17,16 +17,16 @@
|
|||||||
* specific language governing permissions and limitations
|
* specific language governing permissions and limitations
|
||||||
* under the License.
|
* under the License.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* global cordova, FileSystem */
|
/* global cordova, FileSystem */
|
||||||
|
|
||||||
var argscheck = require('cordova/argscheck'),
|
var argscheck = require('cordova/argscheck');
|
||||||
exec = require('cordova/exec'),
|
var exec = require('cordova/exec');
|
||||||
FileTransferError = require('./FileTransferError'),
|
var FileTransferError = require('./FileTransferError');
|
||||||
ProgressEvent = require('cordova-plugin-file.ProgressEvent');
|
var ProgressEvent = require('cordova-plugin-file.ProgressEvent');
|
||||||
|
|
||||||
function newProgressEvent(result) {
|
function newProgressEvent (result) {
|
||||||
var pe = new ProgressEvent();
|
var pe = new ProgressEvent();
|
||||||
pe.lengthComputable = result.lengthComputable;
|
pe.lengthComputable = result.lengthComputable;
|
||||||
pe.loaded = result.loaded;
|
pe.loaded = result.loaded;
|
||||||
@@ -34,16 +34,15 @@ function newProgressEvent(result) {
|
|||||||
return pe;
|
return pe;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getUrlCredentials(urlString) {
|
function getUrlCredentials (urlString) {
|
||||||
var credentialsPattern = /^https?\:\/\/(?:(?:(([^:@\/]*)(?::([^@\/]*))?)?@)?([^:\/?#]*)(?::(\d*))?).*$/,
|
var credentialsPattern = /^https?:\/\/(?:(?:(([^:@/]*)(?::([^@/]*))?)?@)?([^:/?#]*)(?::(\d*))?).*$/;
|
||||||
credentials = credentialsPattern.exec(urlString);
|
var credentials = credentialsPattern.exec(urlString);
|
||||||
|
|
||||||
return credentials && credentials[1];
|
return credentials && credentials[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
function getBasicAuthHeader(urlString) {
|
function getBasicAuthHeader (urlString) {
|
||||||
var header = null;
|
var header = null;
|
||||||
|
|
||||||
|
|
||||||
// This is changed due to MS Windows doesn't support credentials in http uris
|
// 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
|
// so we detect them by regexp and strip off from result url
|
||||||
@@ -52,12 +51,12 @@ function getBasicAuthHeader(urlString) {
|
|||||||
if (window.btoa) {
|
if (window.btoa) {
|
||||||
var credentials = getUrlCredentials(urlString);
|
var credentials = getUrlCredentials(urlString);
|
||||||
if (credentials) {
|
if (credentials) {
|
||||||
var authHeader = "Authorization";
|
var authHeader = 'Authorization';
|
||||||
var authHeaderValue = "Basic " + window.btoa(credentials);
|
var authHeaderValue = 'Basic ' + window.btoa(credentials);
|
||||||
|
|
||||||
header = {
|
header = {
|
||||||
name : authHeader,
|
name: authHeader,
|
||||||
value : authHeaderValue
|
value: authHeaderValue
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -65,10 +64,10 @@ function getBasicAuthHeader(urlString) {
|
|||||||
return header;
|
return header;
|
||||||
}
|
}
|
||||||
|
|
||||||
function convertHeadersToArray(headers) {
|
function convertHeadersToArray (headers) {
|
||||||
var result = [];
|
var result = [];
|
||||||
for (var header in headers) {
|
for (var header in headers) {
|
||||||
if (headers.hasOwnProperty(header)) {
|
if (Object.prototype.hasOwnProperty.call(headers, header)) {
|
||||||
var headerValue = headers[header];
|
var headerValue = headers[header];
|
||||||
result.push({
|
result.push({
|
||||||
name: header,
|
name: header,
|
||||||
@@ -85,22 +84,22 @@ var idCounter = 0;
|
|||||||
* FileTransfer uploads a file to a remote server.
|
* FileTransfer uploads a file to a remote server.
|
||||||
* @constructor
|
* @constructor
|
||||||
*/
|
*/
|
||||||
var FileTransfer = function() {
|
var FileTransfer = function () {
|
||||||
this._id = ++idCounter;
|
this._id = ++idCounter;
|
||||||
this.onprogress = null; // optional callback
|
this.onprogress = null; // optional callback
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Given an absolute file path, uploads a file on the device to a remote server
|
* Given an absolute file path, uploads a file on the device to a remote server
|
||||||
* using a multipart HTTP request.
|
* using a multipart HTTP request.
|
||||||
* @param filePath {String} Full path of the file on the device
|
* @param filePath {String} Full path of the file on the device
|
||||||
* @param server {String} URL of the server to receive the file
|
* @param server {String} URL of the server to receive the file
|
||||||
* @param successCallback (Function} Callback to be invoked when upload has completed
|
* @param successCallback (Function} Callback to be invoked when upload has completed
|
||||||
* @param errorCallback {Function} Callback to be invoked upon error
|
* @param errorCallback {Function} Callback to be invoked upon error
|
||||||
* @param options {FileUploadOptions} Optional parameters such as file name and mimetype
|
* @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
|
* @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) {
|
||||||
argscheck.checkArgs('ssFFO*', 'FileTransfer.upload', arguments);
|
argscheck.checkArgs('ssFFO*', 'FileTransfer.upload', arguments);
|
||||||
// check for options
|
// check for options
|
||||||
var fileKey = null;
|
var fileKey = null;
|
||||||
@@ -124,36 +123,37 @@ FileTransfer.prototype.upload = function(filePath, server, successCallback, erro
|
|||||||
fileName = options.fileName;
|
fileName = options.fileName;
|
||||||
mimeType = options.mimeType;
|
mimeType = options.mimeType;
|
||||||
headers = options.headers;
|
headers = options.headers;
|
||||||
httpMethod = options.httpMethod || "POST";
|
httpMethod = options.httpMethod || 'POST';
|
||||||
if (httpMethod.toUpperCase() == "PUT"){
|
if (httpMethod.toUpperCase() === 'PUT') {
|
||||||
httpMethod = "PUT";
|
httpMethod = 'PUT';
|
||||||
} else {
|
} else {
|
||||||
httpMethod = "POST";
|
httpMethod = 'POST';
|
||||||
}
|
}
|
||||||
if (options.chunkedMode !== null || typeof options.chunkedMode != "undefined") {
|
if (options.chunkedMode !== null || typeof options.chunkedMode !== 'undefined') {
|
||||||
chunkedMode = options.chunkedMode;
|
chunkedMode = options.chunkedMode;
|
||||||
}
|
}
|
||||||
if (options.params) {
|
if (options.params) {
|
||||||
params = options.params;
|
params = options.params;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
params = {};
|
params = {};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cordova.platformId === "windowsphone") {
|
if (cordova.platformId === 'windowsphone') {
|
||||||
headers = headers && convertHeadersToArray(headers);
|
headers = headers && convertHeadersToArray(headers);
|
||||||
params = params && convertHeadersToArray(params);
|
params = params && convertHeadersToArray(params);
|
||||||
}
|
}
|
||||||
|
|
||||||
var fail = errorCallback && function(e) {
|
var fail =
|
||||||
var error = new FileTransferError(e.code, e.source, e.target, e.http_status, e.body, e.exception);
|
errorCallback &&
|
||||||
errorCallback(error);
|
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 self = this;
|
||||||
var win = function(result) {
|
var win = function (result) {
|
||||||
if (typeof result.lengthComputable != "undefined") {
|
if (typeof result.lengthComputable !== 'undefined') {
|
||||||
if (self.onprogress) {
|
if (self.onprogress) {
|
||||||
self.onprogress(newProgressEvent(result));
|
self.onprogress(newProgressEvent(result));
|
||||||
}
|
}
|
||||||
@@ -163,7 +163,19 @@ FileTransfer.prototype.upload = function(filePath, server, successCallback, erro
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
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
|
||||||
|
]);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -175,7 +187,7 @@ FileTransfer.prototype.upload = function(filePath, server, successCallback, erro
|
|||||||
* @param trustAllHosts {Boolean} Optional trust all hosts (e.g. for self-signed certs), defaults to false
|
* @param trustAllHosts {Boolean} Optional trust all hosts (e.g. for self-signed certs), defaults to false
|
||||||
* @param options {FileDownloadOptions} Optional parameters such as headers
|
* @param options {FileDownloadOptions} Optional parameters such as headers
|
||||||
*/
|
*/
|
||||||
FileTransfer.prototype.download = function(source, target, successCallback, errorCallback, trustAllHosts, options) {
|
FileTransfer.prototype.download = function (source, target, successCallback, errorCallback, trustAllHosts, options) {
|
||||||
argscheck.checkArgs('ssFF*', 'FileTransfer.download', arguments);
|
argscheck.checkArgs('ssFF*', 'FileTransfer.download', arguments);
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
@@ -193,12 +205,12 @@ FileTransfer.prototype.download = function(source, target, successCallback, erro
|
|||||||
headers = options.headers || null;
|
headers = options.headers || null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cordova.platformId === "windowsphone" && headers) {
|
if (cordova.platformId === 'windowsphone' && headers) {
|
||||||
headers = convertHeadersToArray(headers);
|
headers = convertHeadersToArray(headers);
|
||||||
}
|
}
|
||||||
|
|
||||||
var win = function(result) {
|
var win = function (result) {
|
||||||
if (typeof result.lengthComputable != "undefined") {
|
if (typeof result.lengthComputable !== 'undefined') {
|
||||||
if (self.onprogress) {
|
if (self.onprogress) {
|
||||||
return self.onprogress(newProgressEvent(result));
|
return self.onprogress(newProgressEvent(result));
|
||||||
}
|
}
|
||||||
@@ -206,24 +218,27 @@ FileTransfer.prototype.download = function(source, target, successCallback, erro
|
|||||||
var entry = null;
|
var entry = null;
|
||||||
if (result.isDirectory) {
|
if (result.isDirectory) {
|
||||||
entry = new (require('cordova-plugin-file.DirectoryEntry'))();
|
entry = new (require('cordova-plugin-file.DirectoryEntry'))();
|
||||||
}
|
} else if (result.isFile) {
|
||||||
else if (result.isFile) {
|
|
||||||
entry = new (require('cordova-plugin-file.FileEntry'))();
|
entry = new (require('cordova-plugin-file.FileEntry'))();
|
||||||
}
|
}
|
||||||
entry.isDirectory = result.isDirectory;
|
entry.isDirectory = result.isDirectory;
|
||||||
entry.isFile = result.isFile;
|
entry.isFile = result.isFile;
|
||||||
entry.name = result.name;
|
entry.name = result.name;
|
||||||
entry.fullPath = result.fullPath;
|
entry.fullPath = result.fullPath;
|
||||||
entry.filesystem = new FileSystem(result.filesystemName || (result.filesystem == window.PERSISTENT ? 'persistent' : 'temporary'));
|
entry.filesystem = new FileSystem(
|
||||||
|
result.filesystemName || (result.filesystem === window.PERSISTENT ? 'persistent' : 'temporary')
|
||||||
|
);
|
||||||
entry.nativeURL = result.nativeURL;
|
entry.nativeURL = result.nativeURL;
|
||||||
successCallback(entry);
|
successCallback(entry);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var fail = errorCallback && function(e) {
|
var fail =
|
||||||
var error = new FileTransferError(e.code, e.source, e.target, e.http_status, e.body, e.exception);
|
errorCallback &&
|
||||||
errorCallback(error);
|
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]);
|
exec(win, fail, 'FileTransfer', 'download', [source, target, trustAllHosts, this._id, headers]);
|
||||||
};
|
};
|
||||||
@@ -232,7 +247,7 @@ FileTransfer.prototype.download = function(source, target, successCallback, erro
|
|||||||
* Aborts the ongoing file transfer on this object. The original error
|
* Aborts the ongoing file transfer on this object. The original error
|
||||||
* callback for the file transfer will be called if necessary.
|
* callback for the file transfer will be called if necessary.
|
||||||
*/
|
*/
|
||||||
FileTransfer.prototype.abort = function() {
|
FileTransfer.prototype.abort = function () {
|
||||||
exec(null, null, 'FileTransfer', 'abort', [this._id]);
|
exec(null, null, 'FileTransfer', 'abort', [this._id]);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -17,13 +17,13 @@
|
|||||||
* specific language governing permissions and limitations
|
* specific language governing permissions and limitations
|
||||||
* under the License.
|
* under the License.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* FileTransferError
|
* FileTransferError
|
||||||
* @constructor
|
* @constructor
|
||||||
*/
|
*/
|
||||||
var FileTransferError = function(code, source, target, status, body, exception) {
|
var FileTransferError = function (code, source, target, status, body, exception) {
|
||||||
this.code = code || null;
|
this.code = code || null;
|
||||||
this.source = source || null;
|
this.source = source || null;
|
||||||
this.target = target || null;
|
this.target = target || null;
|
||||||
|
|||||||
@@ -17,33 +17,32 @@
|
|||||||
* specific language governing permissions and limitations
|
* specific language governing permissions and limitations
|
||||||
* under the License.
|
* under the License.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*global module, require*/
|
/* global FileUploadResult */
|
||||||
|
|
||||||
var argscheck = require('cordova/argscheck'),
|
var argscheck = require('cordova/argscheck');
|
||||||
FileTransferError = require('./FileTransferError');
|
var FileTransferError = require('./FileTransferError');
|
||||||
|
|
||||||
function getParentPath(filePath) {
|
function getParentPath (filePath) {
|
||||||
var pos = filePath.lastIndexOf('/');
|
var pos = filePath.lastIndexOf('/');
|
||||||
return filePath.substring(0, pos + 1);
|
return filePath.substring(0, pos + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getFileName(filePath) {
|
function getFileName (filePath) {
|
||||||
var pos = filePath.lastIndexOf('/');
|
var pos = filePath.lastIndexOf('/');
|
||||||
return filePath.substring(pos + 1);
|
return filePath.substring(pos + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getUrlCredentials(urlString) {
|
function getUrlCredentials (urlString) {
|
||||||
var credentialsPattern = /^https?\:\/\/(?:(?:(([^:@\/]*)(?::([^@\/]*))?)?@)?([^:\/?#]*)(?::(\d*))?).*$/,
|
var credentialsPattern = /^https?:\/\/(?:(?:(([^:@/]*)(?::([^@/]*))?)?@)?([^:/?#]*)(?::(\d*))?).*$/;
|
||||||
credentials = credentialsPattern.exec(urlString);
|
var credentials = credentialsPattern.exec(urlString);
|
||||||
|
|
||||||
return credentials && credentials[1];
|
return credentials && credentials[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
function getBasicAuthHeader(urlString) {
|
function getBasicAuthHeader (urlString) {
|
||||||
var header = null;
|
var header = null;
|
||||||
|
|
||||||
|
|
||||||
// This is changed due to MS Windows doesn't support credentials in http uris
|
// 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
|
// so we detect them by regexp and strip off from result url
|
||||||
@@ -52,12 +51,12 @@ function getBasicAuthHeader(urlString) {
|
|||||||
if (window.btoa) {
|
if (window.btoa) {
|
||||||
var credentials = getUrlCredentials(urlString);
|
var credentials = getUrlCredentials(urlString);
|
||||||
if (credentials) {
|
if (credentials) {
|
||||||
var authHeader = "Authorization";
|
var authHeader = 'Authorization';
|
||||||
var authHeaderValue = "Basic " + window.btoa(credentials);
|
var authHeaderValue = 'Basic ' + window.btoa(credentials);
|
||||||
|
|
||||||
header = {
|
header = {
|
||||||
name : authHeader,
|
name: authHeader,
|
||||||
value : authHeaderValue
|
value: authHeaderValue
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -65,8 +64,8 @@ function getBasicAuthHeader(urlString) {
|
|||||||
return header;
|
return header;
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkURL(url) {
|
function checkURL (url) {
|
||||||
return url.indexOf(' ') === -1 ? true : false;
|
return url.indexOf(' ') === -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
var idCounter = 0;
|
var idCounter = 0;
|
||||||
@@ -77,7 +76,7 @@ var transfers = {};
|
|||||||
* FileTransfer uploads a file to a remote server.
|
* FileTransfer uploads a file to a remote server.
|
||||||
* @constructor
|
* @constructor
|
||||||
*/
|
*/
|
||||||
var FileTransfer = function() {
|
var FileTransfer = function () {
|
||||||
this._id = ++idCounter;
|
this._id = ++idCounter;
|
||||||
this.onprogress = null; // optional callback
|
this.onprogress = null; // optional callback
|
||||||
};
|
};
|
||||||
@@ -92,7 +91,7 @@ var FileTransfer = function() {
|
|||||||
* @param options {FileUploadOptions} Optional parameters such as file name and mimetype
|
* @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
|
* @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) {
|
FileTransfer.prototype.upload = function (filePath, server, successCallback, errorCallback, options) {
|
||||||
// check for arguments
|
// check for arguments
|
||||||
argscheck.checkArgs('ssFFO*', 'FileTransfer.upload', arguments);
|
argscheck.checkArgs('ssFFO*', 'FileTransfer.upload', arguments);
|
||||||
|
|
||||||
@@ -107,14 +106,14 @@ FileTransfer.prototype.upload = function(filePath, server, successCallback, erro
|
|||||||
|
|
||||||
options = options || {};
|
options = options || {};
|
||||||
|
|
||||||
var fileKey = options.fileKey || "file";
|
var fileKey = options.fileKey || 'file';
|
||||||
var fileName = options.fileName || "image.jpg";
|
var fileName = options.fileName || 'image.jpg';
|
||||||
var mimeType = options.mimeType || "image/jpeg";
|
var mimeType = options.mimeType || 'image/jpeg';
|
||||||
var params = options.params || {};
|
var params = options.params || {};
|
||||||
var withCredentials = options.withCredentials || false;
|
var withCredentials = options.withCredentials || false;
|
||||||
// var chunkedMode = !!options.chunkedMode; // Not supported
|
// var chunkedMode = !!options.chunkedMode; // Not supported
|
||||||
var headers = options.headers || {};
|
var headers = options.headers || {};
|
||||||
var httpMethod = options.httpMethod && options.httpMethod.toUpperCase() === "PUT" ? "PUT" : "POST";
|
var httpMethod = options.httpMethod && options.httpMethod.toUpperCase() === 'PUT' ? 'PUT' : 'POST';
|
||||||
|
|
||||||
var basicAuthHeader = getBasicAuthHeader(server);
|
var basicAuthHeader = getBasicAuthHeader(server);
|
||||||
if (basicAuthHeader) {
|
if (basicAuthHeader) {
|
||||||
@@ -123,93 +122,101 @@ FileTransfer.prototype.upload = function(filePath, server, successCallback, erro
|
|||||||
}
|
}
|
||||||
|
|
||||||
var that = this;
|
var that = this;
|
||||||
var xhr = transfers[this._id] = new XMLHttpRequest();
|
var xhr = (transfers[this._id] = new XMLHttpRequest());
|
||||||
xhr.withCredentials = withCredentials;
|
xhr.withCredentials = withCredentials;
|
||||||
|
|
||||||
var fail = errorCallback && function(code, status, response) {
|
var fail =
|
||||||
if (transfers[this._id]) {
|
errorCallback &&
|
||||||
delete transfers[this._id];
|
function (code, status, response) {
|
||||||
}
|
if (transfers[this._id]) {
|
||||||
var error = new FileTransferError(code, filePath, server, status, response);
|
delete transfers[this._id];
|
||||||
if (errorCallback) {
|
}
|
||||||
errorCallback(error);
|
var error = new FileTransferError(code, filePath, server, status, response);
|
||||||
}
|
if (errorCallback) {
|
||||||
};
|
errorCallback(error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
window.resolveLocalFileSystemURL(filePath, function(entry) {
|
window.resolveLocalFileSystemURL(
|
||||||
entry.file(function(file) {
|
filePath,
|
||||||
var reader = new FileReader();
|
function (entry) {
|
||||||
reader.onloadend = function() {
|
entry.file(
|
||||||
var blob = new Blob([this.result], {type: mimeType});
|
function (file) {
|
||||||
|
var reader = new FileReader();
|
||||||
|
reader.onloadend = function () {
|
||||||
|
var blob = new Blob([this.result], { type: mimeType });
|
||||||
|
|
||||||
// Prepare form data to send to server
|
// Prepare form data to send to server
|
||||||
var fd = new FormData();
|
var fd = new FormData();
|
||||||
fd.append(fileKey, blob, fileName);
|
fd.append(fileKey, blob, fileName);
|
||||||
for (var prop in params) {
|
for (var prop in params) {
|
||||||
if (params.hasOwnProperty(prop)) {
|
if (Object.prototype.hasOwnProperty.call(params, prop)) {
|
||||||
fd.append(prop, params[prop]);
|
fd.append(prop, params[prop]);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
xhr.open(httpMethod, server);
|
||||||
|
|
||||||
|
// Fill XHR headers
|
||||||
|
for (var header in headers) {
|
||||||
|
if (Object.prototype.hasOwnProperty.call(headers, header)) {
|
||||||
|
xhr.setRequestHeader(header, headers[header]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
xhr.onload = function () {
|
||||||
|
// 2xx codes are valid
|
||||||
|
if (this.status >= 200 && this.status < 300) {
|
||||||
|
var result = new FileUploadResult();
|
||||||
|
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) {
|
||||||
|
if (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);
|
||||||
}
|
}
|
||||||
|
);
|
||||||
xhr.open(httpMethod, server);
|
},
|
||||||
|
function () {
|
||||||
// Fill XHR headers
|
|
||||||
for (var header in headers) {
|
|
||||||
if (headers.hasOwnProperty(header)) {
|
|
||||||
xhr.setRequestHeader(header, headers[header]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
xhr.onload = function() {
|
|
||||||
// 2xx codes are valid
|
|
||||||
if (this.status >= 200 &&
|
|
||||||
this.status < 300) {
|
|
||||||
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) {
|
|
||||||
if (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);
|
fail(FileTransferError.FILE_NOT_FOUND_ERR);
|
||||||
});
|
}
|
||||||
}, function() {
|
);
|
||||||
fail(FileTransferError.FILE_NOT_FOUND_ERR);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -221,7 +228,7 @@ FileTransfer.prototype.upload = function(filePath, server, successCallback, erro
|
|||||||
* @param trustAllHosts {Boolean} Optional trust all hosts (e.g. for self-signed certs), defaults to false
|
* @param trustAllHosts {Boolean} Optional trust all hosts (e.g. for self-signed certs), defaults to false
|
||||||
* @param options {FileDownloadOptions} Optional parameters such as headers
|
* @param options {FileDownloadOptions} Optional parameters such as headers
|
||||||
*/
|
*/
|
||||||
FileTransfer.prototype.download = function(source, target, successCallback, errorCallback, trustAllHosts, options) {
|
FileTransfer.prototype.download = function (source, target, successCallback, errorCallback, trustAllHosts, options) {
|
||||||
argscheck.checkArgs('ssFF*', 'FileTransfer.download', arguments);
|
argscheck.checkArgs('ssFF*', 'FileTransfer.download', arguments);
|
||||||
|
|
||||||
// Check if target URL doesn't contain spaces. If contains, it should be escaped first
|
// Check if target URL doesn't contain spaces. If contains, it should be escaped first
|
||||||
@@ -234,7 +241,7 @@ FileTransfer.prototype.download = function(source, target, successCallback, erro
|
|||||||
}
|
}
|
||||||
|
|
||||||
options = options || {};
|
options = options || {};
|
||||||
|
|
||||||
var headers = options.headers || {};
|
var headers = options.headers || {};
|
||||||
var withCredentials = options.withCredentials || false;
|
var withCredentials = options.withCredentials || false;
|
||||||
|
|
||||||
@@ -245,29 +252,30 @@ FileTransfer.prototype.download = function(source, target, successCallback, erro
|
|||||||
}
|
}
|
||||||
|
|
||||||
var that = this;
|
var that = this;
|
||||||
var xhr = transfers[this._id] = new XMLHttpRequest();
|
var xhr = (transfers[this._id] = new XMLHttpRequest());
|
||||||
xhr.withCredentials = withCredentials;
|
xhr.withCredentials = withCredentials;
|
||||||
var fail = errorCallback && function(code, status, response) {
|
var fail =
|
||||||
if (transfers[that._id]) {
|
errorCallback &&
|
||||||
delete transfers[that._id];
|
function (code, status, response) {
|
||||||
}
|
if (transfers[that._id]) {
|
||||||
// In XHR GET reqests we're setting response type to Blob
|
delete transfers[that._id];
|
||||||
// but in case of error we need to raise event with plain text response
|
}
|
||||||
if (response instanceof Blob) {
|
// In XHR GET reqests we're setting response type to Blob
|
||||||
var reader = new FileReader();
|
// but in case of error we need to raise event with plain text response
|
||||||
reader.readAsText(response);
|
if (response instanceof Blob) {
|
||||||
reader.onloadend = function(e) {
|
var reader = new FileReader();
|
||||||
var error = new FileTransferError(code, source, target, status, e.target.result);
|
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);
|
errorCallback(error);
|
||||||
};
|
}
|
||||||
} else {
|
};
|
||||||
var error = new FileTransferError(code, source, target, status, response);
|
|
||||||
errorCallback(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
xhr.onload = function (e) {
|
xhr.onload = function (e) {
|
||||||
|
|
||||||
var fileNotFound = function () {
|
var fileNotFound = function () {
|
||||||
fail(FileTransferError.FILE_NOT_FOUND_ERR);
|
fail(FileTransferError.FILE_NOT_FOUND_ERR);
|
||||||
};
|
};
|
||||||
@@ -275,27 +283,36 @@ FileTransfer.prototype.download = function(source, target, successCallback, erro
|
|||||||
var req = e.target;
|
var req = e.target;
|
||||||
// req.status === 0 is special case for local files with file:// URI scheme
|
// req.status === 0 is special case for local files with file:// URI scheme
|
||||||
if ((req.status === 200 || req.status === 0) && req.response) {
|
if ((req.status === 200 || req.status === 0) && req.response) {
|
||||||
window.resolveLocalFileSystemURL(getParentPath(target), function (dir) {
|
window.resolveLocalFileSystemURL(
|
||||||
dir.getFile(getFileName(target), {create: true}, function writeFile(entry) {
|
getParentPath(target),
|
||||||
entry.createWriter(function (fileWriter) {
|
function (dir) {
|
||||||
fileWriter.onwriteend = function (evt) {
|
dir.getFile(
|
||||||
if (!evt.target.error) {
|
getFileName(target),
|
||||||
entry.filesystemName = entry.filesystem.name;
|
{ create: true },
|
||||||
delete transfers[that._id];
|
function writeFile (entry) {
|
||||||
if (successCallback) {
|
entry.createWriter(function (fileWriter) {
|
||||||
successCallback(entry);
|
fileWriter.onwriteend = function (evt) {
|
||||||
}
|
if (!evt.target.error) {
|
||||||
} else {
|
entry.filesystemName = entry.filesystem.name;
|
||||||
fail(FileTransferError.FILE_NOT_FOUND_ERR);
|
delete transfers[that._id];
|
||||||
}
|
if (successCallback) {
|
||||||
};
|
successCallback(entry);
|
||||||
fileWriter.onerror = function () {
|
}
|
||||||
fail(FileTransferError.FILE_NOT_FOUND_ERR);
|
} else {
|
||||||
};
|
fail(FileTransferError.FILE_NOT_FOUND_ERR);
|
||||||
fileWriter.write(req.response);
|
}
|
||||||
}, fileNotFound);
|
};
|
||||||
}, fileNotFound);
|
fileWriter.onerror = function () {
|
||||||
}, fileNotFound);
|
fail(FileTransferError.FILE_NOT_FOUND_ERR);
|
||||||
|
};
|
||||||
|
fileWriter.write(req.response);
|
||||||
|
}, fileNotFound);
|
||||||
|
},
|
||||||
|
fileNotFound
|
||||||
|
);
|
||||||
|
},
|
||||||
|
fileNotFound
|
||||||
|
);
|
||||||
} else if (req.status === 404) {
|
} else if (req.status === 404) {
|
||||||
fail(FileTransferError.INVALID_URL_ERR, req.status, req.response);
|
fail(FileTransferError.INVALID_URL_ERR, req.status, req.response);
|
||||||
} else {
|
} else {
|
||||||
@@ -317,15 +334,15 @@ FileTransfer.prototype.download = function(source, target, successCallback, erro
|
|||||||
fail(FileTransferError.ABORT_ERR, this.status, this.response);
|
fail(FileTransferError.ABORT_ERR, this.status, this.response);
|
||||||
};
|
};
|
||||||
|
|
||||||
xhr.open("GET", source, true);
|
xhr.open('GET', source, true);
|
||||||
|
|
||||||
for (var header in headers) {
|
for (var header in headers) {
|
||||||
if (headers.hasOwnProperty(header)) {
|
if (Object.prototype.hasOwnProperty.call(headers, header)) {
|
||||||
xhr.setRequestHeader(header, headers[header]);
|
xhr.setRequestHeader(header, headers[header]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
xhr.responseType = "blob";
|
xhr.responseType = 'blob';
|
||||||
|
|
||||||
xhr.send();
|
xhr.send();
|
||||||
};
|
};
|
||||||
@@ -334,7 +351,7 @@ FileTransfer.prototype.download = function(source, target, successCallback, erro
|
|||||||
* Aborts the ongoing file transfer on this object. The original error
|
* Aborts the ongoing file transfer on this object. The original error
|
||||||
* callback for the file transfer will be called if necessary.
|
* callback for the file transfer will be called if necessary.
|
||||||
*/
|
*/
|
||||||
FileTransfer.prototype.abort = function() {
|
FileTransfer.prototype.abort = function () {
|
||||||
if (this instanceof FileTransfer) {
|
if (this instanceof FileTransfer) {
|
||||||
if (transfers[this._id]) {
|
if (transfers[this._id]) {
|
||||||
transfers[this._id].abort();
|
transfers[this._id].abort();
|
||||||
|
|||||||
Reference in New Issue
Block a user