From 62696b45fde4baef6fa09b4e82470eefca1b5149 Mon Sep 17 00:00:00 2001 From: sigoden <sigoden@gmail.com> Date: Mon, 30 May 2022 14:22:35 +0800 Subject: [PATCH] feat: unzip zip file when unload --- README.md | 7 +++++++ src/server.rs | 27 ++++++++++++++++++++++++++- src/static/index.css | 4 ++++ src/static/index.html | 6 ++++++ src/static/index.js | 35 ++++++++++++----------------------- 5 files changed, 55 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 27aa55e..978c015 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ Duf is a simple file server. - Upload files - Delete files - Basic authentication +- Unzip zip file when upload - Easy to use with curl ## Install @@ -71,6 +72,12 @@ Upload a 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 ``` diff --git a/src/server.rs b/src/server.rs index 5f28ea1..5c2c909 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,6 +1,7 @@ use crate::{Args, BoxResult}; use async_walkdir::WalkDir; +use async_zip::read::seek::ZipFileReader; use async_zip::write::{EntryOptions, ZipFileWriter}; use async_zip::Compression; use futures::stream::StreamExt; @@ -157,7 +158,7 @@ impl InnerService { 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 .body_mut() @@ -169,6 +170,30 @@ impl InnerService { 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)); } diff --git a/src/static/index.css b/src/static/index.css index c8a2457..748e8ed 100644 --- a/src/static/index.css +++ b/src/static/index.css @@ -8,6 +8,10 @@ body { width: 700px; } +.hidden { + display: none; +} + .head { display: flex; flex-wrap: wrap; diff --git a/src/static/index.html b/src/static/index.html index 8a5eb3d..6cb6614 100644 --- a/src/static/index.html +++ b/src/static/index.html @@ -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> </a> </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> <form class="searchbar"> <div class="icon"> diff --git a/src/static/index.js b/src/static/index.js index f819542..e09838c 100644 --- a/src/static/index.js +++ b/src/static/index.js @@ -1,6 +1,6 @@ -var uploaderIdx = 0; -var breadcrumb, paths, readonly; var $toolbox, $tbody, $breadcrumb, $uploaders, $uploadControl; +var uploaderIdx = 0; +var baseDir; class Uploader { idx = 0; @@ -14,6 +14,9 @@ class Uploader { upload() { var { file, idx } = this; var url = getUrl(file.name); + if (file.name == baseDir + ".zip") { + url += "?unzip"; + } $uploaders.insertAdjacentHTML("beforeend", ` <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> @@ -55,6 +58,7 @@ function addBreadcrumb(value) { } if (i === len - 1) { $breadcrumb.insertAdjacentHTML("beforeend", `<b>${name}</b>`); + baseDir = name; } else if (i === 0) { $breadcrumb.insertAdjacentHTML("beforeend", `<a href="/"><b>${name}</b></a>`); } else { @@ -83,7 +87,7 @@ function addPath(file, index) { </a> </div>`; } - if (!readonly) { + if (!DATA.readonly) { actionDelete = ` <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> @@ -108,7 +112,7 @@ ${actionCell} } async function deletePath(index) { - var file = paths[index]; + var file = DATA.paths[index]; if (!file) return; var ajax = new XMLHttpRequest(); @@ -121,17 +125,6 @@ async function deletePath(index) { 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) { var url = location.href.split('?')[0]; if (!url.endsWith("/")) url += "/"; @@ -177,19 +170,15 @@ function formatSize(size) { function ready() { - breadcrumb = DATA.breadcrumb; - paths = DATA.paths; - readonly = DATA.readonly; $toolbox = document.querySelector(".toolbox"); $tbody = document.querySelector(".main tbody"); $breadcrumb = document.querySelector(".breadcrumb"); $uploaders = document.querySelector(".uploaders"); - $uploadControl = document.querySelector(".upload-control"); - addBreadcrumb(breadcrumb); - paths.forEach((file, index) => addPath(file, index)); - if (!readonly) { - addUploadControl(); + addBreadcrumb(DATA.breadcrumb); + DATA.paths.forEach((file, index) => addPath(file, index)); + if (!DATA.readonly) { + document.querySelector(".upload-control").classList.remove(["hidden"]); document.getElementById("file").addEventListener("change", e => { var files = e.target.files; for (var file of files) {