Merge pull request #3335 from thelounge/xpaw/upload-sanity

Handle upload token requesting in a better way
This commit is contained in:
Pavel Djundik 2019-08-03 22:23:20 +03:00 committed by GitHub
commit ecb4dd9675
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -5,6 +5,7 @@ const updateCursor = require("undate").update;
class Uploader {
init() {
this.vueApp = require("./vue").vueApp;
this.xhr = null;
this.fileQueue = [];
this.overlay = document.getElementById("upload-overlay");
@ -17,6 +18,8 @@ class Uploader {
document.addEventListener("dragleave", (e) => this.dragLeave(e));
document.addEventListener("drop", (e) => this.drop(e));
document.addEventListener("paste", (e) => this.paste(e));
socket.on("upload:auth", (token) => this.uploadNextFileInQueue(token));
}
dragOver(event) {
@ -89,6 +92,14 @@ class Uploader {
return;
}
if (!this.vueApp.isConnected) {
this.handleResponse({
error: `You are currently disconnected, unable to initiate upload process.`,
});
return;
}
const wasQueueEmpty = this.fileQueue.length === 0;
for (const file of files) {
@ -105,18 +116,12 @@ class Uploader {
// if the queue was empty and we added some files to it, start uploading them
if (wasQueueEmpty && this.fileQueue.length > 0) {
this.dequeueNextFile();
this.requestToken();
}
}
dequeueNextFile() {
const file = this.fileQueue.shift();
// request an upload authentication token and then upload a file using said token
requestToken() {
socket.emit("upload:auth");
socket.once("upload:auth", (token) => {
this.uploadSingleFile(file, token);
});
}
setProgress(value) {
@ -124,7 +129,8 @@ class Uploader {
this.uploadProgressbar.style.width = value + "%";
}
uploadSingleFile(file, token) {
uploadNextFileInQueue(token) {
const file = this.fileQueue.shift();
const xhr = new XMLHttpRequest();
this.xhr = xhr;
@ -159,7 +165,7 @@ class Uploader {
// this file was processed, if we still have files in the queue, upload the next one
if (this.fileQueue.length > 0) {
this.dequeueNextFile();
this.requestToken();
}
}
};