mirror of
https://github.com/apache/cordova-plugin-file-transfer.git
synced 2026-04-28 00:02:49 +08:00
chore: eslint upgrade (#325)
This commit is contained in:
+43
-43
@@ -21,38 +21,38 @@
|
||||
|
||||
/* global FileUploadResult */
|
||||
|
||||
var argscheck = require('cordova/argscheck');
|
||||
var FileTransferError = require('./FileTransferError');
|
||||
const argscheck = require('cordova/argscheck');
|
||||
const FileTransferError = require('./FileTransferError');
|
||||
|
||||
function getParentPath (filePath) {
|
||||
var pos = filePath.lastIndexOf('/');
|
||||
const pos = filePath.lastIndexOf('/');
|
||||
return filePath.substring(0, pos + 1);
|
||||
}
|
||||
|
||||
function getFileName (filePath) {
|
||||
var pos = filePath.lastIndexOf('/');
|
||||
const pos = filePath.lastIndexOf('/');
|
||||
return filePath.substring(pos + 1);
|
||||
}
|
||||
|
||||
function getUrlCredentials (urlString) {
|
||||
var credentialsPattern = /^https?:\/\/(?:(?:(([^:@/]*)(?::([^@/]*))?)?@)?([^:/?#]*)(?::(\d*))?).*$/;
|
||||
var credentials = credentialsPattern.exec(urlString);
|
||||
const credentialsPattern = /^https?:\/\/(?:(?:(([^:@/]*)(?::([^@/]*))?)?@)?([^:/?#]*)(?::(\d*))?).*$/;
|
||||
const credentials = credentialsPattern.exec(urlString);
|
||||
|
||||
return credentials && credentials[1];
|
||||
}
|
||||
|
||||
function getBasicAuthHeader (urlString) {
|
||||
var header = null;
|
||||
let 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);
|
||||
const credentials = getUrlCredentials(urlString);
|
||||
if (credentials) {
|
||||
var authHeader = 'Authorization';
|
||||
var authHeaderValue = 'Basic ' + window.btoa(credentials);
|
||||
const authHeader = 'Authorization';
|
||||
const authHeaderValue = 'Basic ' + window.btoa(credentials);
|
||||
|
||||
header = {
|
||||
name: authHeader,
|
||||
@@ -68,15 +68,15 @@ function checkURL (url) {
|
||||
return url.indexOf(' ') === -1;
|
||||
}
|
||||
|
||||
var idCounter = 0;
|
||||
let idCounter = 0;
|
||||
|
||||
var transfers = {};
|
||||
const transfers = {};
|
||||
|
||||
/**
|
||||
* FileTransfer uploads a file to a remote server.
|
||||
* @constructor
|
||||
*/
|
||||
var FileTransfer = function () {
|
||||
const FileTransfer = function () {
|
||||
this._id = ++idCounter;
|
||||
this.onprogress = null; // optional callback
|
||||
};
|
||||
@@ -106,32 +106,32 @@ FileTransfer.prototype.upload = function (filePath, server, successCallback, err
|
||||
|
||||
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;
|
||||
const fileKey = options.fileKey || 'file';
|
||||
const fileName = options.fileName || 'image.jpg';
|
||||
const mimeType = options.mimeType || 'image/jpeg';
|
||||
const params = options.params || {};
|
||||
const withCredentials = options.withCredentials || false;
|
||||
// var chunkedMode = !!options.chunkedMode; // Not supported
|
||||
var headers = options.headers || {};
|
||||
var httpMethod = options.httpMethod && options.httpMethod.toUpperCase() === 'PUT' ? 'PUT' : 'POST';
|
||||
const headers = options.headers || {};
|
||||
const httpMethod = options.httpMethod && options.httpMethod.toUpperCase() === 'PUT' ? 'PUT' : 'POST';
|
||||
|
||||
var basicAuthHeader = getBasicAuthHeader(server);
|
||||
const 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());
|
||||
const that = this;
|
||||
const xhr = (transfers[this._id] = new XMLHttpRequest());
|
||||
xhr.withCredentials = withCredentials;
|
||||
|
||||
var fail =
|
||||
const fail =
|
||||
errorCallback &&
|
||||
function (code, status, response) {
|
||||
if (transfers[this._id]) {
|
||||
delete transfers[this._id];
|
||||
}
|
||||
var error = new FileTransferError(code, filePath, server, status, response);
|
||||
const error = new FileTransferError(code, filePath, server, status, response);
|
||||
if (errorCallback) {
|
||||
errorCallback(error);
|
||||
}
|
||||
@@ -142,14 +142,14 @@ FileTransfer.prototype.upload = function (filePath, server, successCallback, err
|
||||
function (entry) {
|
||||
entry.file(
|
||||
function (file) {
|
||||
var reader = new FileReader();
|
||||
const reader = new FileReader();
|
||||
reader.onloadend = function () {
|
||||
var blob = new Blob([this.result], { type: mimeType });
|
||||
const blob = new Blob([this.result], { type: mimeType });
|
||||
|
||||
// Prepare form data to send to server
|
||||
var fd = new FormData();
|
||||
const fd = new FormData();
|
||||
fd.append(fileKey, blob, fileName);
|
||||
for (var prop in params) {
|
||||
for (const prop in params) {
|
||||
if (Object.prototype.hasOwnProperty.call(params, prop)) {
|
||||
fd.append(prop, params[prop]);
|
||||
}
|
||||
@@ -158,7 +158,7 @@ FileTransfer.prototype.upload = function (filePath, server, successCallback, err
|
||||
xhr.open(httpMethod, server);
|
||||
|
||||
// Fill XHR headers
|
||||
for (var header in headers) {
|
||||
for (const header in headers) {
|
||||
if (Object.prototype.hasOwnProperty.call(headers, header)) {
|
||||
xhr.setRequestHeader(header, headers[header]);
|
||||
}
|
||||
@@ -167,7 +167,7 @@ FileTransfer.prototype.upload = function (filePath, server, successCallback, err
|
||||
xhr.onload = function () {
|
||||
// 2xx codes are valid
|
||||
if (this.status >= 200 && this.status < 300) {
|
||||
var result = new FileUploadResult();
|
||||
const result = new FileUploadResult();
|
||||
result.bytesSent = blob.size;
|
||||
result.responseCode = this.status;
|
||||
result.response = this.response;
|
||||
@@ -242,19 +242,19 @@ FileTransfer.prototype.download = function (source, target, successCallback, err
|
||||
|
||||
options = options || {};
|
||||
|
||||
var headers = options.headers || {};
|
||||
var withCredentials = options.withCredentials || false;
|
||||
const headers = options.headers || {};
|
||||
const withCredentials = options.withCredentials || false;
|
||||
|
||||
var basicAuthHeader = getBasicAuthHeader(source);
|
||||
const 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());
|
||||
const that = this;
|
||||
const xhr = (transfers[this._id] = new XMLHttpRequest());
|
||||
xhr.withCredentials = withCredentials;
|
||||
var fail =
|
||||
const fail =
|
||||
errorCallback &&
|
||||
function (code, status, response) {
|
||||
if (transfers[that._id]) {
|
||||
@@ -263,24 +263,24 @@ FileTransfer.prototype.download = function (source, target, successCallback, err
|
||||
// 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();
|
||||
const reader = new FileReader();
|
||||
reader.readAsText(response);
|
||||
reader.onloadend = function (e) {
|
||||
var error = new FileTransferError(code, source, target, status, e.target.result);
|
||||
const error = new FileTransferError(code, source, target, status, e.target.result);
|
||||
errorCallback(error);
|
||||
};
|
||||
} else {
|
||||
var error = new FileTransferError(code, source, target, status, response);
|
||||
const error = new FileTransferError(code, source, target, status, response);
|
||||
errorCallback(error);
|
||||
}
|
||||
};
|
||||
|
||||
xhr.onload = function (e) {
|
||||
var fileNotFound = function () {
|
||||
const fileNotFound = function () {
|
||||
fail(FileTransferError.FILE_NOT_FOUND_ERR);
|
||||
};
|
||||
|
||||
var req = e.target;
|
||||
const 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(
|
||||
@@ -336,7 +336,7 @@ FileTransfer.prototype.download = function (source, target, successCallback, err
|
||||
|
||||
xhr.open('GET', source, true);
|
||||
|
||||
for (var header in headers) {
|
||||
for (const header in headers) {
|
||||
if (Object.prototype.hasOwnProperty.call(headers, header)) {
|
||||
xhr.setRequestHeader(header, headers[header]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user