From bff6b209c9e33d7e0cd98200d0349729eb02c066 Mon Sep 17 00:00:00 2001 From: yggverse Date: Sat, 9 Aug 2025 20:07:59 +0300 Subject: [PATCH] store file names as the PathBuf --- src/main.rs | 4 ++-- src/torrent.rs | 28 ++++++++++------------------ src/torrent/file.rs | 13 +++++++------ templates/info.html.tera | 2 +- 4 files changed, 20 insertions(+), 27 deletions(-) diff --git a/src/main.rs b/src/main.rs index 2e1c281..a8452fa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -102,7 +102,7 @@ fn info( #[derive(Serialize)] #[serde(crate = "rocket::serde")] struct F { - name: String, + path: String, size: String, } let torrent = Torrent::from_storage(&t.bytes, t.time).map_err(|e| { @@ -118,7 +118,7 @@ fn info( files_list: torrent.files.as_ref().map(|f| { f.iter() .map(|f| F { - name: f.name(), + path: f.path(), size: f.size(), }) .collect::>() diff --git a/src/torrent.rs b/src/torrent.rs index fdbbdbf..162ed4d 100644 --- a/src/torrent.rs +++ b/src/torrent.rs @@ -44,28 +44,20 @@ impl Torrent { .unwrap_or_default(), files: i.info.files.map(|files| { let mut b = Vec::with_capacity(files.len()); - for f in files.iter() { + for f in files { + let mut p = std::path::PathBuf::new(); b.push(File { - name: String::from_utf8( - f.path - .iter() - .enumerate() - .flat_map(|(n, b)| { - if n == 0 { - b.0.to_vec() - } else { - let mut p = vec![b'/']; - p.extend(b.0.to_vec()); - p - } - }) - .collect(), - ) - .ok(), 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.name.cmp(&b.name)); // @TODO optional + b.sort_by(|a, b| a.path.cmp(&b.path)); // @TODO optional b }), publisher_url: i.publisher_url.map(|u| u.to_string()), diff --git a/src/torrent/file.rs b/src/torrent/file.rs index b83c585..ca0f109 100644 --- a/src/torrent/file.rs +++ b/src/torrent/file.rs @@ -1,15 +1,16 @@ -use rocket::serde::Serialize; - -#[derive(Clone, Debug, Serialize)] +#[derive(Clone, Debug, rocket::serde::Serialize)] #[serde(crate = "rocket::serde")] pub struct File { - pub name: Option, + pub path: Option, pub length: u64, } impl File { - pub fn name(&self) -> String { - self.name.as_deref().unwrap_or("?").into() + pub fn path(&self) -> String { + self.path + .as_ref() + .map(|p| p.to_string_lossy().into()) + .unwrap_or("?".into()) } pub fn size(&self) -> String { super::size(self.length) diff --git a/templates/info.html.tera b/templates/info.html.tera index 476bee8..33194f9 100644 --- a/templates/info.html.tera +++ b/templates/info.html.tera @@ -31,7 +31,7 @@ {% for file in files_list %} - {{ file.name }} + {{ file.path }} {{ file.size }} {% endfor %}