@@ -82,10 +82,22 @@ impl FileServer {
82
82
/// `path` with a default rank.
83
83
///
84
84
/// Adds a set of default rewrites:
85
- /// - [`filter_dotfiles`]: Hides all dotfiles.
86
- /// - [`prefix(path)`](prefix): Applies the root path.
87
- /// - [`normalize_dirs`]: Normalizes directories to have a trailing slash.
88
- /// - [`index("index.html")`](index): Appends `index.html` to directory requests.
85
+ /// - `|f, _| f.is_visible()`: Hides all dotfiles.
86
+ /// - [`Prefix::checked(path)`]: Applies the root path.
87
+ /// - [`TrailingDirs`]: Normalizes directories to have a trailing slash.
88
+ /// - [`DirIndex::unconditional("index.html")`](DirIndex::unconditional):
89
+ /// Appends `index.html` to directory requests.
90
+ ///
91
+ /// If you don't want to serve requests for directories, or want to
92
+ /// customize what files are served when a directory is requested, see
93
+ /// [`Self::new_without_index`].
94
+ ///
95
+ /// If you need to allow requests for dotfiles, or make any other changes
96
+ /// to the default rewrites, see [`Self::empty`].
97
+ ///
98
+ /// [`Prefix::checked(path)`]: crate::fs::rewrite::Prefix::checked
99
+ /// [`TrailingDirs`]: crate::fs::rewrite::TrailingDirs
100
+ /// [`DirIndex::unconditional`]: crate::fs::DirIndex::unconditional
89
101
pub fn new < P : AsRef < Path > > ( path : P ) -> Self {
90
102
Self :: empty ( )
91
103
. filter ( |f, _| f. is_visible ( ) )
@@ -95,22 +107,48 @@ impl FileServer {
95
107
}
96
108
97
109
/// Constructs a new `FileServer` that serves files from the file system
98
- /// `path` with a default rank.
110
+ /// `path` with a default rank. This variant does not add a default
111
+ /// directory index option.
99
112
///
100
113
/// Adds a set of default rewrites:
101
- /// - [`filter_dotfiles`]: Hides all dotfiles.
102
- /// - [`prefix(path)`](prefix): Applies the root path.
103
- /// - [`normalize_dirs`]: Normalizes directories to have a trailing slash.
104
- pub fn directory < P : AsRef < Path > > ( path : P ) -> Self {
114
+ /// - `|f, _| f.is_visible()`: Hides all dotfiles.
115
+ /// - [`Prefix::checked(path)`]: Applies the root path.
116
+ /// - [`TrailingDirs`]: Normalizes directories to have a trailing slash.
117
+ ///
118
+ /// In most cases, [`Self::new`] is good enough. However, if you do not want
119
+ /// to automatically respond to requests for directories with `index.html`,
120
+ /// this method is provided.
121
+ ///
122
+ /// # Example
123
+ ///
124
+ /// Constructs a default file server to server files from `./static`, but
125
+ /// uses `index.txt` if `index.html` doesn't exist.
126
+ ///
127
+ /// ```rust,no_run
128
+ /// # #[macro_use] extern crate rocket;
129
+ /// use rocket::fs::{FileServer, rewrite::DirIndex};
130
+ ///
131
+ /// #[launch]
132
+ /// fn rocket() -> _ {
133
+ /// let server = FileServer::new("static")
134
+ /// .rewrite(DirIndex::if_exists("index.html"))
135
+ /// .rewrite(DirIndex::unconditional("index.txt"));
136
+ ///
137
+ /// rocket::build()
138
+ /// .mount("/", server)
139
+ /// }
140
+ /// ```
141
+ ///
142
+ /// [`Prefix::checked(path)`]: crate::fs::rewrite::Prefix::checked
143
+ /// [`TrailingDirs`]: crate::fs::rewrite::TrailingDirs
144
+ pub fn new_without_index < P : AsRef < Path > > ( path : P ) -> Self {
105
145
Self :: empty ( )
106
146
. filter ( |f, _| f. is_visible ( ) )
107
147
. rewrite ( Prefix :: checked ( path) )
108
148
. rewrite ( TrailingDirs )
109
149
}
110
150
111
151
/// Constructs a new `FileServer`, with default rank, and no rewrites.
112
- ///
113
- /// See [`FileServer::empty_ranked()`].
114
152
pub fn empty ( ) -> Self {
115
153
Self {
116
154
rewrites : vec ! [ ] ,
@@ -127,7 +165,7 @@ impl FileServer {
127
165
/// # fn make_server() -> FileServer {
128
166
/// FileServer::empty()
129
167
/// .rank(5)
130
- /// }
168
+ /// # }
131
169
pub fn rank ( mut self , rank : isize ) -> Self {
132
170
self . rank = rank;
133
171
self
@@ -140,7 +178,7 @@ impl FileServer {
140
178
/// Redirects all requests that have been filtered to the root of the `FileServer`.
141
179
///
142
180
/// ```rust,no_run
143
- /// # use rocket::fs::{FileServer, Rewrite};
181
+ /// # use rocket::fs::{FileServer, rewrite:: Rewrite};
144
182
/// # use rocket::{response::Redirect, uri, Build, Rocket, Request};
145
183
/// fn redir_missing<'r>(p: Option<Rewrite<'r>>, _req: &Request<'_>) -> Option<Rewrite<'r>> {
146
184
/// match p {
@@ -169,7 +207,7 @@ impl FileServer {
169
207
/// Filter out all paths with a filename of `hidden`.
170
208
///
171
209
/// ```rust,no_run
172
- /// #[macro_use] extern crate rocket;
210
+ /// # # [macro_use] extern crate rocket;
173
211
/// use rocket::fs::FileServer;
174
212
///
175
213
/// #[launch]
@@ -179,7 +217,7 @@ impl FileServer {
179
217
///
180
218
/// rocket::build()
181
219
/// .mount("/", server)
182
- /// # }
220
+ /// }
183
221
/// ```
184
222
pub fn filter < F : Send + Sync + ' static > ( self , f : F ) -> Self
185
223
where F : Fn ( & File < ' _ > , & Request < ' _ > ) -> bool
@@ -200,6 +238,25 @@ impl FileServer {
200
238
self . rewrite ( Filter ( f) )
201
239
}
202
240
241
+ /// Change what files this `FileServer` will respond with
242
+ ///
243
+ /// # Example
244
+ ///
245
+ /// Append `index.txt` to every path.
246
+ ///
247
+ /// ```rust,no_run
248
+ /// # #[macro_use] extern crate rocket;
249
+ /// use rocket::fs::FileServer;
250
+ ///
251
+ /// #[launch]
252
+ /// fn rocket() -> _ {
253
+ /// let server = FileServer::new("static")
254
+ /// .map(|f, _| f.map_path(|p| p.join("index.txt")).into());
255
+ ///
256
+ /// rocket::build()
257
+ /// .mount("/", server)
258
+ /// }
259
+ /// ```
203
260
pub fn map < F : Send + Sync + ' static > ( self , f : F ) -> Self
204
261
where F : for < ' r > Fn ( File < ' r > , & Request < ' _ > ) -> Rewrite < ' r >
205
262
{
0 commit comments