Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a new function and change some es6 features to es5 #86

Merged
merged 7 commits into from
Mar 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions source/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,19 @@ function createClient(remoteURL, username, password) {
return putFile.putFileContents(remoteFilename, data, putOptions);
},

/**
* Get the upload link
* Only supported for Basic authentication or unauthenticated connections.
* @param {String} remoteFilename The path of the remote file location
* @param {PutOptions=} options The options for the request
* @memberof ClientInterface
* @returns {String} A upload URL
*/
getFileUploadLink: function getFileUploadLink(remoteFilename, options) {
var putOptions = merge(baseOptions, options || {});
return putFile.getFileUploadLink(remoteFilename, putOptions);
},

/**
* Stat a remote object
* @param {String} remotePath The path of the item
Expand Down
18 changes: 17 additions & 1 deletion source/interface/putFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ function putFileContents(filePath, data, options) {
return fetch(fetchURL, fetchOptions).then(responseHandlers.handleResponseCode);
}

function getFileUploadLink(filePath, options) {
let fetchURL = joinURL(options.remoteURL, encodePath(filePath));
fetchURL += "?Content-Type=application/octet-stream";
const protocol = /^https:/i.test(fetchURL) ? "https" : "http";
if (options.headers.Authorization) {
if (/^Basic /i.test(options.headers.Authorization) === false) {
throw new Error("Failed retrieving download link: Invalid authorisation method");
}
const authPart = options.headers.Authorization.replace(/^Basic /i, "").trim();
const authContents = Buffer.from(authPart, "base64").toString("utf8");
fetchURL = fetchURL.replace(/^https?:\/\//, `${protocol}://${authContents}@`);
}
return fetchURL;
}

module.exports = {
putFileContents: putFileContents
getFileUploadLink,
putFileContents
};