mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-27 17:52:56 +01:00
rename to Files
This commit is contained in:
parent
6e638129c5
commit
e56691bcf2
@ -27,8 +27,8 @@ path = "src/lib.rs"
|
|||||||
[workspace]
|
[workspace]
|
||||||
members = [
|
members = [
|
||||||
".",
|
".",
|
||||||
|
"actix-files",
|
||||||
"actix-session",
|
"actix-session",
|
||||||
"actix-staticfiles",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
[package.metadata.docs.rs]
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-staticfiles"
|
name = "actix-files"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Static files support for Actix web."
|
description = "Static files support for Actix web."
|
@ -3,7 +3,7 @@ use derive_more::Display;
|
|||||||
|
|
||||||
/// Errors which can occur when serving static files.
|
/// Errors which can occur when serving static files.
|
||||||
#[derive(Display, Debug, PartialEq)]
|
#[derive(Display, Debug, PartialEq)]
|
||||||
pub enum StaticFilesError {
|
pub enum FilesError {
|
||||||
/// Path is not a directory
|
/// Path is not a directory
|
||||||
#[display(fmt = "Path is not a directory. Unable to serve static files")]
|
#[display(fmt = "Path is not a directory. Unable to serve static files")]
|
||||||
IsNotDirectory,
|
IsNotDirectory,
|
||||||
@ -13,8 +13,8 @@ pub enum StaticFilesError {
|
|||||||
IsDirectory,
|
IsDirectory,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return `NotFound` for `StaticFilesError`
|
/// Return `NotFound` for `FilesError`
|
||||||
impl ResponseError for StaticFilesError {
|
impl ResponseError for FilesError {
|
||||||
fn error_response(&self) -> HttpResponse {
|
fn error_response(&self) -> HttpResponse {
|
||||||
HttpResponse::new(StatusCode::NOT_FOUND)
|
HttpResponse::new(StatusCode::NOT_FOUND)
|
||||||
}
|
}
|
@ -29,7 +29,7 @@ mod error;
|
|||||||
mod named;
|
mod named;
|
||||||
mod range;
|
mod range;
|
||||||
|
|
||||||
use self::error::{StaticFilesError, UriSegmentError};
|
use self::error::{FilesError, UriSegmentError};
|
||||||
pub use crate::config::{DefaultConfig, StaticFileConfig};
|
pub use crate::config::{DefaultConfig, StaticFileConfig};
|
||||||
pub use crate::named::NamedFile;
|
pub use crate::named::NamedFile;
|
||||||
pub use crate::range::HttpRange;
|
pub use crate::range::HttpRange;
|
||||||
@ -222,10 +222,10 @@ fn directory_listing(
|
|||||||
///
|
///
|
||||||
/// fn main() {
|
/// fn main() {
|
||||||
/// let app = App::new()
|
/// let app = App::new()
|
||||||
/// .service(fs::StaticFiles::new("/static", "."));
|
/// .service(fs::Files::new("/static", "."));
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
pub struct StaticFiles<S, C = DefaultConfig> {
|
pub struct Files<S, C = DefaultConfig> {
|
||||||
path: String,
|
path: String,
|
||||||
directory: PathBuf,
|
directory: PathBuf,
|
||||||
index: Option<String>,
|
index: Option<String>,
|
||||||
@ -237,28 +237,28 @@ pub struct StaticFiles<S, C = DefaultConfig> {
|
|||||||
_cd_map: PhantomData<C>,
|
_cd_map: PhantomData<C>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S: 'static> StaticFiles<S> {
|
impl<S: 'static> Files<S> {
|
||||||
/// Create new `StaticFiles` instance for specified base directory.
|
/// Create new `Files` instance for specified base directory.
|
||||||
///
|
///
|
||||||
/// `StaticFile` uses `ThreadPool` for blocking filesystem operations.
|
/// `StaticFile` uses `ThreadPool` for blocking filesystem operations.
|
||||||
/// By default pool with 5x threads of available cpus is used.
|
/// By default pool with 5x threads of available cpus is used.
|
||||||
/// Pool size can be changed by setting ACTIX_CPU_POOL environment variable.
|
/// Pool size can be changed by setting ACTIX_CPU_POOL environment variable.
|
||||||
pub fn new<T: Into<PathBuf>>(path: &str, dir: T) -> StaticFiles<S> {
|
pub fn new<T: Into<PathBuf>>(path: &str, dir: T) -> Files<S> {
|
||||||
Self::with_config(path, dir, DefaultConfig)
|
Self::with_config(path, dir, DefaultConfig)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S: 'static, C: StaticFileConfig> StaticFiles<S, C> {
|
impl<S: 'static, C: StaticFileConfig> Files<S, C> {
|
||||||
/// Create new `StaticFiles` instance for specified base directory.
|
/// Create new `Files` instance for specified base directory.
|
||||||
///
|
///
|
||||||
/// Identical with `new` but allows to specify configiration to use.
|
/// Identical with `new` but allows to specify configiration to use.
|
||||||
pub fn with_config<T: Into<PathBuf>>(path: &str, dir: T, _: C) -> StaticFiles<S, C> {
|
pub fn with_config<T: Into<PathBuf>>(path: &str, dir: T, _: C) -> Files<S, C> {
|
||||||
let dir = dir.into().canonicalize().unwrap_or_else(|_| PathBuf::new());
|
let dir = dir.into().canonicalize().unwrap_or_else(|_| PathBuf::new());
|
||||||
if !dir.is_dir() {
|
if !dir.is_dir() {
|
||||||
log::error!("Specified path is not a directory");
|
log::error!("Specified path is not a directory");
|
||||||
}
|
}
|
||||||
|
|
||||||
StaticFiles {
|
Files {
|
||||||
path: path.to_string(),
|
path: path.to_string(),
|
||||||
directory: dir,
|
directory: dir,
|
||||||
index: None,
|
index: None,
|
||||||
@ -294,13 +294,13 @@ impl<S: 'static, C: StaticFileConfig> StaticFiles<S, C> {
|
|||||||
///
|
///
|
||||||
/// Shows specific index file for directory "/" instead of
|
/// Shows specific index file for directory "/" instead of
|
||||||
/// showing files listing.
|
/// showing files listing.
|
||||||
pub fn index_file<T: Into<String>>(mut self, index: T) -> StaticFiles<S, C> {
|
pub fn index_file<T: Into<String>>(mut self, index: T) -> Files<S, C> {
|
||||||
self.index = Some(index.into());
|
self.index = Some(index.into());
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<P, C> HttpServiceFactory<P> for StaticFiles<P, C>
|
impl<P, C> HttpServiceFactory<P> for Files<P, C>
|
||||||
where
|
where
|
||||||
P: 'static,
|
P: 'static,
|
||||||
C: StaticFileConfig + 'static,
|
C: StaticFileConfig + 'static,
|
||||||
@ -319,16 +319,16 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<P, C: StaticFileConfig + 'static> NewService<ServiceRequest<P>>
|
impl<P, C: StaticFileConfig + 'static> NewService<ServiceRequest<P>>
|
||||||
for StaticFiles<P, C>
|
for Files<P, C>
|
||||||
{
|
{
|
||||||
type Response = ServiceResponse;
|
type Response = ServiceResponse;
|
||||||
type Error = ();
|
type Error = ();
|
||||||
type Service = StaticFilesService<P, C>;
|
type Service = FilesService<P, C>;
|
||||||
type InitError = ();
|
type InitError = ();
|
||||||
type Future = FutureResult<Self::Service, Self::InitError>;
|
type Future = FutureResult<Self::Service, Self::InitError>;
|
||||||
|
|
||||||
fn new_service(&self, _: &()) -> Self::Future {
|
fn new_service(&self, _: &()) -> Self::Future {
|
||||||
ok(StaticFilesService {
|
ok(FilesService {
|
||||||
directory: self.directory.clone(),
|
directory: self.directory.clone(),
|
||||||
index: self.index.clone(),
|
index: self.index.clone(),
|
||||||
show_index: self.show_index,
|
show_index: self.show_index,
|
||||||
@ -341,7 +341,7 @@ impl<P, C: StaticFileConfig + 'static> NewService<ServiceRequest<P>>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct StaticFilesService<S, C = DefaultConfig> {
|
pub struct FilesService<S, C = DefaultConfig> {
|
||||||
directory: PathBuf,
|
directory: PathBuf,
|
||||||
index: Option<String>,
|
index: Option<String>,
|
||||||
show_index: bool,
|
show_index: bool,
|
||||||
@ -352,7 +352,7 @@ pub struct StaticFilesService<S, C = DefaultConfig> {
|
|||||||
_cd_map: PhantomData<C>,
|
_cd_map: PhantomData<C>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<P, C: StaticFileConfig> Service<ServiceRequest<P>> for StaticFilesService<P, C> {
|
impl<P, C: StaticFileConfig> Service<ServiceRequest<P>> for FilesService<P, C> {
|
||||||
type Response = ServiceResponse;
|
type Response = ServiceResponse;
|
||||||
type Error = ();
|
type Error = ();
|
||||||
type Future = FutureResult<Self::Response, Self::Error>;
|
type Future = FutureResult<Self::Response, Self::Error>;
|
||||||
@ -395,7 +395,7 @@ impl<P, C: StaticFileConfig> Service<ServiceRequest<P>> for StaticFilesService<P
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ok(ServiceResponse::from_err(
|
ok(ServiceResponse::from_err(
|
||||||
StaticFilesError::IsDirectory,
|
FilesError::IsDirectory,
|
||||||
req.clone(),
|
req.clone(),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
@ -706,7 +706,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_named_file_ranges_status_code() {
|
fn test_named_file_ranges_status_code() {
|
||||||
let mut srv = test::init_service(
|
let mut srv = test::init_service(
|
||||||
App::new().service(StaticFiles::new("/test", ".").index_file("Cargo.toml")),
|
App::new().service(Files::new("/test", ".").index_file("Cargo.toml")),
|
||||||
);
|
);
|
||||||
|
|
||||||
// Valid range header
|
// Valid range header
|
||||||
@ -731,7 +731,7 @@ mod tests {
|
|||||||
fn test_named_file_content_range_headers() {
|
fn test_named_file_content_range_headers() {
|
||||||
let mut srv = test::init_service(
|
let mut srv = test::init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.service(StaticFiles::new("/test", ".").index_file("tests/test.binary")),
|
.service(Files::new("/test", ".").index_file("tests/test.binary")),
|
||||||
);
|
);
|
||||||
|
|
||||||
// Valid range header
|
// Valid range header
|
||||||
@ -771,7 +771,7 @@ mod tests {
|
|||||||
fn test_named_file_content_length_headers() {
|
fn test_named_file_content_length_headers() {
|
||||||
let mut srv = test::init_service(
|
let mut srv = test::init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.service(StaticFiles::new("test", ".").index_file("tests/test.binary")),
|
.service(Files::new("test", ".").index_file("tests/test.binary")),
|
||||||
);
|
);
|
||||||
|
|
||||||
// Valid range header
|
// Valid range header
|
||||||
@ -844,7 +844,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_static_files_with_spaces() {
|
fn test_static_files_with_spaces() {
|
||||||
let mut srv = test::init_service(
|
let mut srv = test::init_service(
|
||||||
App::new().service(StaticFiles::new("/", ".").index_file("Cargo.toml")),
|
App::new().service(Files::new("/", ".").index_file("Cargo.toml")),
|
||||||
);
|
);
|
||||||
let request = TestRequest::get()
|
let request = TestRequest::get()
|
||||||
.uri("/tests/test%20space.binary")
|
.uri("/tests/test%20space.binary")
|
||||||
@ -925,21 +925,21 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_static_files() {
|
fn test_static_files() {
|
||||||
let mut srv = test::init_service(
|
let mut srv = test::init_service(
|
||||||
App::new().service(StaticFiles::new("/", ".").show_files_listing()),
|
App::new().service(Files::new("/", ".").show_files_listing()),
|
||||||
);
|
);
|
||||||
let req = TestRequest::with_uri("/missing").to_request();
|
let req = TestRequest::with_uri("/missing").to_request();
|
||||||
|
|
||||||
let resp = test::call_success(&mut srv, req);
|
let resp = test::call_success(&mut srv, req);
|
||||||
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
||||||
|
|
||||||
let mut srv = test::init_service(App::new().service(StaticFiles::new("/", ".")));
|
let mut srv = test::init_service(App::new().service(Files::new("/", ".")));
|
||||||
|
|
||||||
let req = TestRequest::default().to_request();
|
let req = TestRequest::default().to_request();
|
||||||
let resp = test::call_success(&mut srv, req);
|
let resp = test::call_success(&mut srv, req);
|
||||||
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
||||||
|
|
||||||
let mut srv = test::init_service(
|
let mut srv = test::init_service(
|
||||||
App::new().service(StaticFiles::new("/", ".").show_files_listing()),
|
App::new().service(Files::new("/", ".").show_files_listing()),
|
||||||
);
|
);
|
||||||
let req = TestRequest::with_uri("/tests").to_request();
|
let req = TestRequest::with_uri("/tests").to_request();
|
||||||
let mut resp = test::call_success(&mut srv, req);
|
let mut resp = test::call_success(&mut srv, req);
|
||||||
@ -959,13 +959,13 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_static_files_bad_directory() {
|
fn test_static_files_bad_directory() {
|
||||||
let _st: StaticFiles<()> = StaticFiles::new("/", "missing");
|
let _st: Files<()> = Files::new("/", "missing");
|
||||||
let _st: StaticFiles<()> = StaticFiles::new("/", "Cargo.toml");
|
let _st: Files<()> = Files::new("/", "Cargo.toml");
|
||||||
}
|
}
|
||||||
|
|
||||||
// #[test]
|
// #[test]
|
||||||
// fn test_default_handler_file_missing() {
|
// fn test_default_handler_file_missing() {
|
||||||
// let st = StaticFiles::new(".")
|
// let st = Files::new(".")
|
||||||
// .default_handler(|_: &_| "default content");
|
// .default_handler(|_: &_| "default content");
|
||||||
// let req = TestRequest::with_uri("/missing")
|
// let req = TestRequest::with_uri("/missing")
|
||||||
// .param("tail", "missing")
|
// .param("tail", "missing")
|
||||||
@ -982,7 +982,7 @@ mod tests {
|
|||||||
|
|
||||||
// #[test]
|
// #[test]
|
||||||
// fn test_serve_index() {
|
// fn test_serve_index() {
|
||||||
// let st = StaticFiles::new(".").index_file("test.binary");
|
// let st = Files::new(".").index_file("test.binary");
|
||||||
// let req = TestRequest::default().uri("/tests").finish();
|
// let req = TestRequest::default().uri("/tests").finish();
|
||||||
|
|
||||||
// let resp = st.handle(&req).respond_to(&req).unwrap();
|
// let resp = st.handle(&req).respond_to(&req).unwrap();
|
||||||
@ -1028,7 +1028,7 @@ mod tests {
|
|||||||
|
|
||||||
// #[test]
|
// #[test]
|
||||||
// fn test_serve_index_nested() {
|
// fn test_serve_index_nested() {
|
||||||
// let st = StaticFiles::new(".").index_file("mod.rs");
|
// let st = Files::new(".").index_file("mod.rs");
|
||||||
// let req = TestRequest::default().uri("/src/client").finish();
|
// let req = TestRequest::default().uri("/src/client").finish();
|
||||||
// let resp = st.handle(&req).respond_to(&req).unwrap();
|
// let resp = st.handle(&req).respond_to(&req).unwrap();
|
||||||
// let resp = resp.as_msg();
|
// let resp = resp.as_msg();
|
||||||
@ -1048,7 +1048,7 @@ mod tests {
|
|||||||
// let mut srv = test::TestServer::with_factory(|| {
|
// let mut srv = test::TestServer::with_factory(|| {
|
||||||
// App::new().handler(
|
// App::new().handler(
|
||||||
// "test",
|
// "test",
|
||||||
// StaticFiles::new(".").index_file("Cargo.toml"),
|
// Files::new(".").index_file("Cargo.toml"),
|
||||||
// )
|
// )
|
||||||
// });
|
// });
|
||||||
|
|
||||||
@ -1081,7 +1081,7 @@ mod tests {
|
|||||||
// let mut srv = test::TestServer::with_factory(|| {
|
// let mut srv = test::TestServer::with_factory(|| {
|
||||||
// App::new().handler(
|
// App::new().handler(
|
||||||
// "test",
|
// "test",
|
||||||
// StaticFiles::new(".").index_file("Cargo.toml"),
|
// Files::new(".").index_file("Cargo.toml"),
|
||||||
// )
|
// )
|
||||||
// });
|
// });
|
||||||
|
|
Before Width: | Height: | Size: 168 B After Width: | Height: | Size: 168 B |
Loading…
Reference in New Issue
Block a user