Skip to content

Commit

Permalink
cherrypicked changes from tinode#24
Browse files Browse the repository at this point in the history
  • Loading branch information
or-else committed Apr 11, 2019
1 parent d14de68 commit 7f3a50c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 8 deletions.
32 changes: 29 additions & 3 deletions src/tinode.js
Original file line number Diff line number Diff line change
Expand Up @@ -4945,23 +4945,33 @@ var LargeFileHelper = function(tinode) {

LargeFileHelper.prototype = {
/**
* Start uploading the file.
* Start uploading the file to a non-default endpoint.
*
* @memberof Tinode.LargeFileHelper#
*
* @param {String} baseUrl alternative base URL of upload server.
* @param {File} file to upload
* @param {Callback} onProgress callback. Takes one {float} parameter 0..1
* @param {Callback} onSuccess callback. Called when the file is successfully uploaded.
* @param {Callback} onFailure callback. Called in case of a failure.
*
* @returns {Promise} resolved/rejected when the upload is completed/failed.
*/
upload: function(file, onProgress, onSuccess, onFailure) {
uploadWithBaseUrl: function(baseUrl, file, onProgress, onSuccess, onFailure) {
if (!this._authToken) {
throw new Error("Must authenticate first");
}
const instance = this;
this.xhr.open('POST', '/v' + PROTOCOL_VERSION + '/file/u/', true);

let url = '/v' + PROTOCOL_VERSION + '/file/u/';
if (baseUrl) {
if (baseUrl.indexOf('http://') == 0 || baseUrl.indexOf('https://') == 0) {
url = baseUrl + url;
} else {
throw new Error("Invalid base URL '" + baseUrl + "'");
}
}
this.xhr.open('POST', url, true);
this.xhr.setRequestHeader('X-Tinode-APIKey', this._apiKey);
this.xhr.setRequestHeader('X-Tinode-Auth', 'Token ' + this._authToken.token);
const result = new Promise((resolve, reject) => {
Expand Down Expand Up @@ -5041,6 +5051,22 @@ LargeFileHelper.prototype = {
return result;
},

/**
* Start uploading the file to default endpoint.
*
* @memberof Tinode.LargeFileHelper#
*
* @param {File} file to upload
* @param {Callback} onProgress callback. Takes one {float} parameter 0..1
* @param {Callback} onSuccess callback. Called when the file is successfully uploaded.
* @param {Callback} onFailure callback. Called in case of a failure.
*
* @returns {Promise} resolved/rejected when the upload is completed/failed.
*/
upload: function(file, onProgress, onSuccess, onFailure) {
return this.uploadWithBaseUrl(undefined, file, onProgress, onSuccess, onFailure);
},

/**
* Download the file from a given URL using GET request. This method works with the Tinode server only.
*
Expand Down
34 changes: 30 additions & 4 deletions umd/tinode.dev.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion umd/tinode.prod.js

Large diffs are not rendered by default.

0 comments on commit 7f3a50c

Please sign in to comment.