1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-25 05:23:13 +03:00

allow omitting filename from upload entirely

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
Michael Telatynski
2018-06-15 18:25:20 +01:00
parent 5e3b1bf6b0
commit c0b2151929
2 changed files with 26 additions and 9 deletions

View File

@@ -1071,6 +1071,10 @@ MatrixBaseApis.prototype.searchUserDirectory = function(opts) {
* @param {string=} opts.name Name to give the file on the server. Defaults * @param {string=} opts.name Name to give the file on the server. Defaults
* to <tt>file.name</tt>. * to <tt>file.name</tt>.
* *
* @param {boolean=} opts.omitFilename if true will not send the filename,
* e.g for encrypted file uploads where filename leaks are undesirable.
* Defaults to false.
*
* @param {string=} opts.type Content-type for the upload. Defaults to * @param {string=} opts.type Content-type for the upload. Defaults to
* <tt>file.type</tt>, or <tt>applicaton/octet-stream</tt>. * <tt>file.type</tt>, or <tt>applicaton/octet-stream</tt>.
* *

View File

@@ -118,6 +118,10 @@ module.exports.MatrixHttpApi.prototype = {
* @param {string=} opts.name Name to give the file on the server. Defaults * @param {string=} opts.name Name to give the file on the server. Defaults
* to <tt>file.name</tt>. * to <tt>file.name</tt>.
* *
* @param {boolean=} opts.omitFilename if true will not send the filename,
* e.g for encrypted file uploads where filename leaks are undesirable.
* Defaults to false.
*
* @param {string=} opts.type Content-type for the upload. Defaults to * @param {string=} opts.type Content-type for the upload. Defaults to
* <tt>file.type</tt>, or <tt>applicaton/octet-stream</tt>. * <tt>file.type</tt>, or <tt>applicaton/octet-stream</tt>.
* *
@@ -271,20 +275,27 @@ module.exports.MatrixHttpApi.prototype = {
}); });
} }
}); });
let url = this.opts.baseUrl + "/_matrix/media/v1/upload?"; let url = this.opts.baseUrl + "/_matrix/media/v1/upload";
const queryArgs = ["filename=" + encodeURIComponent(fileName)]; const queryArgs = [];
if (this.useAuthorizationHeader) { if (!opts.omitFilename && fileName) {
xhr.setRequestHeader("Authorization", "Bearer " + this.opts.accessToken); queryArgs.push("filename=" + encodeURIComponent(fileName));
} else { }
if (!this.useAuthorizationHeader) {
queryArgs.push("access_token=" queryArgs.push("access_token="
+ encodeURIComponent(this.opts.accessToken)); + encodeURIComponent(this.opts.accessToken));
} }
url += queryArgs.join("&"); if (queryArgs.length > 0) {
url += "?" + queryArgs.join("&");
}
xhr.open("POST", url); xhr.open("POST", url);
if (this.useAuthorizationHeader) {
xhr.setRequestHeader("Authorization", "Bearer " + this.opts.accessToken);
}
xhr.setRequestHeader("Content-Type", contentType); xhr.setRequestHeader("Content-Type", contentType);
xhr.send(body); xhr.send(body);
promise = defer.promise; promise = defer.promise;
@@ -292,9 +303,11 @@ module.exports.MatrixHttpApi.prototype = {
// dirty hack (as per _request) to allow the upload to be cancelled. // dirty hack (as per _request) to allow the upload to be cancelled.
promise.abort = xhr.abort.bind(xhr); promise.abort = xhr.abort.bind(xhr);
} else { } else {
const queryParams = { const queryParams = {};
filename: fileName,
}; if (!opts.omitFilename && fileName) {
queryParams.filename = fileName;
}
promise = this.authedRequest( promise = this.authedRequest(
opts.callback, "POST", "/upload", queryParams, body, { opts.callback, "POST", "/upload", queryParams, body, {