From 412d42e338b00936b78c2b1cb40fc83043cccb80 Mon Sep 17 00:00:00 2001 From: sigoden Date: Tue, 31 May 2022 14:39:07 +0800 Subject: [PATCH] fix: downloaded zip file has no.zip ext in firefox close #2 --- src/server.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/server.rs b/src/server.rs index 5959b18..a317fb2 100644 --- a/src/server.rs +++ b/src/server.rs @@ -10,7 +10,9 @@ use headers::{ AccessControlAllowHeaders, AccessControlAllowOrigin, ContentRange, ContentType, ETag, HeaderMap, HeaderMapExt, IfModifiedSince, IfNoneMatch, IfRange, LastModified, Range, }; -use hyper::header::{HeaderValue, ACCEPT, CONTENT_TYPE, ORIGIN, RANGE, WWW_AUTHENTICATE}; +use hyper::header::{ + HeaderValue, ACCEPT, CONTENT_DISPOSITION, CONTENT_TYPE, ORIGIN, RANGE, WWW_AUTHENTICATE, +}; use hyper::service::{make_service_fn, service_fn}; use hyper::{Body, Method, StatusCode}; use percent_encoding::percent_decode; @@ -271,6 +273,7 @@ impl InnerService { async fn handle_zip_dir(&self, path: &Path, res: &mut Response) -> BoxResult<()> { let (mut writer, reader) = tokio::io::duplex(BUF_SIZE); + let filename = path.file_name().unwrap().to_str().unwrap(); let path = path.to_owned(); tokio::spawn(async move { if let Err(e) = dir_zip(&mut writer, &path).await { @@ -279,6 +282,10 @@ impl InnerService { }); let stream = ReaderStream::new(reader); *res.body_mut() = Body::wrap_stream(stream); + res.headers_mut().insert( + CONTENT_DISPOSITION, + HeaderValue::from_str(&format!("attachment; filename=\"{}.zip\"", filename,)).unwrap(), + ); Ok(()) }