mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-27 17:52:56 +01:00
Use NamedFile with an existing File (#670)
This commit is contained in:
parent
f5bec968c7
commit
9968afe4a6
@ -1,5 +1,11 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
|
## [x.x.xx] - xxxx-xx-xx
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
* Add `from_file` and `from_file_with_config` to `NamedFile` to allow sending files without a known path. #670
|
||||||
|
|
||||||
## [0.7.18] - 2019-01-10
|
## [0.7.18] - 2019-01-10
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
63
src/fs.rs
63
src/fs.rs
@ -120,6 +120,32 @@ pub struct NamedFile<C = DefaultConfig> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl 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
|
||||||
|
///
|
||||||
|
/// ```no_run
|
||||||
|
/// extern crate actix_web;
|
||||||
|
///
|
||||||
|
/// use actix_web::fs::NamedFile;
|
||||||
|
/// use std::io::{self, Write};
|
||||||
|
/// use std::env;
|
||||||
|
/// use std::fs::File;
|
||||||
|
///
|
||||||
|
/// fn main() -> io::Result<()> {
|
||||||
|
/// let mut file = File::create("foo.txt")?;
|
||||||
|
/// file.write_all(b"Hello, world!")?;
|
||||||
|
/// let named_file = NamedFile::from_file(file, "bar.txt")?;
|
||||||
|
/// Ok(())
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
pub fn from_file<P: AsRef<Path>>(file: File, path: P) -> io::Result<NamedFile> {
|
||||||
|
Self::from_file_with_config(file, path, DefaultConfig)
|
||||||
|
}
|
||||||
|
|
||||||
/// Attempts to open a file in read-only mode.
|
/// Attempts to open a file in read-only mode.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
@ -135,16 +161,29 @@ impl NamedFile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<C: StaticFileConfig> NamedFile<C> {
|
impl<C: StaticFileConfig> NamedFile<C> {
|
||||||
/// 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
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```no_run
|
||||||
/// use actix_web::fs::{DefaultConfig, NamedFile};
|
/// extern crate actix_web;
|
||||||
///
|
///
|
||||||
/// let file = NamedFile::open_with_config("foo.txt", DefaultConfig);
|
/// use actix_web::fs::{DefaultConfig, NamedFile};
|
||||||
|
/// use std::io::{self, Write};
|
||||||
|
/// use std::env;
|
||||||
|
/// use std::fs::File;
|
||||||
|
///
|
||||||
|
/// fn main() -> io::Result<()> {
|
||||||
|
/// let mut file = File::create("foo.txt")?;
|
||||||
|
/// file.write_all(b"Hello, world!")?;
|
||||||
|
/// let named_file = NamedFile::from_file_with_config(file, "bar.txt", DefaultConfig)?;
|
||||||
|
/// Ok(())
|
||||||
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn open_with_config<P: AsRef<Path>>(path: P, _: C) -> io::Result<NamedFile<C>> {
|
pub fn from_file_with_config<P: AsRef<Path>>(file: File, path: P, _: C) -> io::Result<NamedFile<C>> {
|
||||||
let path = path.as_ref().to_path_buf();
|
let path = path.as_ref().to_path_buf();
|
||||||
|
|
||||||
// Get the name of the file and use it to construct default Content-Type
|
// Get the name of the file and use it to construct default Content-Type
|
||||||
@ -169,7 +208,6 @@ impl<C: StaticFileConfig> NamedFile<C> {
|
|||||||
(ct, cd)
|
(ct, cd)
|
||||||
};
|
};
|
||||||
|
|
||||||
let file = File::open(&path)?;
|
|
||||||
let md = file.metadata()?;
|
let md = file.metadata()?;
|
||||||
let modified = md.modified().ok();
|
let modified = md.modified().ok();
|
||||||
let cpu_pool = None;
|
let cpu_pool = None;
|
||||||
@ -188,6 +226,19 @@ impl<C: StaticFileConfig> NamedFile<C> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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<P: AsRef<Path>>(path: P, config: C) -> io::Result<NamedFile<C>> {
|
||||||
|
Self::from_file_with_config(File::open(&path)?, path, config)
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns reference to the underlying `File` object.
|
/// Returns reference to the underlying `File` object.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn file(&self) -> &File {
|
pub fn file(&self) -> &File {
|
||||||
|
Loading…
Reference in New Issue
Block a user