linx-server/static/js/upload.js

155 lines
6.2 KiB
JavaScript
Raw Normal View History

// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3-or-Later
2015-10-01 05:37:00 +02:00
Dropzone.options.dropzone = {
2020-03-08 02:52:07 +01:00
init: function () {
var dzone = document.getElementById("dzone");
dzone.style.display = "block";
},
2020-03-08 02:52:07 +01:00
addedfile: function (file) {
if (!this.options.autoProcessQueue) {
var dropzone = this;
var xhr = new XMLHttpRequest();
2020-03-08 02:52:07 +01:00
xhr.onload = function () {
if (xhr.readyState !== XMLHttpRequest.DONE) {
return;
}
if (xhr.status < 400) {
dropzone.processQueue()
dropzone.options.autoProcessQueue = true;
} else {
dropzone.cancelUpload(file)
}
};
2020-03-11 06:45:24 +01:00
xhr.open("HEAD", "/auth", true);
xhr.send()
}
var upload = document.createElement("div");
upload.className = "upload";
2015-10-01 05:37:00 +02:00
var fileLabel = document.createElement("span");
fileLabel.innerHTML = file.name;
file.fileLabel = fileLabel;
upload.appendChild(fileLabel);
2015-10-01 05:37:00 +02:00
var fileActions = document.createElement("div");
fileActions.className = "right";
file.fileActions = fileActions;
upload.appendChild(fileActions);
2015-10-04 09:14:21 +02:00
var cancelAction = document.createElement("span");
cancelAction.className = "cancel";
cancelAction.innerHTML = "Cancel";
2020-03-08 02:52:07 +01:00
cancelAction.addEventListener('click', function (ev) {
this.removeFile(file);
}.bind(this));
file.cancelActionElement = cancelAction;
fileActions.appendChild(cancelAction);
2015-10-04 09:14:21 +02:00
var progress = document.createElement("span");
file.progressElement = progress;
fileActions.appendChild(progress);
2015-10-01 05:37:00 +02:00
file.uploadElement = upload;
2015-10-04 09:14:21 +02:00
document.getElementById("uploads").appendChild(upload);
},
2020-03-08 02:52:07 +01:00
uploadprogress: function (file, p, bytesSent) {
p = parseInt(p);
file.progressElement.innerHTML = p + "%";
file.uploadElement.setAttribute("style", 'background-image: -webkit-linear-gradient(left, #F2F4F7 ' + p + '%, #E2E2E2 ' + p + '%); background-image: -moz-linear-gradient(left, #F2F4F7 ' + p + '%, #E2E2E2 ' + p + '%); background-image: -ms-linear-gradient(left, #F2F4F7 ' + p + '%, #E2E2E2 ' + p + '%); background-image: -o-linear-gradient(left, #F2F4F7 ' + p + '%, #E2E2E2 ' + p + '%); background-image: linear-gradient(left, #F2F4F7 ' + p + '%, #E2E2E2 ' + p + '%)');
},
2020-03-08 02:52:07 +01:00
sending: function (file, xhr, formData) {
var randomize = document.getElementById("randomize");
2020-03-08 02:52:07 +01:00
if (randomize != null) {
formData.append("randomize", randomize.checked);
}
formData.append("expires", document.getElementById("expires").value);
},
2020-03-08 02:52:07 +01:00
success: function (file, resp) {
2015-10-04 09:14:21 +02:00
file.fileActions.removeChild(file.progressElement);
var fileLabelLink = document.createElement("a");
fileLabelLink.href = resp.url;
fileLabelLink.target = "_blank";
fileLabelLink.innerHTML = resp.url;
file.fileLabel.innerHTML = "";
file.fileLabelLink = fileLabelLink;
file.fileLabel.appendChild(fileLabelLink);
var deleteAction = document.createElement("span");
deleteAction.innerHTML = "Delete";
deleteAction.className = "cancel";
2020-03-08 02:52:07 +01:00
deleteAction.addEventListener('click', function (ev) {
xhr = new XMLHttpRequest();
xhr.open("DELETE", resp.url, true);
xhr.setRequestHeader("Linx-Delete-Key", resp.delete_key);
2020-03-08 02:52:07 +01:00
xhr.onreadystatechange = function (file) {
if (xhr.readyState == 4 && xhr.status === 200) {
var text = document.createTextNode("Deleted ");
file.fileLabel.insertBefore(text, file.fileLabelLink);
file.fileLabel.className = "deleted";
file.fileActions.removeChild(file.cancelActionElement);
}
}.bind(this, file);
xhr.send();
});
file.fileActions.removeChild(file.cancelActionElement);
file.cancelActionElement = deleteAction;
file.fileActions.appendChild(deleteAction);
},
2020-03-08 02:52:07 +01:00
canceled: function (file) {
this.options.error(file);
},
2020-03-08 02:52:07 +01:00
error: function (file, resp, xhrO) {
2015-10-04 09:14:21 +02:00
file.fileActions.removeChild(file.cancelActionElement);
file.fileActions.removeChild(file.progressElement);
if (file.status === "canceled") {
2020-03-08 02:52:07 +01:00
file.fileLabel.innerHTML = file.name + ": Canceled ";
}
else {
if (resp.error) {
file.fileLabel.innerHTML = file.name + ": " + resp.error;
}
else if (resp.includes("<html")) {
file.fileLabel.innerHTML = file.name + ": Server Error";
}
else {
file.fileLabel.innerHTML = file.name + ": " + resp;
}
}
file.fileLabel.className = "error";
},
2015-10-01 18:46:34 +02:00
autoProcessQueue: document.getElementById("dropzone").getAttribute("data-auth") !== "basic",
maxFilesize: Math.round(parseInt(document.getElementById("dropzone").getAttribute("data-maxsize"), 10) / 1024 / 1024),
previewsContainer: "#uploads",
parallelUploads: 5,
2020-03-08 02:52:07 +01:00
headers: { "Accept": "application/json" },
dictDefaultMessage: "Click or Drop file(s) or Paste image",
dictFallbackMessage: ""
2015-10-01 05:37:00 +02:00
};
2020-03-08 02:52:07 +01:00
document.onpaste = function (event) {
var items = (event.clipboardData || event.originalEvent.clipboardData).items;
for (index in items) {
var item = items[index];
if (item.kind === "file") {
Dropzone.forElement("#dropzone").addFile(item.getAsFile());
}
}
};
2020-03-08 02:52:07 +01:00
document.getElementById("access_key_checkbox").onchange = function (event) {
2020-03-08 03:52:29 +01:00
if (event.target.checked) {
document.getElementById("access_key_input").style.display = "inline-block";
document.getElementById("access_key_text").style.display = "none";
} else {
2020-03-08 02:52:07 +01:00
document.getElementById("access_key_input").value = "";
2020-03-08 03:52:29 +01:00
document.getElementById("access_key_input").style.display = "none";
document.getElementById("access_key_text").style.display = "inline-block";
2020-03-08 02:52:07 +01:00
}
};
// @end-license