mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-24 00:21:08 +01:00
files: allow show_files_listing()
with index_file()
(#2228)
This commit is contained in:
parent
73a655544e
commit
6893773280
@ -8,11 +8,13 @@
|
|||||||
* For symbolic links, `Content-Disposition` header no longer shows the filename of the original file. [#2156]
|
* For symbolic links, `Content-Disposition` header no longer shows the filename of the original file. [#2156]
|
||||||
* `Files::redirect_to_slash_directory()` now works as expected when used with `Files::show_files_listing()`. [#2225]
|
* `Files::redirect_to_slash_directory()` now works as expected when used with `Files::show_files_listing()`. [#2225]
|
||||||
* `application/{javascript, json, wasm}` mime type now have `inline` disposition by default. [#2257]
|
* `application/{javascript, json, wasm}` mime type now have `inline` disposition by default. [#2257]
|
||||||
|
* `Files::show_files_listing()` can now be used with `Files::index_file()` to show files listing as a fallback when the index file is not found. [#2228]
|
||||||
|
|
||||||
[#2135]: https://github.com/actix/actix-web/pull/2135
|
[#2135]: https://github.com/actix/actix-web/pull/2135
|
||||||
[#2156]: https://github.com/actix/actix-web/pull/2156
|
[#2156]: https://github.com/actix/actix-web/pull/2156
|
||||||
[#2225]: https://github.com/actix/actix-web/pull/2225
|
[#2225]: https://github.com/actix/actix-web/pull/2225
|
||||||
[#2257]: https://github.com/actix/actix-web/pull/2257
|
[#2257]: https://github.com/actix/actix-web/pull/2257
|
||||||
|
[#2228]: https://github.com/actix/actix-web/pull/2228
|
||||||
|
|
||||||
|
|
||||||
## 0.6.0-beta.4 - 2021-04-02
|
## 0.6.0-beta.4 - 2021-04-02
|
||||||
|
@ -114,6 +114,9 @@ impl Files {
|
|||||||
/// Show files listing for directories.
|
/// Show files listing for directories.
|
||||||
///
|
///
|
||||||
/// By default show files listing is disabled.
|
/// By default show files listing is disabled.
|
||||||
|
///
|
||||||
|
/// When used with [`Files::index_file()`], files listing is shown as a fallback
|
||||||
|
/// when the index file is not found.
|
||||||
pub fn show_files_listing(mut self) -> Self {
|
pub fn show_files_listing(mut self) -> Self {
|
||||||
self.show_index = true;
|
self.show_index = true;
|
||||||
self
|
self
|
||||||
@ -148,8 +151,11 @@ impl Files {
|
|||||||
|
|
||||||
/// Set index file
|
/// Set index file
|
||||||
///
|
///
|
||||||
/// Shows specific index file for directory "/" instead of
|
/// Shows specific index file for directories instead of
|
||||||
/// showing files listing.
|
/// showing files listing.
|
||||||
|
///
|
||||||
|
/// If the index file is not found, files listing is shown as a fallback if
|
||||||
|
/// [`Files::show_files_listing()`] is set.
|
||||||
pub fn index_file<T: Into<String>>(mut self, index: T) -> Self {
|
pub fn index_file<T: Into<String>>(mut self, index: T) -> Self {
|
||||||
self.index = Some(index.into());
|
self.index = Some(index.into());
|
||||||
self
|
self
|
||||||
|
@ -872,4 +872,33 @@ mod tests {
|
|||||||
"inline; filename=\"symlink-test.png\""
|
"inline; filename=\"symlink-test.png\""
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_index_with_show_files_listing() {
|
||||||
|
let service = Files::new(".", ".")
|
||||||
|
.index_file("lib.rs")
|
||||||
|
.show_files_listing()
|
||||||
|
.new_service(())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
// Serve the index if exists
|
||||||
|
let req = TestRequest::default().uri("/src").to_srv_request();
|
||||||
|
let resp = test::call_service(&service, req).await;
|
||||||
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
assert_eq!(
|
||||||
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
||||||
|
"text/x-rust"
|
||||||
|
);
|
||||||
|
|
||||||
|
// Show files listing, otherwise.
|
||||||
|
let req = TestRequest::default().uri("/tests").to_srv_request();
|
||||||
|
let resp = test::call_service(&service, req).await;
|
||||||
|
assert_eq!(
|
||||||
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
||||||
|
"text/html; charset=utf-8"
|
||||||
|
);
|
||||||
|
let bytes = test::read_body(resp).await;
|
||||||
|
assert!(format!("{:?}", bytes).contains("/tests/test.png"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -102,26 +102,20 @@ impl Service<ServiceRequest> for FilesService {
|
|||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(ref redir_index) = self.index {
|
let serve_named_file = |req: ServiceRequest, mut named_file: NamedFile| {
|
||||||
let path = path.join(redir_index);
|
if let Some(ref mime_override) = self.mime_override {
|
||||||
|
let new_disposition = mime_override(&named_file.content_type.type_());
|
||||||
match NamedFile::open(path) {
|
named_file.content_disposition.disposition = new_disposition;
|
||||||
Ok(mut named_file) => {
|
|
||||||
if let Some(ref mime_override) = self.mime_override {
|
|
||||||
let new_disposition =
|
|
||||||
mime_override(&named_file.content_type.type_());
|
|
||||||
named_file.content_disposition.disposition = new_disposition;
|
|
||||||
}
|
|
||||||
named_file.flags = self.file_flags;
|
|
||||||
|
|
||||||
let (req, _) = req.into_parts();
|
|
||||||
let res = named_file.into_response(&req);
|
|
||||||
Box::pin(ok(ServiceResponse::new(req, res)))
|
|
||||||
}
|
|
||||||
Err(err) => self.handle_err(err, req),
|
|
||||||
}
|
}
|
||||||
} else if self.show_index {
|
named_file.flags = self.file_flags;
|
||||||
let dir = Directory::new(self.directory.clone(), path);
|
|
||||||
|
let (req, _) = req.into_parts();
|
||||||
|
let res = named_file.into_response(&req);
|
||||||
|
Box::pin(ok(ServiceResponse::new(req, res)))
|
||||||
|
};
|
||||||
|
|
||||||
|
let show_index = |req: ServiceRequest| {
|
||||||
|
let dir = Directory::new(self.directory.clone(), path.clone());
|
||||||
|
|
||||||
let (req, _) = req.into_parts();
|
let (req, _) = req.into_parts();
|
||||||
let x = (self.renderer)(&dir, &req);
|
let x = (self.renderer)(&dir, &req);
|
||||||
@ -130,11 +124,19 @@ impl Service<ServiceRequest> for FilesService {
|
|||||||
Ok(resp) => ok(resp),
|
Ok(resp) => ok(resp),
|
||||||
Err(err) => ok(ServiceResponse::from_err(err, req)),
|
Err(err) => ok(ServiceResponse::from_err(err, req)),
|
||||||
})
|
})
|
||||||
} else {
|
};
|
||||||
Box::pin(ok(ServiceResponse::from_err(
|
|
||||||
|
match self.index {
|
||||||
|
Some(ref index) => match NamedFile::open(path.join(index)) {
|
||||||
|
Ok(named_file) => serve_named_file(req, named_file),
|
||||||
|
Err(_) if self.show_index => show_index(req),
|
||||||
|
Err(err) => self.handle_err(err, req),
|
||||||
|
},
|
||||||
|
None if self.show_index => show_index(req),
|
||||||
|
_ => Box::pin(ok(ServiceResponse::from_err(
|
||||||
FilesError::IsDirectory,
|
FilesError::IsDirectory,
|
||||||
req.into_parts().0,
|
req.into_parts().0,
|
||||||
)))
|
))),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
match NamedFile::open(path) {
|
match NamedFile::open(path) {
|
||||||
|
Loading…
Reference in New Issue
Block a user