From 62696b45fde4baef6fa09b4e82470eefca1b5149 Mon Sep 17 00:00:00 2001 From: sigoden 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 @@ +