Skip to content

Commit f1da1c5

Browse files
committed
fix: root_path to be absolute from parameter or cwd
Currently the root_path is taken as is from the CLI arguments. If the user provides "./" then, FileExplorer's entry paths will be created from this path instead of the absolute path that "./" really represents in user's system.
1 parent e3df3b4 commit f1da1c5

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

src/config/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ impl TryFrom<Cli> for Config {
6262

6363
fn try_from(cli_aguments: Cli) -> Result<Self, Self::Error> {
6464
let verbose = cli_aguments.verbose;
65+
let root_dir = if cli_aguments.root_dir.to_str().unwrap() == "./" {
66+
current_dir().unwrap()
67+
} else {
68+
cli_aguments.root_dir.canonicalize().unwrap()
69+
};
6570

6671
let tls: Option<tls::TlsConfig> = if cli_aguments.tls {
6772
Some(tls::TlsConfig::new(
@@ -77,7 +82,7 @@ impl TryFrom<Cli> for Config {
7782
host: cli_aguments.host,
7883
port: cli_aguments.port,
7984
address: SocketAddr::new(cli_aguments.host, cli_aguments.port),
80-
root_dir: cli_aguments.root_dir,
85+
root_dir,
8186
verbose,
8287
tls,
8388
})

src/server/service/file_explorer.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use hyper_staticfile::{resolve, ResolveResult, ResponseBuilder as FileResponseBu
99
use serde::Serialize;
1010
use std::cmp::{Ord, Ordering};
1111
use std::fs::read_dir;
12-
use std::path::PathBuf;
12+
use std::path::{Path, PathBuf};
1313
use std::str::FromStr;
1414
use std::sync::Arc;
1515
use std::time::SystemTime;
@@ -234,7 +234,7 @@ impl<'a> FileExplorer<'a> {
234234
.to_string(),
235235
is_dir: metadata.is_dir(),
236236
size: FileExplorer::format_bytes(metadata.len() as f64),
237-
entry_path: FileExplorer::make_dir_entry_link(&root_dir, entry.path()),
237+
entry_path: FileExplorer::make_dir_entry_link(&root_dir, &entry.path()),
238238
created_at,
239239
updated_at,
240240
});
@@ -281,12 +281,12 @@ impl<'a> FileExplorer<'a> {
281281
///
282282
/// This happens because links should behave relative to the `/` path
283283
/// 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 {
284+
fn make_dir_entry_link(root_dir: &Path, entry_path: &Path) -> String {
285285
// format!("/{}", &entry_path[current_dir_path.len()..])
286286
let root_dir = root_dir.to_str().unwrap();
287287
let entry_path = entry_path.to_str().unwrap();
288-
289-
entry_path[root_dir.len() - 1..].to_string()
288+
println!("{}\n{}", root_dir, entry_path);
289+
entry_path[root_dir.len()..].to_string()
290290
}
291291

292292
/// Calculates the format of the `Bytes` by converting `bytes` to the
@@ -349,7 +349,7 @@ mod tests {
349349

350350
assert_eq!(
351351
"/src/server/service/file_explorer.rs",
352-
FileExplorer::make_dir_entry_link(&root_dir, entry_path)
352+
FileExplorer::make_dir_entry_link(&root_dir, &entry_path)
353353
);
354354
}
355355
}

0 commit comments

Comments
 (0)