Automatically include audio if available

This commit is contained in:
p11 2024-06-24 16:49:36 +02:00
parent 15bb171066
commit e74cb61176

View File

@ -279,7 +279,7 @@ fn handle_connection(
let mut pk_path = path.to_path_buf();
let mut data_path = path.to_path_buf();
let (pki_path, start_level, relative_path, partial) = if relative_path.is_empty() {
let (pki_path, audio_path, start_level, relative_path, partial) = if relative_path.is_empty() {
if access == Access::Full {
pk_path.push("index.pk");
data_path.push("index.dat");
@ -288,9 +288,10 @@ fn handle_connection(
data_path.push("partial.dat");
}
(None, 0, String::new(), None)
(None, None, 0, String::new(), None)
} else {
let mut pki_path = path.to_path_buf();
let mut audio_path = path.to_path_buf();
let path = percent_decode_str(relative_path).decode_utf8_lossy();
if path.contains('_') {
@ -312,14 +313,16 @@ fn handle_connection(
} else {
pki_path.push(format!("{path}.pkc"));
}
audio_path.push(format!("{path}.mp3"));
data_path.push(format!("{path}.dat"));
(Some(pki_path), 1, path.to_string(), num)
(Some(pki_path), Some(audio_path), 1, path.to_string(), num)
};
let file_paths = DocumentPaths {
pk: &pk_path,
pki: pki_path.as_ref().map(|path| path.as_ref()),
audio: audio_path.as_ref().map(|path| path.as_ref()),
data: &data_path,
};
@ -414,6 +417,7 @@ fn reply_binary(
struct DocumentPaths<'a> {
pk: &'a Path,
pki: Option<&'a Path>,
audio: Option<&'a Path>,
data: &'a Path,
}
@ -532,24 +536,36 @@ fn handle_relative_connection(
let handle_entry = |mut entry: &str, output: &mut TcpStream, level: usize| {
let level = level + 1;
let mut path = path.to_path_buf();
let mut pki_path = path.to_path_buf();
let mut audio_path = path.to_path_buf();
if let Some((real_entry, _)) = entry.split_once(':') {
entry = real_entry
}
let pki_extension = if censored { "pkc" } else { "pki" };
path.push(if relative_path.is_empty() {
format!("{entry}.{pki_extension}")
if relative_path.is_empty() {
pki_path.push(format!("{entry}.{pki_extension}"));
audio_path.push(format!("{entry}.mp3"));
} else {
format!("{relative_path}/{entry}.{pki_extension}")
});
pki_path.push(format!("{relative_path}/{entry}.{pki_extension}"));
audio_path.push(format!("{relative_path}/{entry}.mp3"));
};
let Ok(file) = File::open(path) else {
let Ok(file) = File::open(pki_path) else {
return;
};
let _ = writeln!(
output,
"<h{level}><a href=\"{relative_path}/{entry}\">{entry}</a></h{level}>"
);
if Path::is_file(&audio_path) {
let _ = writeln!(
output,
"<p><audio controls src=\"/{relative_path}/{entry}.mp3\"/></p>"
);
}
convert_subheader(
BufReader::new(file)
.lines()
@ -654,8 +670,17 @@ fn handle_relative_connection(
if let Some(pki_path) = file_paths.pki {
if let Ok(pki_file) = File::open(pki_path) {
let _ = writeln!(stream, "<h1>Description</h1>");
let lines = BufReader::new(pki_file).lines();
if let Some(audio_path) = &file_paths.audio {
if Path::is_file(audio_path) {
let _ = writeln!(
stream,
"<p><audio controls src=\"/{relative_path}.mp3\"/></p>"
);
}
}
let lines = BufReader::new(pki_file).lines();
convert_subheader(lines.map(|line| line.unwrap_or_default()), &mut stream, 1);
section(&mut stream);