@@ -209,7 +209,6 @@ impl<'a> FileExplorer<'a> {
209209 /// Creates a `DirectoryIndex` with the provided `root_dir` and `path`
210210 /// (HTTP Request URI)
211211 fn index_directory ( root_dir : PathBuf , path : PathBuf ) -> Result < DirectoryIndex > {
212- let root_dir: & str = root_dir. to_str ( ) . unwrap ( ) ;
213212 let entries = read_dir ( path) . context ( "Unable to read directory" ) ?;
214213 let mut directory_entries: Vec < DirectoryEntry > = Vec :: new ( ) ;
215214
@@ -235,11 +234,7 @@ impl<'a> FileExplorer<'a> {
235234 . to_string ( ) ,
236235 is_dir : metadata. is_dir ( ) ,
237236 size : FileExplorer :: format_bytes ( metadata. len ( ) as f64 ) ,
238- entry_path : FileExplorer :: make_entry_relative_path (
239- root_dir,
240- entry. path ( ) . to_str ( ) . unwrap ( ) ,
241- )
242- . to_string ( ) ,
237+ entry_path : FileExplorer :: make_dir_entry_link ( & root_dir, entry. path ( ) ) ,
243238 created_at,
244239 updated_at,
245240 } ) ;
@@ -277,8 +272,21 @@ impl<'a> FileExplorer<'a> {
277272
278273 /// Creates entry's relative path. Used by Handlebars template engine to
279274 /// provide navigation through `FileExplorer`
280- fn make_entry_relative_path < ' b > ( current_dir_path : & ' b str , entry_path : & ' b str ) -> & ' b str {
281- & entry_path[ current_dir_path. len ( ) ..]
275+ ///
276+ /// If the root_dir is: `https-server/src`
277+ /// The entry path is: `https-server/src/server/service/file_explorer.rs`
278+ ///
279+ /// Then the resulting path from this function is the absolute path to
280+ /// the "entry path" in relation to the "root_dir" path.
281+ ///
282+ /// This happens because links should behave relative to the `/` path
283+ /// which in this case is `http-server/src` instead of system's root path.
284+ fn make_dir_entry_link ( root_dir : & PathBuf , entry_path : PathBuf ) -> String {
285+ // format!("/{}", &entry_path[current_dir_path.len()..])
286+ let root_dir = root_dir. to_str ( ) . unwrap ( ) ;
287+ let entry_path = entry_path. to_str ( ) . unwrap ( ) ;
288+
289+ entry_path[ root_dir. len ( ) - 1 ..] . to_string ( )
282290 }
283291
284292 /// Calculates the format of the `Bytes` by converting `bytes` to the
@@ -317,7 +325,7 @@ mod tests {
317325 use super :: * ;
318326
319327 #[ test]
320- fn format_bytes ( ) {
328+ fn formats_bytes ( ) {
321329 let byte_sizes = vec ! [ 1024. , 1048576. , 1073741824. , 1099511627776. ] ;
322330
323331 let expect = vec ! [
@@ -331,4 +339,17 @@ mod tests {
331339 assert_eq ! ( FileExplorer :: format_bytes( size) , expect[ idx] ) ;
332340 }
333341 }
342+
343+ #[ test]
344+ fn makes_dir_entry_link ( ) {
345+ let root_dir = PathBuf :: from_str ( "/Users/bob/sources/http-server" ) . unwrap ( ) ;
346+ let entry_path =
347+ PathBuf :: from_str ( "/Users/bob/sources/http-server/src/server/service/file_explorer.rs" )
348+ . unwrap ( ) ;
349+
350+ assert_eq ! (
351+ "/src/server/service/file_explorer.rs" ,
352+ FileExplorer :: make_dir_entry_link( & root_dir, entry_path)
353+ ) ;
354+ }
334355}
0 commit comments