diff --git a/Cargo.toml b/Cargo.toml index f927e24e8..b54d866fe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -129,6 +129,7 @@ tokio-uds = { version="0.2", optional = true } [dev-dependencies] env_logger = "0.6" serde_derive = "1.0" +tempfile = "3.0" [build-dependencies] version_check = "0.1" diff --git a/src/fs.rs b/src/fs.rs index 47bd81a78..91a43e117 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -120,6 +120,32 @@ pub struct NamedFile { } impl NamedFile { + /// Creates an instance from a previously opened file. + /// + /// The given `path` need not exist and is only used to determine the `ContentType` and + /// `ContentDisposition` headers. + /// + /// # Examples + /// + /// ```rust + /// extern crate tempfile; + /// extern crate actix_web; + /// + /// use actix_web::fs::NamedFile; + /// use tempfile::tempfile; + /// use std::io::{self, Write}; + /// + /// fn main() -> io::Result<()> { + /// let mut file = tempfile()?; + /// file.write_all(b"Hello, world!")?; + /// let named_file = NamedFile::from_file(file, "foo.txt")?; + /// Ok(()) + /// } + /// ``` + pub fn from_file>(file: File, path: P) -> io::Result { + Self::from_file_with_config(file, path, DefaultConfig) + } + /// Attempts to open a file in read-only mode. /// /// # Examples @@ -135,16 +161,29 @@ impl NamedFile { } impl NamedFile { - /// Attempts to open a file in read-only mode using provided configiration. + /// Creates an instance from a previously opened file using the provided configuration. + /// + /// The given `path` need not exist and is only used to determine the `ContentType` and + /// `ContentDisposition` headers. /// /// # Examples /// /// ```rust - /// use actix_web::fs::{DefaultConfig, NamedFile}; + /// extern crate tempfile; + /// extern crate actix_web; /// - /// let file = NamedFile::open_with_config("foo.txt", DefaultConfig); + /// use actix_web::fs::{NamedFile, DefaultConfig}; + /// use tempfile::tempfile; + /// use std::io::{self, Write}; + /// + /// fn main() -> io::Result<()> { + /// let mut file = tempfile()?; + /// file.write_all(b"Hello, world!")?; + /// let named_file = NamedFile::from_file_with_config(file, "foo.txt", DefaultConfig)?; + /// Ok(()) + /// } /// ``` - pub fn open_with_config>(path: P, _: C) -> io::Result> { + pub fn from_file_with_config>(file: File, path: P, _: C) -> io::Result> { let path = path.as_ref().to_path_buf(); // Get the name of the file and use it to construct default Content-Type @@ -169,7 +208,6 @@ impl NamedFile { (ct, cd) }; - let file = File::open(&path)?; let md = file.metadata()?; let modified = md.modified().ok(); let cpu_pool = None; @@ -187,6 +225,18 @@ impl NamedFile { _cd_map: PhantomData, }) } + /// Attempts to open a file in read-only mode using provided configuration. + /// + /// # Examples + /// + /// ```rust + /// use actix_web::fs::{DefaultConfig, NamedFile}; + /// + /// let file = NamedFile::open_with_config("foo.txt", DefaultConfig); + /// ``` + pub fn open_with_config>(path: P, config: C) -> io::Result> { + Self::from_file_with_config(File::open(&path)?, path, config) + } /// Returns reference to the underlying `File` object. #[inline]