Moved file paths into a common struct

This commit is contained in:
p11 2023-12-07 20:03:04 +01:00
parent bba2c0a4f0
commit c54265a6bd

View File

@ -199,6 +199,12 @@ fn handle_connection(context: Arc<Mutex<Context>>, path: PathBuf, mut stream: Tc
(Some(pki_path), 1, path.to_string(), num) (Some(pki_path), 1, path.to_string(), num)
}; };
let file_paths = DocumentPaths {
pk: &pk_path,
pki: pki_path.as_ref().map(|path| path.as_ref()),
data: &data_path,
};
if let Some((_, ending)) = relative_path.rsplit_once('.') { if let Some((_, ending)) = relative_path.rsplit_once('.') {
let path = path.to_path_buf(); let path = path.to_path_buf();
let ending = ending.to_lowercase(); let ending = ending.to_lowercase();
@ -248,9 +254,7 @@ fn handle_connection(context: Arc<Mutex<Context>>, path: PathBuf, mut stream: Tc
&request.body, &request.body,
&relative_path, &relative_path,
&path, &path,
&pk_path, file_paths,
pki_path.as_ref().map(|path| path.as_ref()),
&data_path,
partial, partial,
start_level, start_level,
) )
@ -277,15 +281,20 @@ fn reply_image(mut stream: TcpStream, relative_path: &str, image_type: &str, mut
} }
} }
#[derive(Copy, Clone)]
struct DocumentPaths<'a> {
pk: &'a Path,
pki: Option<&'a Path>,
data: &'a Path,
}
fn handle_relative_connection( fn handle_relative_connection(
info: Arc<Mutex<SiteInfo>>, info: Arc<Mutex<SiteInfo>>,
mut stream: TcpStream, mut stream: TcpStream,
body: &str, body: &str,
relative_path: &str, relative_path: &str,
path: &Path, path: &Path,
pk_path: &Path, file_paths: DocumentPaths,
pki_path: Option<&Path>,
data_path: &Path,
partial: Option<usize>, partial: Option<usize>,
start_level: usize, start_level: usize,
) { ) {
@ -352,7 +361,7 @@ fn handle_relative_connection(
return; return;
}; };
if let Ok(mut file) = File::create(data_path) { if let Ok(mut file) = File::create(file_paths.data) {
if to_stream::<PortableSettings, _, _>(&info, &mut file).is_err() { if to_stream::<PortableSettings, _, _>(&info, &mut file).is_err() {
eprintln!("Error saving data!"); eprintln!("Error saving data!");
eprintln!(); eprintln!();
@ -414,7 +423,7 @@ fn handle_relative_connection(
); );
}; };
if let Ok(pk_file) = File::open(pk_path) { if let Ok(pk_file) = File::open(file_paths.pk) {
let lines = BufReader::new(pk_file).lines(); let lines = BufReader::new(pk_file).lines();
if let Some(i) = partial { if let Some(i) = partial {
@ -506,7 +515,7 @@ fn handle_relative_connection(
section(&mut stream); section(&mut stream);
if let Some(pki_path) = pki_path { if let Some(pki_path) = file_paths.pki {
if let Ok(pki_file) = File::open(pki_path) { if let Ok(pki_file) = File::open(pki_path) {
let _ = writeln!(stream, "<h1>Description</h1>"); let _ = writeln!(stream, "<h1>Description</h1>");
let lines = BufReader::new(pki_file).lines(); let lines = BufReader::new(pki_file).lines();