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