From 4749b541a9ba1e09b212ce926bb59171092982e9 Mon Sep 17 00:00:00 2001 From: Wilson Birney Date: Mon, 27 Nov 2017 17:58:50 -0500 Subject: [PATCH] Emit sized body when file length is known in 'NamedFile'. --- lib/src/response/named_file.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/src/response/named_file.rs b/lib/src/response/named_file.rs index c2de0e37..b6b08395 100644 --- a/lib/src/response/named_file.rs +++ b/lib/src/response/named_file.rs @@ -1,11 +1,12 @@ use std::fs::File; use std::path::{Path, PathBuf}; -use std::io::{self, BufReader}; +use std::io; use std::ops::{Deref, DerefMut}; use request::Request; use response::{Response, Responder}; use http::{Status, ContentType}; +use response::Body; /// A file with an associated name; responds with the Content-Type based on the /// 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) } }