From 492c0725640387975913f14662a855ac28c74b28 Mon Sep 17 00:00:00 2001 From: Sven-Hendrik Haase Date: Fri, 27 Apr 2018 09:49:55 +0200 Subject: [PATCH] Add Content-Disposition to NamedFile (fixes #172) --- src/fs.rs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/fs.rs b/src/fs.rs index ce0e42d57..19f8f9ee0 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -205,6 +205,9 @@ impl Responder for NamedFile { resp.set(header::ContentType(get_mime_type( &ext.to_string_lossy(), ))); + }).if_some(self.path().file_name(), |file_name, resp| { + resp.header("Content-Disposition", + format!("attachment; filename={}", file_name.to_string_lossy())); }); let reader = ChunkedReadFile { size: self.md.len(), @@ -256,12 +259,14 @@ impl Responder for NamedFile { resp.set(header::ContentType(get_mime_type( &ext.to_string_lossy(), ))); + }).if_some(self.path().file_name(), |file_name, resp| { + resp.header("Content-Disposition", + format!("attachment; filename={}", file_name.to_string_lossy())); }).if_some(last_modified, |lm, resp| { resp.set(header::LastModified(lm)); - }) - .if_some(etag, |etag, resp| { - resp.set(header::ETag(etag)); - }); + }).if_some(etag, |etag, resp| { + resp.set(header::ETag(etag)); + }); if precondition_failed { return Ok(resp.status(StatusCode::PRECONDITION_FAILED).finish()); @@ -612,7 +617,11 @@ mod tests { assert_eq!( resp.headers().get(header::CONTENT_TYPE).unwrap(), "text/x-toml" - ) + ); + assert_eq!( + resp.headers().get(header::CONTENT_DISPOSITION).unwrap(), + "attachment; filename=Cargo.toml" + ); } #[test] @@ -634,6 +643,10 @@ mod tests { resp.headers().get(header::CONTENT_TYPE).unwrap(), "text/x-toml" ); + assert_eq!( + resp.headers().get(header::CONTENT_DISPOSITION).unwrap(), + "attachment; filename=Cargo.toml" + ); assert_eq!(resp.status(), StatusCode::NOT_FOUND); }