Do not request upload token if there's an upload in process
This commit is contained in:
parent
04e1e004da
commit
554c602230
@ -115,8 +115,9 @@ class Uploader {
|
|||||||
this.fileQueue.push(file);
|
this.fileQueue.push(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
// if the queue was empty and we added some files to it, start uploading them
|
// if the queue was empty and we added some files to it, and there currently
|
||||||
if (wasQueueEmpty && this.fileQueue.length > 0) {
|
// is no upload in process, request a token to start the upload process
|
||||||
|
if (wasQueueEmpty && this.xhr === null && this.fileQueue.length > 0) {
|
||||||
this.requestToken();
|
this.requestToken();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -132,10 +133,9 @@ class Uploader {
|
|||||||
|
|
||||||
uploadNextFileInQueue(token) {
|
uploadNextFileInQueue(token) {
|
||||||
const file = this.fileQueue.shift();
|
const file = this.fileQueue.shift();
|
||||||
const xhr = new XMLHttpRequest();
|
this.xhr = new XMLHttpRequest();
|
||||||
this.xhr = xhr;
|
|
||||||
|
|
||||||
xhr.upload.addEventListener(
|
this.xhr.upload.addEventListener(
|
||||||
"progress",
|
"progress",
|
||||||
(e) => {
|
(e) => {
|
||||||
const percent = Math.floor((e.loaded / e.total) * 1000) / 10;
|
const percent = Math.floor((e.loaded / e.total) * 1000) / 10;
|
||||||
@ -144,26 +144,26 @@ class Uploader {
|
|||||||
false
|
false
|
||||||
);
|
);
|
||||||
|
|
||||||
xhr.onreadystatechange = () => {
|
this.xhr.onreadystatechange = () => {
|
||||||
if (xhr.readyState === XMLHttpRequest.DONE) {
|
if (this.xhr.readyState === XMLHttpRequest.DONE) {
|
||||||
this.xhr = null;
|
|
||||||
|
|
||||||
let response;
|
let response;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
response = JSON.parse(xhr.responseText);
|
response = JSON.parse(this.xhr.responseText);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// This is just a safe guard and should not happen if server doesn't throw any errors.
|
// This is just a safe guard and should not happen if server doesn't throw any errors.
|
||||||
// Browsers break the HTTP spec by aborting the request without reading any response data,
|
// Browsers break the HTTP spec by aborting the request without reading any response data,
|
||||||
// if there is still data to be uploaded. Servers will only error in extreme cases like bad
|
// if there is still data to be uploaded. Servers will only error in extreme cases like bad
|
||||||
// authentication or server-side errors.
|
// authentication or server-side errors.
|
||||||
response = {
|
response = {
|
||||||
error: `Upload aborted: HTTP ${xhr.status}`,
|
error: `Upload aborted: HTTP ${this.xhr.status}`,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
this.handleResponse(response);
|
this.handleResponse(response);
|
||||||
|
|
||||||
|
this.xhr = null;
|
||||||
|
|
||||||
// this file was processed, if we still have files in the queue, upload the next one
|
// this file was processed, if we still have files in the queue, upload the next one
|
||||||
if (this.fileQueue.length > 0) {
|
if (this.fileQueue.length > 0) {
|
||||||
this.requestToken();
|
this.requestToken();
|
||||||
@ -173,8 +173,8 @@ class Uploader {
|
|||||||
|
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
formData.append("file", file);
|
formData.append("file", file);
|
||||||
xhr.open("POST", `uploads/new/${token}`);
|
this.xhr.open("POST", `uploads/new/${token}`);
|
||||||
xhr.send(formData);
|
this.xhr.send(formData);
|
||||||
}
|
}
|
||||||
|
|
||||||
handleResponse(response) {
|
handleResponse(response) {
|
||||||
|
Loading…
Reference in New Issue
Block a user