implement files list

This commit is contained in:
yggverse 2025-09-08 16:29:52 +03:00
parent 1dd9631c8a
commit 09dba402cd
2 changed files with 64 additions and 24 deletions

View file

@ -16,6 +16,7 @@ use std::{
fs::File,
io::{Read, Write},
net::{SocketAddr, TcpListener, TcpStream},
path::PathBuf,
str::FromStr,
sync::Arc,
thread,
@ -301,7 +302,7 @@ fn index(config: &Config, public: &Public, page: Option<usize>) -> Result<String
b.push(format!(
"{} • {} • {}\n",
torrent.time.format(&config.date),
format::size(&i),
format::total(&i),
format::files(&i),
))
}
@ -335,6 +336,19 @@ fn index(config: &Config, public: &Public, page: Option<usize>) -> Result<String
}
fn info(config: &Config, torrent: Torrent) -> Result<String> {
struct File {
path: Option<PathBuf>,
length: u64,
}
impl File {
pub fn path(&self) -> String {
self.path
.as_ref()
.map(|p| p.to_string_lossy().into())
.unwrap_or("?".into())
}
}
let i: TorrentMetaV1Owned = torrent_from_bytes(&torrent.bytes)?;
let mut b = Vec::new();
@ -351,7 +365,7 @@ fn info(config: &Config, torrent: Torrent) -> Result<String> {
b.push(format!(
"{} • {} • {}\n",
torrent.time.format(&config.date),
format::size(&i),
format::total(&i),
format::files(&i),
));
@ -360,5 +374,29 @@ fn info(config: &Config, torrent: Torrent) -> Result<String> {
format::magnet(&i, config.tracker.as_ref())
));
if let Some(files) = i.info.files.map(|files| {
let mut b = Vec::with_capacity(files.len());
for f in files {
let mut p = std::path::PathBuf::new();
b.push(File {
length: f.length,
path: match f.full_path(&mut p) {
Ok(()) => Some(p),
Err(e) => {
warn!("Filename decode error: {e}");
None
}
},
})
}
b.sort_by(|a, b| a.path.cmp(&b.path)); // @TODO optional
b
}) {
b.push("## Files".into());
for file in files {
b.push(format!("* {} ({})", file.path(), format::size(file.length)));
}
}
Ok(b.join("\n"))
}