feat: unzip zip file when unload

This commit is contained in:
sigoden 2022-05-30 14:22:35 +08:00
parent d9547ad00b
commit 62696b45fd
5 changed files with 55 additions and 24 deletions

View file

@ -15,6 +15,7 @@ Duf is a simple file server.
- Upload files - Upload files
- Delete files - Delete files
- Basic authentication - Basic authentication
- Unzip zip file when upload
- Easy to use with curl - Easy to use with curl
## Install ## Install
@ -71,6 +72,12 @@ Upload a file
curl --upload-file some-file http://127.0.0.1:5000/some-file curl --upload-file some-file http://127.0.0.1:5000/some-file
``` ```
Unzip zip file when unload
```
curl --upload-file some-folder.zip http://127.0.0.1:5000/some-folder.zip?unzip
```
Delete a file/folder Delete a file/folder
``` ```

View file

@ -1,6 +1,7 @@
use crate::{Args, BoxResult}; use crate::{Args, BoxResult};
use async_walkdir::WalkDir; use async_walkdir::WalkDir;
use async_zip::read::seek::ZipFileReader;
use async_zip::write::{EntryOptions, ZipFileWriter}; use async_zip::write::{EntryOptions, ZipFileWriter};
use async_zip::Compression; use async_zip::Compression;
use futures::stream::StreamExt; use futures::stream::StreamExt;
@ -157,7 +158,7 @@ impl InnerService {
None => return Ok(forbidden), None => return Ok(forbidden),
} }
let mut file = fs::File::create(path).await?; let mut file = fs::File::create(&path).await?;
let body_with_io_error = req let body_with_io_error = req
.body_mut() .body_mut()
@ -169,6 +170,30 @@ impl InnerService {
io::copy(&mut body_reader, &mut file).await?; io::copy(&mut body_reader, &mut file).await?;
let req_query = req.uri().query().unwrap_or_default();
if req_query == "unzip" {
let root = path.parent().unwrap();
let mut zip = ZipFileReader::new(File::open(&path).await?).await?;
for i in 0..zip.entries().len() {
let entry = &zip.entries()[i];
let entry_name = entry.name();
let entry_path = root.join(entry_name);
if entry_name.ends_with('/') {
fs::create_dir_all(entry_path).await?;
} else {
if let Some(parent) = entry_path.parent() {
if fs::symlink_metadata(parent).await.is_err() {
fs::create_dir_all(&parent).await?;
}
}
let mut outfile = fs::File::create(&entry_path).await?;
let mut reader = zip.entry_reader(i).await?;
io::copy(&mut reader, &mut outfile).await?;
}
}
fs::remove_file(&path).await?;
}
return Ok(status_code!(StatusCode::OK)); return Ok(status_code!(StatusCode::OK));
} }

View file

@ -8,6 +8,10 @@ body {
width: 700px; width: 700px;
} }
.hidden {
display: none;
}
.head { .head {
display: flex; display: flex;
flex-wrap: wrap; flex-wrap: wrap;

View file

@ -15,6 +15,12 @@
<svg width="16" height="16" viewBox="0 0 16 16"><path d="M.5 9.9a.5.5 0 0 1 .5.5v2.5a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1v-2.5a.5.5 0 0 1 1 0v2.5a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2v-2.5a.5.5 0 0 1 .5-.5z"/><path d="M7.646 11.854a.5.5 0 0 0 .708 0l3-3a.5.5 0 0 0-.708-.708L8.5 10.293V1.5a.5.5 0 0 0-1 0v8.793L5.354 8.146a.5.5 0 1 0-.708.708l3 3z"/></svg> <svg width="16" height="16" viewBox="0 0 16 16"><path d="M.5 9.9a.5.5 0 0 1 .5.5v2.5a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1v-2.5a.5.5 0 0 1 1 0v2.5a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2v-2.5a.5.5 0 0 1 .5-.5z"/><path d="M7.646 11.854a.5.5 0 0 0 .708 0l3-3a.5.5 0 0 0-.708-.708L8.5 10.293V1.5a.5.5 0 0 0-1 0v8.793L5.354 8.146a.5.5 0 1 0-.708.708l3 3z"/></svg>
</a> </a>
</div> </div>
<div class="upload-control hidden" title="Upload file">
<label for="file">
<svg width="16" height="16" viewBox="0 0 16 16"><path d="M.5 9.9a.5.5 0 0 1 .5.5v2.5a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1v-2.5a.5.5 0 0 1 1 0v2.5a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2v-2.5a.5.5 0 0 1 .5-.5z"/><path d="M7.646 1.146a.5.5 0 0 1 .708 0l3 3a.5.5 0 0 1-.708.708L8.5 2.707V11.5a.5.5 0 0 1-1 0V2.707L5.354 4.854a.5.5 0 1 1-.708-.708l3-3z"/></svg>
</label>
<input type="file" id="file" name="file" multiple>
</div>
</div> </div>
<form class="searchbar"> <form class="searchbar">
<div class="icon"> <div class="icon">

View file

@ -1,6 +1,6 @@
var uploaderIdx = 0;
var breadcrumb, paths, readonly;
var $toolbox, $tbody, $breadcrumb, $uploaders, $uploadControl; var $toolbox, $tbody, $breadcrumb, $uploaders, $uploadControl;
var uploaderIdx = 0;
var baseDir;
class Uploader { class Uploader {
idx = 0; idx = 0;
@ -14,6 +14,9 @@ class Uploader {
upload() { upload() {
var { file, idx } = this; var { file, idx } = this;
var url = getUrl(file.name); var url = getUrl(file.name);
if (file.name == baseDir + ".zip") {
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>
@ -55,6 +58,7 @@ function addBreadcrumb(value) {
} }
if (i === len - 1) { if (i === len - 1) {
$breadcrumb.insertAdjacentHTML("beforeend", `<b>${name}</b>`); $breadcrumb.insertAdjacentHTML("beforeend", `<b>${name}</b>`);
baseDir = name;
} else if (i === 0) { } else if (i === 0) {
$breadcrumb.insertAdjacentHTML("beforeend", `<a href="/"><b>${name}</b></a>`); $breadcrumb.insertAdjacentHTML("beforeend", `<a href="/"><b>${name}</b></a>`);
} else { } else {
@ -83,7 +87,7 @@ function addPath(file, index) {
</a> </a>
</div>`; </div>`;
} }
if (!readonly) { if (!DATA.readonly) {
actionDelete = ` actionDelete = `
<div onclick="deletePath(${index})" class="action-btn" id="deleteBtn${index}" title="Delete ${file.name}"> <div onclick="deletePath(${index})" class="action-btn" id="deleteBtn${index}" title="Delete ${file.name}">
<svg width="16" height="16" fill="currentColor"viewBox="0 0 16 16"><path d="M6.854 7.146a.5.5 0 1 0-.708.708L7.293 9l-1.147 1.146a.5.5 0 0 0 .708.708L8 9.707l1.146 1.147a.5.5 0 0 0 .708-.708L8.707 9l1.147-1.146a.5.5 0 0 0-.708-.708L8 8.293 6.854 7.146z"/><path d="M14 14V4.5L9.5 0H4a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h8a2 2 0 0 0 2-2zM9.5 3A1.5 1.5 0 0 0 11 4.5h2V14a1 1 0 0 1-1 1H4a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1h5.5v2z"/></svg> <svg width="16" height="16" fill="currentColor"viewBox="0 0 16 16"><path d="M6.854 7.146a.5.5 0 1 0-.708.708L7.293 9l-1.147 1.146a.5.5 0 0 0 .708.708L8 9.707l1.146 1.147a.5.5 0 0 0 .708-.708L8.707 9l1.147-1.146a.5.5 0 0 0-.708-.708L8 8.293 6.854 7.146z"/><path d="M14 14V4.5L9.5 0H4a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h8a2 2 0 0 0 2-2zM9.5 3A1.5 1.5 0 0 0 11 4.5h2V14a1 1 0 0 1-1 1H4a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1h5.5v2z"/></svg>
@ -108,7 +112,7 @@ ${actionCell}
} }
async function deletePath(index) { async function deletePath(index) {
var file = paths[index]; var file = DATA.paths[index];
if (!file) return; if (!file) return;
var ajax = new XMLHttpRequest(); var ajax = new XMLHttpRequest();
@ -121,17 +125,6 @@ async function deletePath(index) {
ajax.send(); ajax.send();
} }
function addUploadControl() {
$toolbox.insertAdjacentHTML("beforeend", `
<div class="upload-control" title="Upload file">
<label for="file">
<svg width="16" height="16" viewBox="0 0 16 16"><path d="M.5 9.9a.5.5 0 0 1 .5.5v2.5a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1v-2.5a.5.5 0 0 1 1 0v2.5a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2v-2.5a.5.5 0 0 1 .5-.5z"/><path d="M7.646 1.146a.5.5 0 0 1 .708 0l3 3a.5.5 0 0 1-.708.708L8.5 2.707V11.5a.5.5 0 0 1-1 0V2.707L5.354 4.854a.5.5 0 1 1-.708-.708l3-3z"/></svg>
</label>
<input type="file" id="file" name="file" multiple>
</div>
`);
}
function getUrl(name) { function getUrl(name) {
var url = location.href.split('?')[0]; var url = location.href.split('?')[0];
if (!url.endsWith("/")) url += "/"; if (!url.endsWith("/")) url += "/";
@ -177,19 +170,15 @@ function formatSize(size) {
function ready() { function ready() {
breadcrumb = DATA.breadcrumb;
paths = DATA.paths;
readonly = DATA.readonly;
$toolbox = document.querySelector(".toolbox"); $toolbox = document.querySelector(".toolbox");
$tbody = document.querySelector(".main tbody"); $tbody = document.querySelector(".main tbody");
$breadcrumb = document.querySelector(".breadcrumb"); $breadcrumb = document.querySelector(".breadcrumb");
$uploaders = document.querySelector(".uploaders"); $uploaders = document.querySelector(".uploaders");
$uploadControl = document.querySelector(".upload-control");
addBreadcrumb(breadcrumb); addBreadcrumb(DATA.breadcrumb);
paths.forEach((file, index) => addPath(file, index)); DATA.paths.forEach((file, index) => addPath(file, index));
if (!readonly) { if (!DATA.readonly) {
addUploadControl(); document.querySelector(".upload-control").classList.remove(["hidden"]);
document.getElementById("file").addEventListener("change", e => { document.getElementById("file").addEventListener("change", e => {
var files = e.target.files; var files = e.target.files;
for (var file of files) { for (var file of files) {