parent
755554d3f2
commit
19d7b36462
3 changed files with 60 additions and 19 deletions
|
@ -12,10 +12,10 @@ Duf is a simple file server.
|
||||||
- Serve static files
|
- Serve static files
|
||||||
- Download folder as zip file
|
- Download folder as zip file
|
||||||
- Search files
|
- Search files
|
||||||
- Upload files
|
- Upload files and folders
|
||||||
- Delete files
|
- Delete files
|
||||||
- Basic authentication
|
- Basic authentication
|
||||||
- Unzip zip file when upload
|
- Upload zip file then unzip
|
||||||
- Easy to use with curl
|
- Easy to use with curl
|
||||||
|
|
||||||
## Install
|
## Install
|
||||||
|
|
|
@ -159,8 +159,6 @@ body {
|
||||||
}
|
}
|
||||||
|
|
||||||
.uploaders {
|
.uploaders {
|
||||||
display: flex;
|
|
||||||
flex-wrap: wrap;
|
|
||||||
padding: 0.5em 0;
|
padding: 0.5em 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,26 +1,28 @@
|
||||||
let $tbody, $uploaders;
|
let $tbody, $uploaders;
|
||||||
let uploaderIdx = 0;
|
|
||||||
let baseDir;
|
let baseDir;
|
||||||
|
|
||||||
class Uploader {
|
class Uploader {
|
||||||
idx = 0;
|
idx;
|
||||||
file;
|
file;
|
||||||
|
name;
|
||||||
$elem;
|
$elem;
|
||||||
constructor(idx, file) {
|
static globalIdx = 0;
|
||||||
this.idx = idx;
|
constructor(file, dirs) {
|
||||||
|
this.name = [...dirs, file.name].join("/");
|
||||||
|
this.idx = Uploader.globalIdx++;
|
||||||
this.file = file;
|
this.file = file;
|
||||||
}
|
}
|
||||||
|
|
||||||
upload() {
|
upload() {
|
||||||
const { file, idx } = this;
|
const { file, idx, name } = this;
|
||||||
let url = getUrl(file.name);
|
let url = getUrl(name);
|
||||||
if (file.name == baseDir + ".zip") {
|
if (file.name == baseDir + ".zip") {
|
||||||
url += "?unzip";
|
url += "?unzip";
|
||||||
}
|
}
|
||||||
$uploaders.insertAdjacentHTML("beforeend", `
|
$uploaders.insertAdjacentHTML("beforeend", `
|
||||||
<div class="uploader path">
|
<div class="uploader path">
|
||||||
<div><svg height="16" viewBox="0 0 12 16" width="12"><path fill-rule="evenodd" d="M6 5H2V4h4v1zM2 8h7V7H2v1zm0 2h7V9H2v1zm0 2h7v-1H2v1zm10-7.5V14c0 .55-.45 1-1 1H1c-.55 0-1-.45-1-1V2c0-.55.45-1 1-1h7.5L12 4.5zM11 5L8 2H1v12h10V5z"></path></svg></div>
|
<div><svg height="16" viewBox="0 0 12 16" width="12"><path fill-rule="evenodd" d="M6 5H2V4h4v1zM2 8h7V7H2v1zm0 2h7V9H2v1zm0 2h7v-1H2v1zm10-7.5V14c0 .55-.45 1-1 1H1c-.55 0-1-.45-1-1V2c0-.55.45-1 1-1h7.5L12 4.5zM11 5L8 2H1v12h10V5z"></path></svg></div>
|
||||||
<a href="${url}" id="file${idx}">${file.name} (0%)</a>
|
<a href="${url}" id="file${idx}">${name} (0%)</a>
|
||||||
</div>`);
|
</div>`);
|
||||||
this.$elem = document.getElementById(`file${idx}`);
|
this.$elem = document.getElementById(`file${idx}`);
|
||||||
|
|
||||||
|
@ -43,15 +45,15 @@ class Uploader {
|
||||||
|
|
||||||
progress(event) {
|
progress(event) {
|
||||||
const percent = (event.loaded / event.total) * 100;
|
const percent = (event.loaded / event.total) * 100;
|
||||||
this.$elem.innerHTML = `${this.file.name} (${percent.toFixed(2)}%)`;
|
this.$elem.innerHTML = `${this.name} (${percent.toFixed(2)}%)`;
|
||||||
}
|
}
|
||||||
|
|
||||||
complete() {
|
complete() {
|
||||||
this.$elem.innerHTML = `${this.file.name}`;
|
this.$elem.innerHTML = `${this.name}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
fail() {
|
fail() {
|
||||||
this.$elem.innerHTML = `<strike>${this.file.name}</strike>`;
|
this.$elem.innerHTML = `<strike>${this.name}</strike>`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -140,6 +142,44 @@ async function deletePath(index) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function dropzone() {
|
||||||
|
["drag", "dragstart", "dragend", "dragover", "dragenter", "dragleave", "drop"].forEach(name => {
|
||||||
|
document.addEventListener(name, e => {
|
||||||
|
e.preventDefault();
|
||||||
|
e.stopPropagation();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
document.addEventListener("drop", e => {
|
||||||
|
if (!e.dataTransfer.items[0].webkitGetAsEntry) {
|
||||||
|
const files = e.dataTransfer.files.filter(v => v.size > 0);
|
||||||
|
for (const file of files) {
|
||||||
|
new Uploader(file, []).upload();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const entries = [];
|
||||||
|
const len = e.dataTransfer.items.length;
|
||||||
|
for (let i = 0; i < len; i++) {
|
||||||
|
entries.push(e.dataTransfer.items[i].webkitGetAsEntry());
|
||||||
|
}
|
||||||
|
addFileEntries(entries, [])
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function addFileEntries(entries, dirs) {
|
||||||
|
for (const entry of entries) {
|
||||||
|
if (entry.isFile) {
|
||||||
|
entry.file(file => {
|
||||||
|
new Uploader(file, dirs).upload();
|
||||||
|
});
|
||||||
|
} else if (entry.isDirectory) {
|
||||||
|
const dirReader = entry.createReader()
|
||||||
|
dirReader.readEntries(entries => addFileEntries(entries, [...dirs, entry.name]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
function getUrl(name) {
|
function getUrl(name) {
|
||||||
let url = location.href.split('?')[0];
|
let url = location.href.split('?')[0];
|
||||||
if (!url.endsWith("/")) url += "/";
|
if (!url.endsWith("/")) url += "/";
|
||||||
|
@ -183,21 +223,24 @@ function formatSize(size) {
|
||||||
return Math.round(size / Math.pow(1024, i), 2) + ' ' + sizes[i];
|
return Math.round(size / Math.pow(1024, i), 2) + ' ' + sizes[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function ready() {
|
function ready() {
|
||||||
$tbody = document.querySelector(".main tbody");
|
$tbody = document.querySelector(".main tbody");
|
||||||
$uploaders = document.querySelector(".uploaders");
|
$uploaders = document.querySelector(".uploaders");
|
||||||
|
|
||||||
addBreadcrumb(DATA.breadcrumb);
|
addBreadcrumb(DATA.breadcrumb);
|
||||||
DATA.paths.forEach((file, index) => addPath(file, index));
|
if (Array.isArray(DATA.paths)) {
|
||||||
|
const len = DATA.paths.length;
|
||||||
|
for (let i = 0; i < len; i++) {
|
||||||
|
addPath(DATA.paths[i], i);
|
||||||
|
}
|
||||||
|
}
|
||||||
if (DATA.allow_upload) {
|
if (DATA.allow_upload) {
|
||||||
|
dropzone();
|
||||||
document.querySelector(".upload-control").classList.remove(["hidden"]);
|
document.querySelector(".upload-control").classList.remove(["hidden"]);
|
||||||
document.getElementById("file").addEventListener("change", e => {
|
document.getElementById("file").addEventListener("change", e => {
|
||||||
const files = e.target.files;
|
const files = e.target.files;
|
||||||
for (let file of files) {
|
for (let file of files) {
|
||||||
uploaderIdx += 1;
|
new Uploader(file, []).upload();
|
||||||
const uploader = new Uploader(uploaderIdx, file);
|
|
||||||
uploader.upload();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue