mirror of
https://github.com/fafhrd91/actix-web
synced 2025-06-30 08:24:28 +02:00
add support of filtering guards in Files of actix-files (#2046)
Co-authored-by: Rob Ede <robjtede@icloud.com>
This commit is contained in:
@ -37,7 +37,8 @@ pub struct Files {
|
||||
renderer: Rc<DirectoryRenderer>,
|
||||
mime_override: Option<Rc<MimeOverride>>,
|
||||
file_flags: named::Flags,
|
||||
guards: Option<Rc<dyn Guard>>,
|
||||
use_guards: Option<Rc<dyn Guard>>,
|
||||
guards: Vec<Box<Rc<dyn Guard>>>,
|
||||
hidden_files: bool,
|
||||
}
|
||||
|
||||
@ -59,12 +60,12 @@ impl Clone for Files {
|
||||
file_flags: self.file_flags,
|
||||
path: self.path.clone(),
|
||||
mime_override: self.mime_override.clone(),
|
||||
use_guards: self.use_guards.clone(),
|
||||
guards: self.guards.clone(),
|
||||
hidden_files: self.hidden_files,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Files {
|
||||
/// Create new `Files` instance for a specified base directory.
|
||||
///
|
||||
@ -104,7 +105,8 @@ impl Files {
|
||||
renderer: Rc::new(directory_listing),
|
||||
mime_override: None,
|
||||
file_flags: named::Flags::default(),
|
||||
guards: None,
|
||||
use_guards: None,
|
||||
guards: Vec::new(),
|
||||
hidden_files: false,
|
||||
}
|
||||
}
|
||||
@ -185,7 +187,28 @@ impl Files {
|
||||
/// Default behaviour allows GET and HEAD.
|
||||
#[inline]
|
||||
pub fn use_guards<G: Guard + 'static>(mut self, guards: G) -> Self {
|
||||
self.guards = Some(Rc::new(guards));
|
||||
self.use_guards = Some(Rc::new(guards));
|
||||
self
|
||||
}
|
||||
|
||||
/// Add match guard to use on directory listings and files.
|
||||
///
|
||||
/// ```rust
|
||||
/// use actix_web::{guard, App};
|
||||
/// use actix_files::Files;
|
||||
///
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = App::new()
|
||||
/// .service(
|
||||
/// Files::new("/","/my/site/files")
|
||||
/// .guard(guard::Header("Host", "example.com"))
|
||||
/// );
|
||||
/// }
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn guard<G: Guard + 'static>(mut self, guard: G) -> Self {
|
||||
self.guards.push(Box::new(Rc::new(guard)));
|
||||
self
|
||||
}
|
||||
|
||||
@ -238,7 +261,19 @@ impl Files {
|
||||
}
|
||||
|
||||
impl HttpServiceFactory for Files {
|
||||
fn register(self, config: &mut AppService) {
|
||||
fn register(mut self, config: &mut AppService) {
|
||||
let guards = if self.guards.is_empty() {
|
||||
None
|
||||
} else {
|
||||
let guards = std::mem::take(&mut self.guards);
|
||||
Some(
|
||||
guards
|
||||
.into_iter()
|
||||
.map(|guard| -> Box<dyn Guard> { guard })
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
};
|
||||
|
||||
if self.default.borrow().is_none() {
|
||||
*self.default.borrow_mut() = Some(config.default_service());
|
||||
}
|
||||
@ -249,7 +284,7 @@ impl HttpServiceFactory for Files {
|
||||
ResourceDef::prefix(&self.path)
|
||||
};
|
||||
|
||||
config.register_service(rdef, None, self, None)
|
||||
config.register_service(rdef, guards, self, None)
|
||||
}
|
||||
}
|
||||
|
||||
@ -271,7 +306,7 @@ impl ServiceFactory<ServiceRequest> for Files {
|
||||
renderer: self.renderer.clone(),
|
||||
mime_override: self.mime_override.clone(),
|
||||
file_flags: self.file_flags,
|
||||
guards: self.guards.clone(),
|
||||
guards: self.use_guards.clone(),
|
||||
hidden_files: self.hidden_files,
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user