mirror of
https://github.com/Dangoware/confetti-box.git
synced 2025-04-19 23:32:58 -05:00
Added ability to upload multiple files at the same time and see progress
This commit is contained in:
parent
2d720c28ab
commit
e356e9377d
3 changed files with 58 additions and 31 deletions
|
@ -47,6 +47,7 @@ fn favicon() -> (ContentType, &'static str) {
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
fn home(settings: &State<Settings>) -> Markup {
|
fn home(settings: &State<Settings>) -> Markup {
|
||||||
|
dbg!(settings.duration.default);
|
||||||
html! {
|
html! {
|
||||||
(head("Confetti-Box"))
|
(head("Confetti-Box"))
|
||||||
|
|
||||||
|
@ -69,7 +70,7 @@ fn home(settings: &State<Settings>) -> Markup {
|
||||||
input id="fileInput" type="file" name="fileUpload"
|
input id="fileInput" type="file" name="fileUpload"
|
||||||
onchange="formSubmit(this.parentNode)" data-max-filesize=(settings.max_filesize) style="display:none;";
|
onchange="formSubmit(this.parentNode)" data-max-filesize=(settings.max_filesize) style="display:none;";
|
||||||
input id="fileDuration" type="text" name="duration" minlength="2"
|
input id="fileDuration" type="text" name="duration" minlength="2"
|
||||||
maxlength="7" value=(settings.duration.default) style="display:none;";
|
maxlength="7" value=(settings.duration.default.num_seconds().to_string() + "s") style="display:none;";
|
||||||
}
|
}
|
||||||
button.main_file_upload onclick="document.getElementById('fileInput').click()" {
|
button.main_file_upload onclick="document.getElementById('fileInput').click()" {
|
||||||
h4 { "Upload File" }
|
h4 { "Upload File" }
|
||||||
|
@ -79,7 +80,7 @@ fn home(settings: &State<Settings>) -> Markup {
|
||||||
|
|
||||||
h3 { "Uploaded Files" }
|
h3 { "Uploaded Files" }
|
||||||
div #uploadedFilesDisplay {
|
div #uploadedFilesDisplay {
|
||||||
div { p {"File Name Here"} span {" "} div {p {"File Link Here"} button {"copy"}} }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
hr;
|
hr;
|
||||||
|
|
|
@ -116,10 +116,9 @@ button.main_file_upload {
|
||||||
gap: 10px;
|
gap: 10px;
|
||||||
}
|
}
|
||||||
|
|
||||||
#uploadedFilesDisplay > div > span {
|
#uploadedFilesDisplay > div > progress {
|
||||||
flex-grow: 2;
|
flex-grow: 2;
|
||||||
border-top: 2px dotted grey;
|
height: 20px;
|
||||||
height: 0px;
|
|
||||||
margin: auto;
|
margin: auto;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -127,3 +126,31 @@ button.main_file_upload {
|
||||||
height: 50px;
|
height: 50px;
|
||||||
width: 50px;
|
width: 50px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
progress[value] {
|
||||||
|
--color: #84FFAE; /* the progress color */
|
||||||
|
--background: lightgrey; /* the background color */
|
||||||
|
|
||||||
|
-webkit-appearance: none;
|
||||||
|
-moz-appearance: none;
|
||||||
|
appearance: none;
|
||||||
|
background-color: var(--background);
|
||||||
|
}
|
||||||
|
|
||||||
|
progress[value]::-moz-progress-bar {
|
||||||
|
background-color: var(--color);
|
||||||
|
border-radius: 5px;
|
||||||
|
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.25) inset;
|
||||||
|
}
|
||||||
|
|
||||||
|
progress[value]::-webkit-progress-bar {
|
||||||
|
background-color: var(--background);
|
||||||
|
border-radius: 5px;
|
||||||
|
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.25) inset;
|
||||||
|
}
|
||||||
|
|
||||||
|
progress[value]::-webkit-progress-value {
|
||||||
|
border-radius: 5px;
|
||||||
|
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.25) inset;
|
||||||
|
background-color: var(--color);
|
||||||
|
}
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
let progressBar;
|
|
||||||
let progressValue;
|
|
||||||
let statusNotifier;
|
let statusNotifier;
|
||||||
let uploadedFilesDisplay;
|
let uploadedFilesDisplay;
|
||||||
let durationBox;
|
let durationBox;
|
||||||
|
@ -9,8 +7,6 @@ let uploadInProgress = false;
|
||||||
const TOO_LARGE_TEXT = "File is too large!";
|
const TOO_LARGE_TEXT = "File is too large!";
|
||||||
const ERROR_TEXT = "An error occured!";
|
const ERROR_TEXT = "An error occured!";
|
||||||
|
|
||||||
let CAPABILITIES;
|
|
||||||
|
|
||||||
async function formSubmit(form) {
|
async function formSubmit(form) {
|
||||||
if (uploadInProgress) {
|
if (uploadInProgress) {
|
||||||
return; // TODO: REMOVE THIS ONCE MULTIPLE CAN WORK!
|
return; // TODO: REMOVE THIS ONCE MULTIPLE CAN WORK!
|
||||||
|
@ -19,7 +15,7 @@ async function formSubmit(form) {
|
||||||
// Get file size and don't upload if it's too large
|
// Get file size and don't upload if it's too large
|
||||||
let file_upload = document.getElementById("fileInput");
|
let file_upload = document.getElementById("fileInput");
|
||||||
let file = file_upload.files[0];
|
let file = file_upload.files[0];
|
||||||
if (file.size > CAPABILITIES.max_filesize) {
|
if (file.size > file_upload.dataset.maxFilesize) {
|
||||||
progressValue.textContent = TOO_LARGE_TEXT;
|
progressValue.textContent = TOO_LARGE_TEXT;
|
||||||
console.error(
|
console.error(
|
||||||
"Provided file is too large", file.size, "bytes; max",
|
"Provided file is too large", file.size, "bytes; max",
|
||||||
|
@ -28,14 +24,16 @@ async function formSubmit(form) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let [progressBar, progressText] = addNewToList(file.name);
|
||||||
|
|
||||||
let url = "/upload";
|
let url = "/upload";
|
||||||
let request = new XMLHttpRequest();
|
let request = new XMLHttpRequest();
|
||||||
request.open('POST', url, true);
|
request.open('POST', url, true);
|
||||||
|
|
||||||
// Set up the listeners
|
// Set up event listeners
|
||||||
request.addEventListener('load', uploadComplete, false);
|
request.upload.addEventListener('progress', (p) => {uploadProgress(p, progressBar, progressText)}, false);
|
||||||
|
request.addEventListener('load', (c) => {uploadComplete(c, progressBar, progressText)}, false);
|
||||||
request.addEventListener('error', networkErrorHandler, false);
|
request.addEventListener('error', networkErrorHandler, false);
|
||||||
request.upload.addEventListener('progress', uploadProgress, false);
|
|
||||||
|
|
||||||
uploadInProgress = true;
|
uploadInProgress = true;
|
||||||
// Create and send FormData
|
// Create and send FormData
|
||||||
|
@ -55,42 +53,47 @@ function networkErrorHandler(_err) {
|
||||||
progressValue.textContent = "A network error occured!";
|
progressValue.textContent = "A network error occured!";
|
||||||
}
|
}
|
||||||
|
|
||||||
function uploadComplete(response) {
|
function uploadComplete(response, _progressBar, progressText) {
|
||||||
let target = response.target;
|
let target = response.target;
|
||||||
progressBar.value = 0;
|
|
||||||
|
|
||||||
if (target.status === 200) {
|
if (target.status === 200) {
|
||||||
const response = JSON.parse(target.responseText);
|
const response = JSON.parse(target.responseText);
|
||||||
|
|
||||||
if (response.status) {
|
if (response.status) {
|
||||||
progressValue.textContent = "Success";
|
progressText.textContent = "Success";
|
||||||
addToList(response.name, response.url);
|
|
||||||
} else {
|
} else {
|
||||||
console.error("Error uploading", response)
|
console.error("Error uploading", response)
|
||||||
progressValue.textContent = response.response;
|
progressText.textContent = response.response;
|
||||||
}
|
}
|
||||||
} else if (target.status === 413) {
|
} else if (target.status === 413) {
|
||||||
progressValue.textContent = TOO_LARGE_TEXT;
|
progressText.textContent = TOO_LARGE_TEXT;
|
||||||
} else {
|
} else {
|
||||||
progressValue.textContent = ERROR_TEXT;
|
progressText.textContent = ERROR_TEXT;
|
||||||
}
|
}
|
||||||
|
|
||||||
uploadInProgress = false;
|
uploadInProgress = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
function addToList(filename, link) {
|
function addNewToList(origFileName) {
|
||||||
const link_row = uploadedFilesDisplay.appendChild(document.createElement("p"));
|
const linkRow = uploadedFilesDisplay.appendChild(document.createElement("div"));
|
||||||
const new_link = link_row.appendChild(document.createElement("a"));
|
const fileName = linkRow.appendChild(document.createElement("p"));
|
||||||
|
const progressBar = linkRow.appendChild(document.createElement("progress"));
|
||||||
|
const progressTxt = linkRow.appendChild(document.createElement("p"));
|
||||||
|
|
||||||
new_link.href = link;
|
fileName.textContent = origFileName;
|
||||||
new_link.textContent = filename;
|
progressBar.max="100";
|
||||||
|
progressBar.value="0";
|
||||||
|
|
||||||
|
return [progressBar, progressTxt];
|
||||||
}
|
}
|
||||||
|
|
||||||
function uploadProgress(progress) {
|
function uploadProgress(progress, progressBar, progressText) {
|
||||||
|
console.log(progress);
|
||||||
if (progress.lengthComputable) {
|
if (progress.lengthComputable) {
|
||||||
const progressPercent = Math.floor((progress.loaded / progress.total) * 100);
|
const progressPercent = Math.floor((progress.loaded / progress.total) * 100);
|
||||||
progressBar.value = progressPercent;
|
progressBar.value = progressPercent;
|
||||||
progressValue.textContent = progressPercent + "%";
|
console.log(progressBar.value);
|
||||||
|
progressText.textContent = progressPercent + "%";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -119,8 +122,6 @@ function toPrettyTime(seconds) {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getServerCapabilities() {
|
async function getServerCapabilities() {
|
||||||
let file_duration = document.getElementById("fileDuration");
|
|
||||||
|
|
||||||
const durationButtons = durationBox.getElementsByTagName("button");
|
const durationButtons = durationBox.getElementsByTagName("button");
|
||||||
for (const b of durationButtons) {
|
for (const b of durationButtons) {
|
||||||
b.addEventListener("click", function (_e) {
|
b.addEventListener("click", function (_e) {
|
||||||
|
@ -129,8 +130,6 @@ async function getServerCapabilities() {
|
||||||
}
|
}
|
||||||
let selected = this.parentNode.getElementsByClassName("selected");
|
let selected = this.parentNode.getElementsByClassName("selected");
|
||||||
selected[0].classList.remove("selected");
|
selected[0].classList.remove("selected");
|
||||||
|
|
||||||
file_duration.value = this.dataset.durationSeconds + "s";
|
|
||||||
this.classList.add("selected");
|
this.classList.add("selected");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue