This commit is contained in:
Philipp Heckel 2022-04-05 19:55:43 -04:00
parent 35ddcb27f0
commit 2cd7839da3
2 changed files with 19 additions and 4 deletions

View file

@ -44,6 +44,19 @@ class Api {
});
}
/**
* Publishes to a topic using XMLHttpRequest (XHR), and returns a Promise with the active request.
* Unfortunately, fetch() does not support a progress hook, which is why XHR has to be used.
*
* Firefox XHR bug:
* Firefox has a bug(?), which returns 0 and "" for all fields of the XHR response in the case of an error,
* so we cannot determine the exact error. It also sometimes complains about CORS violations, even when the
* correct headers are clearly set. It's quite the odd behavior.
*
* There is an example, and the bug report here:
* - https://bugzilla.mozilla.org/show_bug.cgi?id=1733755
* - https://gist.github.com/binwiederhier/627f146d1959799be207ad8c17a8f345
*/
publishXHR(baseUrl, topic, body, headers, onProgress) {
const url = topicUrl(baseUrl, topic);
const xhr = new XMLHttpRequest();
@ -63,6 +76,7 @@ class Api {
console.log(`[Api] Publish successful (HTTP ${xhr.status})`, xhr.response);
resolve(xhr.response);
} else if (xhr.readyState === 4) {
// Firefox bug; see description above!
console.log(`[Api] Publish failed (HTTP ${xhr.status})`, xhr.responseText);
let errorText;
try {
@ -109,7 +123,9 @@ class Api {
if (response.status !== 200) {
throw new Error(`Unexpected server response ${response.status}`);
}
return response.json();
const stats = response.json();
console.log(`[Api] Stats`, stats);
return stats;
}
}

View file

@ -147,17 +147,16 @@ const SendDialog = (props) => {
try {
const { baseUrl } = splitTopicUrl(topicUrl);
const stats = await api.userStats(baseUrl);
console.log(`[SendDialog] Visitor attachment limits`, stats);
const fileSizeLimit = stats.attachmentFileSizeLimit ?? 0;
const remainingBytes = stats.visitorAttachmentBytesRemaining ?? 0;
const fileSizeLimitReached = fileSizeLimit > 0 && file.size > fileSizeLimit;
const quotaReached = remainingBytes > 0 && file.size > remainingBytes;
if (fileSizeLimitReached && quotaReached) {
return setAttachFileError(`exceeds ${formatBytes(fileSizeLimit)} file limit, and quota reached, ${formatBytes(remainingBytes)} remaining`);
return setAttachFileError(`exceeds ${formatBytes(fileSizeLimit)} file limit and quota, ${formatBytes(remainingBytes)} remaining`);
} else if (fileSizeLimitReached) {
return setAttachFileError(`exceeds ${formatBytes(fileSizeLimit)} file limit`);
} else if (quotaReached) {
return setAttachFileError(`quota reached, ${formatBytes(remainingBytes)} remaining`);
return setAttachFileError(`exceeds quota, ${formatBytes(remainingBytes)} remaining`);
}
setAttachFileError("");
} catch (e) {