Files
fnx_web/svelte/src/filesystem/filemanager/FileUploader.svelte

245 lines
4.9 KiB
Svelte
Raw Normal View History

2020-11-17 23:39:27 +01:00
<script>
import { createEventDispatcher } from "svelte";
let dispatch = createEventDispatcher();
export let bucket_id;
export let target_dir;
2022-02-22 19:53:48 +01:00
export let write_password = "";
let upload_jobs = [];
let upload_threads = 0;
let max_upload_threads = 3;
2021-12-09 22:33:02 +01:00
let last_reload = 0;
// Adds files to the upload queue. The file_list parameter needs to be of type
// FileList. Upload will also create the necessary directories to place nested
// files in.
export const upload = (file_list) => {
console.log(file_list)
for (let i = 0; i < file_list.length; i++) {
upload_jobs.push({
file: file_list[i],
progress: 0,
target_dir: target_dir.valueOf(),
uploading: false,
finished: false,
tries: 0,
});
}
2020-11-17 23:39:27 +01:00
// This updates the UI
upload_jobs = upload_jobs;
2020-11-17 23:39:27 +01:00
while (upload_threads < max_upload_threads) {
upload_threads++;
setTimeout(upload_file, 1);
}
};
const upload_file = () => {
let job = null;
for (let i = 0; i < upload_jobs.length; i++) {
// If a file is done we remove it from the array
if (upload_jobs[i].progress >= 1) {
upload_jobs.splice(i, 1);
continue;
2020-11-17 23:39:27 +01:00
}
if (
upload_jobs[i].uploading === false &&
upload_jobs[i].finished === false
) {
job = upload_jobs[i];
job.uploading = true;
upload_jobs = upload_jobs;
break;
}
}
if (job === null) {
upload_threads--;
2020-11-17 23:39:27 +01:00
if (upload_threads === 0) {
2021-12-09 22:33:02 +01:00
dispatch("reload");
}
return;
}
2020-12-15 16:25:20 +01:00
console.log(job);
let form = new FormData();
form.append("type", "file");
form.append("file", job.file);
2022-02-22 19:53:48 +01:00
let url = window.api_endpoint+"/filesystem/"+bucket_id+encodeURIComponent(job.target_dir + "/" + job.file.name)
if (write_password) {
url += "?write_password="+write_password
}
let xhr = new XMLHttpRequest();
2022-02-22 19:53:48 +01:00
xhr.open("POST", url, true);
xhr.timeout = 21600000; // 6 hours, to account for slow connections
// Report progress updates back to the caller
xhr.upload.addEventListener("progress", (evt) => {
if (evt.lengthComputable) {
job.progress = evt.loaded / evt.total;
upload_jobs = upload_jobs;
2020-11-17 23:39:27 +01:00
}
});
2020-11-17 23:39:27 +01:00
xhr.onreadystatechange = () => {
// readystate 4 means the upload is done
if (xhr.readyState !== 4) {
2020-12-15 16:25:20 +01:00
return;
2020-11-17 23:39:27 +01:00
}
2020-12-15 16:25:20 +01:00
if (xhr.status >= 100 && xhr.status < 400) {
// Request is a success
2021-12-09 22:33:02 +01:00
let now = new Date().getTime()
if (now - last_reload > 4000) {
// If the last reload was more than 4 seconds ago we do another reload
last_reload = now
dispatch("reload");
}
// Finish the upload job
job.uploading = false;
job.finished = true;
upload_file();
} else if (xhr.status >= 400) {
// Request failed
console.log(
"Upload error. status: " +
xhr.status +
" response: " +
xhr.response
);
let resp = JSON.parse(xhr.response);
if (job.tries === 3) {
// Upload failed
2020-12-15 16:25:20 +01:00
return;
} else {
// Try again
job.tries++;
job.uploading = false;
job.finished = false;
2020-11-17 23:39:27 +01:00
}
// Sleep the upload thread for 5 seconds
setTimeout(upload_file, 5000);
} else {
// Request did not arrive
if (job.tries === 3) {
// Upload failed
alert("upload failed " + xhr.responseText);
2020-12-15 16:25:20 +01:00
job.uploading = false;
job.finished = false;
2020-12-15 16:25:20 +01:00
} else {
// Try again
job.tries++;
2020-12-15 16:25:20 +01:00
}
2020-11-17 23:39:27 +01:00
// Sleep the upload thread for 5 seconds
setTimeout(upload_file, 5000);
2020-12-15 16:25:20 +01:00
}
upload_jobs = upload_jobs;
2020-12-15 16:25:20 +01:00
};
xhr.send(form);
};
// File input dialog handling
let file_input;
export const picker = () => {
file_input.click();
};
const file_input_change = (e) => {
upload(e.target.files);
file_input.nodeValue = "";
};
// Drag and drop upload
let hidden = true;
const dragover = (e) => {
hidden = false;
};
const dragleave = (e) => {
hidden = true;
};
const drop = (e) => {
hidden = true;
upload(e.dataTransfer.files);
};
const paste = (e) => {
if (e.clipboardData.files[0]) {
e.preventDefault();
e.stopPropagation();
console.log(e.clipboardData.files[0]);
upload(e.clipboardData.files)
}
};
2020-11-17 23:39:27 +01:00
</script>
<svelte:body
on:dragover|preventDefault|stopPropagation={dragover}
on:dragleave|preventDefault|stopPropagation={dragleave}
on:drop|preventDefault|stopPropagation={drop}
2020-12-15 16:25:20 +01:00
on:paste={paste} />
2020-11-17 23:39:27 +01:00
<div>
2020-12-15 16:25:20 +01:00
<input
class="file_input"
bind:this={file_input}
on:change={file_input_change}
type="file"
multiple="multiple" />
2020-11-17 23:39:27 +01:00
<div class:hidden class="highlight_green">
Drop files here to upload them
</div>
{#each upload_jobs as c}
<div class="file_upload">
2020-12-15 16:25:20 +01:00
&nbsp;{c.file.name}&nbsp;<br />
2020-11-17 23:39:27 +01:00
<div class="upload_progress_bar">
2020-12-15 16:25:20 +01:00
<div
class="upload_progress"
style="width: {c.progress * 100}%" />
2020-11-17 23:39:27 +01:00
</div>
</div>
{/each}
</div>
2022-05-03 14:37:26 +02:00
<style>
.hidden {
display: none;
}
.file_input {
display: block;
visibility: hidden;
width: 0;
height: 0;
}
.file_upload {
display: block;
text-align: left;
width: 100%;
margin: 6px 0 0 0;
padding: 0;
background: var(--body_color);
box-shadow: 1px 1px 4px -1px var(--shadow_color);
}
.upload_progress_bar {
width: 100%;
height: 2px;
}
.upload_progress {
background-color: var(--highlight_color);
height: 100%;
}
</style>