handle_static_file_download: optimize small files
Avoid chuncked transfer for small files.
This commit is contained in:
parent
579fbe7dc8
commit
78d0783b00
30
src/main.rs
30
src/main.rs
|
@ -167,7 +167,26 @@ fn handle_sync_api_request<'a>(
|
|||
|
||||
fn handle_static_file_download(filename: PathBuf) -> BoxFut {
|
||||
|
||||
let response = File::open(filename)
|
||||
let response = tokio::fs::metadata(filename.clone())
|
||||
.map_err(|err| format_err!("File access problems: {}", err))
|
||||
.and_then(|metadata| {
|
||||
println!("TEST METADATA {:?} {}", metadata, metadata.len());
|
||||
|
||||
if metadata.len() < 1024*8 {
|
||||
println!("SMALL SIZED FILE");
|
||||
Either::A(File::open(filename)
|
||||
.map_err(|err| format_err!("File open failed: {}", err))
|
||||
.and_then(|file| {
|
||||
let buf: Vec<u8> = Vec::new();
|
||||
tokio::io::read_to_end(file, buf)
|
||||
.map_err(|err| format_err!("File read failed: {}", err))
|
||||
.and_then(|data| Ok(Response::new(data.1.into())))
|
||||
}))
|
||||
|
||||
} else {
|
||||
Either::B(
|
||||
File::open(filename)
|
||||
.map_err(|err| format_err!("File open failed: {}", err))
|
||||
.and_then(|file| {
|
||||
let payload = tokio_codec::FramedRead::new(file, tokio_codec::BytesCodec::new()).
|
||||
map(|bytes| {
|
||||
|
@ -180,13 +199,8 @@ fn handle_static_file_download(filename: PathBuf) -> BoxFut {
|
|||
.status(StatusCode::OK)
|
||||
.body(body)
|
||||
.unwrap())
|
||||
})
|
||||
.or_else(|err| {
|
||||
// fixme: set content type and other headers
|
||||
Ok(Response::builder()
|
||||
.status(StatusCode::NOT_FOUND)
|
||||
.body(format!("File access problems: {}\n", err).into())
|
||||
.unwrap())
|
||||
}))
|
||||
}
|
||||
});
|
||||
|
||||
return Box::new(response);
|
||||
|
|
Loading…
Reference in New Issue