mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-27 17:52:56 +01:00
Add Content-Disposition to NamedFile (fixes #172)
This commit is contained in:
parent
fd876efa68
commit
492c072564
23
src/fs.rs
23
src/fs.rs
@ -205,6 +205,9 @@ impl Responder for NamedFile {
|
|||||||
resp.set(header::ContentType(get_mime_type(
|
resp.set(header::ContentType(get_mime_type(
|
||||||
&ext.to_string_lossy(),
|
&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 {
|
let reader = ChunkedReadFile {
|
||||||
size: self.md.len(),
|
size: self.md.len(),
|
||||||
@ -256,12 +259,14 @@ impl Responder for NamedFile {
|
|||||||
resp.set(header::ContentType(get_mime_type(
|
resp.set(header::ContentType(get_mime_type(
|
||||||
&ext.to_string_lossy(),
|
&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| {
|
}).if_some(last_modified, |lm, resp| {
|
||||||
resp.set(header::LastModified(lm));
|
resp.set(header::LastModified(lm));
|
||||||
})
|
}).if_some(etag, |etag, resp| {
|
||||||
.if_some(etag, |etag, resp| {
|
resp.set(header::ETag(etag));
|
||||||
resp.set(header::ETag(etag));
|
});
|
||||||
});
|
|
||||||
|
|
||||||
if precondition_failed {
|
if precondition_failed {
|
||||||
return Ok(resp.status(StatusCode::PRECONDITION_FAILED).finish());
|
return Ok(resp.status(StatusCode::PRECONDITION_FAILED).finish());
|
||||||
@ -612,7 +617,11 @@ mod tests {
|
|||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
||||||
"text/x-toml"
|
"text/x-toml"
|
||||||
)
|
);
|
||||||
|
assert_eq!(
|
||||||
|
resp.headers().get(header::CONTENT_DISPOSITION).unwrap(),
|
||||||
|
"attachment; filename=Cargo.toml"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -634,6 +643,10 @@ mod tests {
|
|||||||
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
||||||
"text/x-toml"
|
"text/x-toml"
|
||||||
);
|
);
|
||||||
|
assert_eq!(
|
||||||
|
resp.headers().get(header::CONTENT_DISPOSITION).unwrap(),
|
||||||
|
"attachment; filename=Cargo.toml"
|
||||||
|
);
|
||||||
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user