client/posts: fix uploading posts from URLs

This commit is contained in:
rr- 2017-01-08 23:52:20 +01:00
parent 76eab79828
commit 592d2a7dae
1 changed files with 17 additions and 5 deletions

View File

@ -6,6 +6,8 @@ const config = require('./config.js');
const events = require('./events.js'); const events = require('./events.js');
const progress = require('./util/progress.js'); const progress = require('./util/progress.js');
let fileTokens = {};
class Api extends events.EventTarget { class Api extends events.EventTarget {
constructor() { constructor() {
super(); super();
@ -143,6 +145,13 @@ class Api extends events.EventTarget {
return [baseUrl, request]; return [baseUrl, request];
} }
_getFileId(file) {
if (file.constructor === String) {
return file;
}
return file.name + file.size;
}
_wrappedRequest(url, requestFactory, data, files, options) { _wrappedRequest(url, requestFactory, data, files, options) {
// transform the request: upload each file, then make the request use // transform the request: upload each file, then make the request use
// its tokens. // its tokens.
@ -151,9 +160,10 @@ class Api extends events.EventTarget {
let promise = Promise.resolve(); let promise = Promise.resolve();
if (files) { if (files) {
for (let key of Object.keys(files)) { for (let key of Object.keys(files)) {
let file = files[key]; const file = files[key];
if (file.token) { const fileId = this._getFileId(file);
data[key + 'Token'] = file.token; if (fileTokens[fileId]) {
data[key + 'Token'] = fileTokens[fileId];
} else { } else {
promise = promise promise = promise
.then(() => { .then(() => {
@ -163,7 +173,7 @@ class Api extends events.EventTarget {
}) })
.then(token => { .then(token => {
abortFunction = () => {}; abortFunction = () => {};
file.token = token; fileTokens[fileId] = token;
data[key + 'Token'] = token; data[key + 'Token'] = token;
return Promise.resolve(); return Promise.resolve();
}); });
@ -181,7 +191,9 @@ class Api extends events.EventTarget {
if (error.response && error.response.name === if (error.response && error.response.name ===
'MissingOrExpiredRequiredFileError') { 'MissingOrExpiredRequiredFileError') {
for (let key of Object.keys(files)) { for (let key of Object.keys(files)) {
files[key].token = null; const file = files[key];
const fileId = this._getFileId(file);
fileTokens[fileId] = null;
} }
error.message = error.message =
'The uploaded file has expired; ' + 'The uploaded file has expired; ' +