Emit sized body when file length is known in 'NamedFile'.

This commit is contained in:
Wilson Birney 2017-11-27 17:58:50 -05:00 committed by Sergio Benitez
parent aad97e6be0
commit 4749b541a9
1 changed files with 7 additions and 2 deletions

View File

@ -1,11 +1,12 @@
use std::fs::File; use std::fs::File;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::io::{self, BufReader}; use std::io;
use std::ops::{Deref, DerefMut}; use std::ops::{Deref, DerefMut};
use request::Request; use request::Request;
use response::{Response, Responder}; use response::{Response, Responder};
use http::{Status, ContentType}; use http::{Status, ContentType};
use response::Body;
/// A file with an associated name; responds with the Content-Type based on the /// A file with an associated name; responds with the Content-Type based on the
/// file extension. /// file extension.
@ -88,7 +89,11 @@ impl Responder<'static> for NamedFile {
} }
} }
response.set_streamed_body(BufReader::new(self.take_file())); match self.0.metadata() {
Ok(meta) => response.set_raw_body(Body::Sized(self.take_file(), meta.len())),
Err(_) => response.set_streamed_body(self.take_file())
}
Ok(response) Ok(response)
} }
} }