2018-09-03 07:30:05 +00:00
|
|
|
"use strict";
|
|
|
|
|
2019-11-16 17:24:03 +00:00
|
|
|
import {update as updateCursor} from "undate";
|
|
|
|
|
|
|
|
import socket from "./socket";
|
|
|
|
import store from "./store";
|
2019-02-19 15:12:08 +00:00
|
|
|
|
|
|
|
class Uploader {
|
2019-11-12 11:09:12 +00:00
|
|
|
init() {
|
2019-03-03 18:10:35 +00:00
|
|
|
this.xhr = null;
|
2019-02-19 15:12:08 +00:00
|
|
|
this.fileQueue = [];
|
|
|
|
|
|
|
|
document.addEventListener("dragenter", (e) => this.dragEnter(e));
|
|
|
|
document.addEventListener("dragover", (e) => this.dragOver(e));
|
|
|
|
document.addEventListener("dragleave", (e) => this.dragLeave(e));
|
|
|
|
document.addEventListener("drop", (e) => this.drop(e));
|
2019-05-18 16:10:26 +00:00
|
|
|
document.addEventListener("paste", (e) => this.paste(e));
|
2019-07-29 08:04:33 +00:00
|
|
|
|
|
|
|
socket.on("upload:auth", (token) => this.uploadNextFileInQueue(token));
|
2019-02-19 15:12:08 +00:00
|
|
|
}
|
|
|
|
|
2019-10-21 15:52:46 +00:00
|
|
|
mounted() {
|
|
|
|
this.overlay = document.getElementById("upload-overlay");
|
|
|
|
this.uploadProgressbar = document.getElementById("upload-progressbar");
|
|
|
|
}
|
|
|
|
|
2019-02-19 15:12:08 +00:00
|
|
|
dragOver(event) {
|
|
|
|
// Prevent dragover event completely and do nothing with it
|
|
|
|
// This stops the browser from trying to guess which cursor to show
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
dragEnter(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
// relatedTarget is the target where we entered the drag from
|
|
|
|
// when dragging from another window, the target is null, otherwise its a DOM element
|
|
|
|
if (!event.relatedTarget && event.dataTransfer.types.includes("Files")) {
|
|
|
|
this.overlay.classList.add("is-dragover");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dragLeave(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
// If relatedTarget is null, that means we are no longer dragging over the page
|
|
|
|
if (!event.relatedTarget) {
|
|
|
|
this.overlay.classList.remove("is-dragover");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
drop(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
this.overlay.classList.remove("is-dragover");
|
|
|
|
|
|
|
|
let files;
|
|
|
|
|
|
|
|
if (event.dataTransfer.items) {
|
|
|
|
files = Array.from(event.dataTransfer.items)
|
|
|
|
.filter((item) => item.kind === "file")
|
|
|
|
.map((item) => item.getAsFile());
|
|
|
|
} else {
|
|
|
|
files = Array.from(event.dataTransfer.files);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.triggerUpload(files);
|
|
|
|
}
|
|
|
|
|
2019-05-18 16:10:26 +00:00
|
|
|
paste(event) {
|
|
|
|
const items = event.clipboardData.items;
|
|
|
|
const files = [];
|
|
|
|
|
|
|
|
for (const item of items) {
|
|
|
|
if (item.kind === "file") {
|
|
|
|
files.push(item.getAsFile());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (files.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
this.triggerUpload(files);
|
|
|
|
}
|
|
|
|
|
2019-02-19 15:12:08 +00:00
|
|
|
triggerUpload(files) {
|
|
|
|
if (!files.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 19:40:59 +00:00
|
|
|
if (!store.state.isConnected) {
|
2019-07-29 08:04:33 +00:00
|
|
|
this.handleResponse({
|
|
|
|
error: `You are currently disconnected, unable to initiate upload process.`,
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-19 15:12:08 +00:00
|
|
|
const wasQueueEmpty = this.fileQueue.length === 0;
|
2019-11-12 11:09:12 +00:00
|
|
|
const maxFileSize = store.state.serverConfiguration.fileUploadMaxFileSize;
|
2019-02-19 15:12:08 +00:00
|
|
|
|
|
|
|
for (const file of files) {
|
2019-11-12 11:09:12 +00:00
|
|
|
if (maxFileSize > 0 && file.size > maxFileSize) {
|
2019-02-19 15:12:08 +00:00
|
|
|
this.handleResponse({
|
|
|
|
error: `File ${file.name} is over the maximum allowed size`,
|
|
|
|
});
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.fileQueue.push(file);
|
|
|
|
}
|
|
|
|
|
2019-08-25 17:23:32 +00:00
|
|
|
// if the queue was empty and we added some files to it, and there currently
|
|
|
|
// is no upload in process, request a token to start the upload process
|
|
|
|
if (wasQueueEmpty && this.xhr === null && this.fileQueue.length > 0) {
|
2019-07-29 08:04:33 +00:00
|
|
|
this.requestToken();
|
2019-02-19 15:12:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-29 08:04:33 +00:00
|
|
|
requestToken() {
|
2019-02-19 15:12:08 +00:00
|
|
|
socket.emit("upload:auth");
|
|
|
|
}
|
|
|
|
|
|
|
|
setProgress(value) {
|
|
|
|
this.uploadProgressbar.classList.toggle("upload-progressbar-visible", value > 0);
|
|
|
|
this.uploadProgressbar.style.width = value + "%";
|
|
|
|
}
|
|
|
|
|
2019-07-29 08:04:33 +00:00
|
|
|
uploadNextFileInQueue(token) {
|
|
|
|
const file = this.fileQueue.shift();
|
2019-08-25 17:23:32 +00:00
|
|
|
this.xhr = new XMLHttpRequest();
|
2019-02-19 15:12:08 +00:00
|
|
|
|
2019-08-25 17:23:32 +00:00
|
|
|
this.xhr.upload.addEventListener(
|
2019-07-17 09:33:59 +00:00
|
|
|
"progress",
|
|
|
|
(e) => {
|
|
|
|
const percent = Math.floor((e.loaded / e.total) * 1000) / 10;
|
|
|
|
this.setProgress(percent);
|
|
|
|
},
|
|
|
|
false
|
|
|
|
);
|
2019-02-19 15:12:08 +00:00
|
|
|
|
2019-08-25 17:23:32 +00:00
|
|
|
this.xhr.onreadystatechange = () => {
|
|
|
|
if (this.xhr.readyState === XMLHttpRequest.DONE) {
|
2019-02-19 15:12:08 +00:00
|
|
|
let response;
|
|
|
|
|
|
|
|
try {
|
2019-08-25 17:23:32 +00:00
|
|
|
response = JSON.parse(this.xhr.responseText);
|
2019-02-19 15:12:08 +00:00
|
|
|
} catch (err) {
|
|
|
|
// 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,
|
|
|
|
// if there is still data to be uploaded. Servers will only error in extreme cases like bad
|
|
|
|
// authentication or server-side errors.
|
|
|
|
response = {
|
2019-08-25 17:23:32 +00:00
|
|
|
error: `Upload aborted: HTTP ${this.xhr.status}`,
|
2019-02-19 15:12:08 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
this.handleResponse(response);
|
|
|
|
|
2019-08-25 17:23:32 +00:00
|
|
|
this.xhr = null;
|
|
|
|
|
2019-02-19 15:12:08 +00:00
|
|
|
// this file was processed, if we still have files in the queue, upload the next one
|
|
|
|
if (this.fileQueue.length > 0) {
|
2019-07-29 08:04:33 +00:00
|
|
|
this.requestToken();
|
2019-02-19 15:12:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const formData = new FormData();
|
|
|
|
formData.append("file", file);
|
2019-08-25 17:23:32 +00:00
|
|
|
this.xhr.open("POST", `uploads/new/${token}`);
|
|
|
|
this.xhr.send(formData);
|
2019-02-19 15:12:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleResponse(response) {
|
|
|
|
this.setProgress(0);
|
|
|
|
|
|
|
|
if (response.error) {
|
2019-11-02 19:40:59 +00:00
|
|
|
store.commit("currentUserVisibleError", response.error);
|
2019-02-19 15:12:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.url) {
|
|
|
|
this.insertUploadUrl(response.url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
insertUploadUrl(url) {
|
2019-07-17 09:33:59 +00:00
|
|
|
const fullURL = new URL(url, location).toString();
|
2019-02-19 15:12:08 +00:00
|
|
|
const textbox = document.getElementById("input");
|
|
|
|
const initStart = textbox.selectionStart;
|
|
|
|
|
|
|
|
// Get the text before the cursor, and add a space if it's not in the beginning
|
2019-07-17 09:33:59 +00:00
|
|
|
const headToCursor = initStart > 0 ? textbox.value.substr(0, initStart) + " " : "";
|
2019-02-19 15:12:08 +00:00
|
|
|
|
|
|
|
// Get the remaining text after the cursor
|
|
|
|
const cursorToTail = textbox.value.substr(initStart);
|
|
|
|
|
|
|
|
// Construct the value until the point where we want the cursor to be
|
|
|
|
const textBeforeTail = headToCursor + fullURL + " ";
|
|
|
|
|
|
|
|
updateCursor(textbox, textBeforeTail + cursorToTail);
|
|
|
|
|
|
|
|
// Set the cursor after the link and a space
|
|
|
|
textbox.selectionStart = textbox.selectionEnd = textBeforeTail.length;
|
|
|
|
}
|
2019-03-03 18:10:35 +00:00
|
|
|
|
|
|
|
// TODO: This is a temporary hack while Vue porting is finalized
|
|
|
|
abort() {
|
|
|
|
this.fileQueue = [];
|
|
|
|
|
|
|
|
if (this.xhr) {
|
|
|
|
this.xhr.abort();
|
|
|
|
this.xhr = null;
|
|
|
|
}
|
|
|
|
}
|
2019-02-19 15:12:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const instance = new Uploader();
|
2018-09-03 07:30:05 +00:00
|
|
|
|
2019-11-16 17:24:03 +00:00
|
|
|
export default {
|
2019-03-03 18:10:35 +00:00
|
|
|
abort: () => instance.abort(),
|
2019-10-21 15:52:46 +00:00
|
|
|
initialize: () => instance.init(),
|
|
|
|
mounted: () => instance.mounted(),
|
|
|
|
triggerUpload: (files) => instance.triggerUpload(files),
|
2019-03-03 18:10:35 +00:00
|
|
|
};
|