mirror of
https://github.com/sigoden/dufs.git
synced 2026-04-09 00:59:02 +03:00
feat: download folder as zip file
This commit is contained in:
11
src/args.rs
11
src/args.rs
@@ -29,10 +29,9 @@ fn app() -> clap::Command<'static> {
|
||||
.allow_invalid_utf8(true)
|
||||
.help("Path to a directory for serving files");
|
||||
|
||||
let arg_readonly = Arg::new("readonly")
|
||||
.short('r')
|
||||
.long("readonly")
|
||||
.help("Only serve static files, no operations like upload and delete");
|
||||
let arg_static = Arg::new("static")
|
||||
.long("static")
|
||||
.help("Only serve static files, not allowed to upload or delete file");
|
||||
|
||||
let arg_auth = Arg::new("auth")
|
||||
.short('a')
|
||||
@@ -49,7 +48,7 @@ fn app() -> clap::Command<'static> {
|
||||
.arg(arg_address)
|
||||
.arg(arg_port)
|
||||
.arg(arg_path)
|
||||
.arg(arg_readonly)
|
||||
.arg(arg_static)
|
||||
.arg(arg_auth)
|
||||
.arg(arg_no_log)
|
||||
}
|
||||
@@ -78,7 +77,7 @@ impl Args {
|
||||
let port = matches.value_of_t::<u16>("port")?;
|
||||
let path = matches.value_of_os("path").unwrap_or_default();
|
||||
let path = Args::parse_path(path)?;
|
||||
let readonly = matches.is_present("readonly");
|
||||
let readonly = matches.is_present("static");
|
||||
let auth = matches.value_of("auth").map(|v| v.to_owned());
|
||||
let log = !matches.is_present("no-log");
|
||||
|
||||
|
||||
@@ -46,6 +46,7 @@ html {
|
||||
|
||||
.upload-control {
|
||||
cursor: pointer;
|
||||
padding-left: 0.25em;
|
||||
}
|
||||
|
||||
.main {
|
||||
@@ -80,6 +81,7 @@ html {
|
||||
|
||||
.main .cell-actions {
|
||||
width: 100px;
|
||||
display: flex;
|
||||
padding-left: 0.6em;
|
||||
}
|
||||
|
||||
@@ -108,6 +110,10 @@ html {
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
.action-btn {
|
||||
padding-left: 0.4em;
|
||||
}
|
||||
|
||||
.uploaders {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
|
||||
@@ -10,6 +10,11 @@
|
||||
<body>
|
||||
<div class="head">
|
||||
<div class="breadcrumb"></div>
|
||||
<div>
|
||||
<a href="?zip" title="Download folder as a .zip 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 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>
|
||||
<div class="main">
|
||||
<div class="uploaders">
|
||||
@@ -20,6 +25,7 @@
|
||||
<th class="cell-name">Name</th>
|
||||
<th class="cell-mtime">Date modify</th>
|
||||
<th class="cell-size">Size</th>
|
||||
<th class="cell-actions">Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@@ -100,22 +106,45 @@
|
||||
}
|
||||
|
||||
function addPath(file, index) {
|
||||
const actionTd = readonly ? "" : `
|
||||
const url = encodeURI(file.path);
|
||||
let actionDelete = "";
|
||||
let actionDownload = "";
|
||||
if (file.path_type.endsWith("Dir")) {
|
||||
actionDownload = `
|
||||
<div class="action-btn">
|
||||
<a href="${url}?zip" title="Download folder as a .zip 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 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>`;
|
||||
} else {
|
||||
actionDownload = `
|
||||
<div class="action-btn" >
|
||||
<a href="${url}" title="Download file" download>
|
||||
<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>`;
|
||||
}
|
||||
if (!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>
|
||||
</div>`;
|
||||
}
|
||||
const actionCell = `
|
||||
<td class="cell-actions">
|
||||
<div onclick="deletePath(${index})" class="delete-btn" id="deleteBtn${index}" title="Delete ${file.name}">
|
||||
<svg width="18" height="18" viewBox="0 0 40 40"><path d="M28,40H11.8c-3.3,0-5.9-2.7-5.9-5.9V16c0-0.6,0.4-1,1-1s1,0.4,1,1v18.1c0,2.2,1.8,3.9,3.9,3.9H28c2.2,0,3.9-1.8,3.9-3.9V16 c0-0.6,0.4-1,1-1s1,0.4,1,1v18.1C33.9,37.3,31.2,40,28,40z"/></g><g><path d="M33.3,4.9h-7.6C25.2,2.1,22.8,0,19.9,0s-5.3,2.1-5.8,4.9H6.5c-2.3,0-4.1,1.8-4.1,4.1S4.2,13,6.5,13h26.9 c2.3,0,4.1-1.8,4.1-4.1S35.6,4.9,33.3,4.9z M19.9,2c1.8,0,3.3,1.2,3.7,2.9h-7.5C16.6,3.2,18.1,2,19.9,2z M33.3,11H6.5 c-1.1,0-2.1-0.9-2.1-2.1c0-1.1,0.9-2.1,2.1-2.1h26.9c1.1,0,2.1,0.9,2.1,2.1C35.4,10.1,34.5,11,33.3,11z"/></g><g><path d="M12.9,35.1c-0.6,0-1-0.4-1-1V17.4c0-0.6,0.4-1,1-1s1,0.4,1,1v16.7C13.9,34.6,13.4,35.1,12.9,35.1z"/></g><g><path d="M26.9,35.1c-0.6,0-1-0.4-1-1V17.4c0-0.6,0.4-1,1-1s1,0.4,1,1v16.7C27.9,34.6,27.4,35.1,26.9,35.1z"/></g><g><path d="M19.9,35.1c-0.6,0-1-0.4-1-1V17.4c0-0.6,0.4-1,1-1s1,0.4,1,1v16.7C20.9,34.6,20.4,35.1,19.9,35.1z"/></svg>
|
||||
</div>
|
||||
${actionDownload}
|
||||
${actionDelete}
|
||||
</td>`
|
||||
|
||||
$tbody.insertAdjacentHTML("beforeend", `
|
||||
<tr id="addPath${index}">
|
||||
<td class="path cell-name">
|
||||
<div>${getSvg(file.path_type)}</div>
|
||||
<a href="${encodeURI(file.path)}" title="${file.name}">${file.name}</a>
|
||||
<a href="${url}" title="${file.name}">${file.name}</a>
|
||||
</td>
|
||||
<td class="cell-mtime">${formatMtime(file.mtime)}</td>
|
||||
<td class="cell-size">${formatSize(file.size)}</td>
|
||||
${actionTd}
|
||||
${actionCell}
|
||||
</tr>`)
|
||||
}
|
||||
|
||||
@@ -137,7 +166,7 @@
|
||||
$head.insertAdjacentHTML("beforeend", `
|
||||
<div class="upload-control" title="Upload file">
|
||||
<label for="file">
|
||||
<svg viewBox="0 0 384.97 384.97" width="14" height="14"><path d="M372.939,264.641c-6.641,0-12.03,5.39-12.03,12.03v84.212H24.061v-84.212c0-6.641-5.39-12.03-12.03-12.03 S0,270.031,0,276.671v96.242c0,6.641,5.39,12.03,12.03,12.03h360.909c6.641,0,12.03-5.39,12.03-12.03v-96.242 C384.97,270.019,379.58,264.641,372.939,264.641z"></path><path d="M117.067,103.507l63.46-62.558v235.71c0,6.641,5.438,12.03,12.151,12.03c6.713,0,12.151-5.39,12.151-12.03V40.95 l63.46,62.558c4.74,4.704,12.439,4.704,17.179,0c4.74-4.704,4.752-12.319,0-17.011l-84.2-82.997 c-4.692-4.656-12.584-4.608-17.191,0L99.888,86.496c-4.752,4.704-4.74,12.319,0,17.011 C104.628,108.211,112.327,108.211,117.067,103.507z"></path></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 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>
|
||||
@@ -187,7 +216,6 @@
|
||||
paths.forEach((file, index) => addPath(file, index));
|
||||
if (!readonly) {
|
||||
addUploadControl();
|
||||
document.querySelector(".main thead tr").insertAdjacentHTML("beforeend", `<th class="cell-actions">Actions</th>`);
|
||||
document.getElementById("file").addEventListener("change", e => {
|
||||
const files = e.target.files;
|
||||
for (const file of files) {
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
use crate::{Args, BoxResult};
|
||||
|
||||
use async_walkdir::WalkDir;
|
||||
use async_zip::write::{EntryOptions, ZipFileWriter};
|
||||
use async_zip::Compression;
|
||||
use futures::stream::StreamExt;
|
||||
use futures::TryStreamExt;
|
||||
use hyper::header::HeaderValue;
|
||||
use hyper::service::{make_service_fn, service_fn};
|
||||
@@ -10,8 +14,11 @@ use std::convert::Infallible;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Arc;
|
||||
use std::time::SystemTime;
|
||||
use tokio::fs::File;
|
||||
use tokio::io::AsyncWrite;
|
||||
use tokio::{fs, io};
|
||||
use tokio_util::codec::{BytesCodec, FramedRead};
|
||||
use tokio_util::io::ReaderStream;
|
||||
use tokio_util::io::StreamReader;
|
||||
|
||||
type Request = hyper::Request<Body>;
|
||||
@@ -100,7 +107,11 @@ impl InnerService {
|
||||
match fs::metadata(&path).await {
|
||||
Ok(meta) => {
|
||||
if meta.is_dir() {
|
||||
self.handle_send_dir(path.as_path()).await
|
||||
if req.uri().query().map(|v| v == "zip").unwrap_or_default() {
|
||||
self.handle_send_dir_zip(path.as_path()).await
|
||||
} else {
|
||||
self.handle_send_dir(path.as_path()).await
|
||||
}
|
||||
} else {
|
||||
self.handle_send_file(path.as_path()).await
|
||||
}
|
||||
@@ -175,6 +186,14 @@ impl InnerService {
|
||||
Ok(hyper::Response::builder().body(output.into()).unwrap())
|
||||
}
|
||||
|
||||
async fn handle_send_dir_zip(&self, path: &Path) -> BoxResult<Response> {
|
||||
let (mut writer, reader) = tokio::io::duplex(65536);
|
||||
dir_zip(&mut writer, path).await?;
|
||||
let stream = ReaderStream::new(reader);
|
||||
let body = Body::wrap_stream(stream);
|
||||
Ok(Response::new(body))
|
||||
}
|
||||
|
||||
async fn handle_send_file(&self, path: &Path) -> BoxResult<Response> {
|
||||
let file = fs::File::open(path).await?;
|
||||
let stream = FramedRead::new(file, BytesCodec::new());
|
||||
@@ -294,3 +313,27 @@ fn normalize_path<P: AsRef<Path>>(path: P) -> String {
|
||||
path.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
async fn dir_zip<W: AsyncWrite + Unpin>(writer: &mut W, dir: &Path) -> BoxResult<()> {
|
||||
let mut writer = ZipFileWriter::new(writer);
|
||||
let mut walkdir = WalkDir::new(dir);
|
||||
while let Some(entry) = walkdir.next().await {
|
||||
if let Ok(entry) = entry {
|
||||
let meta = fs::symlink_metadata(entry.path()).await?;
|
||||
if meta.is_file() {
|
||||
let filepath = entry.path();
|
||||
let filename = match filepath.strip_prefix(dir).ok().and_then(|v| v.to_str()) {
|
||||
Some(v) => v,
|
||||
None => continue,
|
||||
};
|
||||
let entry_options = EntryOptions::new(filename.to_owned(), Compression::Deflate);
|
||||
let mut file = File::open(&filepath).await?;
|
||||
let mut file_writer = writer.write_entry_stream(entry_options).await?;
|
||||
io::copy(&mut file, &mut file_writer).await?;
|
||||
file_writer.close().await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
writer.close().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user