mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-28 01:32:57 +01:00
Merge pull request #239 from max-frai/master
Add ability to set encoding for exact NamedFile.
This commit is contained in:
commit
174fb0b5f4
36
src/fs.rs
36
src/fs.rs
@ -38,6 +38,7 @@ pub struct NamedFile {
|
|||||||
md: Metadata,
|
md: Metadata,
|
||||||
modified: Option<SystemTime>,
|
modified: Option<SystemTime>,
|
||||||
cpu_pool: Option<CpuPool>,
|
cpu_pool: Option<CpuPool>,
|
||||||
|
encoding: Option<ContentEncoding>,
|
||||||
only_get: bool,
|
only_get: bool,
|
||||||
status_code: StatusCode,
|
status_code: StatusCode,
|
||||||
}
|
}
|
||||||
@ -58,12 +59,14 @@ impl NamedFile {
|
|||||||
let path = path.as_ref().to_path_buf();
|
let path = path.as_ref().to_path_buf();
|
||||||
let modified = md.modified().ok();
|
let modified = md.modified().ok();
|
||||||
let cpu_pool = None;
|
let cpu_pool = None;
|
||||||
|
let encoding = None;
|
||||||
Ok(NamedFile {
|
Ok(NamedFile {
|
||||||
path,
|
path,
|
||||||
file,
|
file,
|
||||||
md,
|
md,
|
||||||
modified,
|
modified,
|
||||||
cpu_pool,
|
cpu_pool,
|
||||||
|
encoding,
|
||||||
only_get: false,
|
only_get: false,
|
||||||
status_code: StatusCode::OK,
|
status_code: StatusCode::OK,
|
||||||
})
|
})
|
||||||
@ -114,6 +117,19 @@ impl NamedFile {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set content encoding for serving this file
|
||||||
|
#[inline]
|
||||||
|
pub fn set_content_encoding(mut self, enc: ContentEncoding) -> Self {
|
||||||
|
self.encoding = Some(enc);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get content encoding used for serving this file
|
||||||
|
#[inline]
|
||||||
|
pub fn content_encoding(&self) -> Option<ContentEncoding> {
|
||||||
|
self.encoding
|
||||||
|
}
|
||||||
|
|
||||||
fn etag(&self) -> Option<header::EntityTag> {
|
fn etag(&self) -> Option<header::EntityTag> {
|
||||||
// This etag format is similar to Apache's.
|
// This etag format is similar to Apache's.
|
||||||
self.modified.as_ref().map(|mtime| {
|
self.modified.as_ref().map(|mtime| {
|
||||||
@ -219,6 +235,9 @@ impl Responder for NamedFile {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
if let Some(current_encoding) = self.encoding {
|
||||||
|
resp.content_encoding(current_encoding);
|
||||||
|
}
|
||||||
let reader = ChunkedReadFile {
|
let reader = ChunkedReadFile {
|
||||||
size: self.md.len(),
|
size: self.md.len(),
|
||||||
offset: 0,
|
offset: 0,
|
||||||
@ -264,6 +283,9 @@ impl Responder for NamedFile {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let mut resp = HttpResponse::build(self.status_code);
|
let mut resp = HttpResponse::build(self.status_code);
|
||||||
|
if let Some(current_encoding) = self.encoding {
|
||||||
|
resp.content_encoding(current_encoding);
|
||||||
|
}
|
||||||
|
|
||||||
resp.if_some(self.path().extension(), |ext, resp| {
|
resp.if_some(self.path().extension(), |ext, resp| {
|
||||||
resp.set(header::ContentType(get_mime_type(&ext.to_string_lossy())));
|
resp.set(header::ContentType(get_mime_type(&ext.to_string_lossy())));
|
||||||
@ -941,6 +963,20 @@ mod tests {
|
|||||||
assert_eq!(resp.status(), StatusCode::METHOD_NOT_ALLOWED);
|
assert_eq!(resp.status(), StatusCode::METHOD_NOT_ALLOWED);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_named_file_content_encoding() {
|
||||||
|
let req = TestRequest::default().method(Method::GET).finish();
|
||||||
|
let file = NamedFile::open("Cargo.toml").unwrap();
|
||||||
|
|
||||||
|
assert!(file.content_encoding().is_none());
|
||||||
|
let resp = file.set_content_encoding(ContentEncoding::Identity)
|
||||||
|
.respond_to(&req)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert!(resp.content_encoding().is_some());
|
||||||
|
assert_eq!(resp.content_encoding().unwrap().as_str(), "identity");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_named_file_any_method() {
|
fn test_named_file_any_method() {
|
||||||
let req = TestRequest::default().method(Method::POST).finish();
|
let req = TestRequest::default().method(Method::POST).finish();
|
||||||
|
Loading…
Reference in New Issue
Block a user