client/api: merge URL and Blob based file uploads

This commit is contained in:
rr-
2017-01-07 12:33:17 +01:00
parent 036fa9ee39
commit be6f8d7f46
3 changed files with 18 additions and 28 deletions

View File

@ -63,6 +63,7 @@ class Api extends events.EventTarget {
_process(url, requestFactory, data, files, options) {
options = options || {};
data = Object.assign({}, data);
const [fullUrl, query] = this._getFullUrl(url);
let abortFunction = null;
@ -74,14 +75,19 @@ class Api extends events.EventTarget {
if (query) {
req.query(query);
}
if (data) {
req.attach('metadata', new Blob([JSON.stringify(data)]));
}
if (files) {
for (let key of Object.keys(files)) {
req.attach(key, files[key] || new Blob());
const value = files[key];
if (value.constructor === String) {
data[key + 'Url'] = value;
} else {
req.attach(key, value || new Blob());
}
}
}
if (data) {
req.attach('metadata', new Blob([JSON.stringify(data)]));
}
try {
if (this.userName && this.userPassword) {
req.auth(