diff --git a/src/feed.rs b/src/feed.rs index db17b29..73ebca1 100644 --- a/src/feed.rs +++ b/src/feed.rs @@ -69,8 +69,7 @@ impl Feed { )); self.buffer.push_str(""); - self.buffer - .push_str(&format!("size: {}", size(torrent.size))); + self.buffer.push_str(&format!("size: {}", torrent.size())); if let Some(f) = torrent.files() { self.buffer.push_str(&format!(" / files: {f}")); } @@ -101,21 +100,3 @@ fn escape(value: &str) -> String { .replace('"', """) .replace("'", "'") } - -fn size(value: u64) -> String { - const KB: f32 = 1024.0; - const MB: f32 = KB * KB; - const GB: f32 = MB * KB; - - let f = value as f32; - - if f < KB { - format!("{value} B") - } else if f < MB { - format!("{:.2} KB", f / KB) - } else if f < GB { - format!("{:.2} MB", f / MB) - } else { - format!("{:.2} GB", f / GB) - } -} diff --git a/src/main.rs b/src/main.rs index afb5869..57f49d9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,7 +33,7 @@ fn index( indexed: String, magnet: String, scrape: Option, - size: usize, + size: String, torrent: Torrent, } let (total, torrents) = public @@ -83,7 +83,7 @@ fn index( indexed: torrent.time.format(&meta.format_time).to_string(), magnet: torrent.magnet(meta.trackers.as_ref()), scrape: scrape::get(scrape, torrent.id.0), - size: torrent.size as usize, // required by `filesizeformat` impl + size: torrent.size(), torrent }), Err(e) => { @@ -115,7 +115,7 @@ fn info( struct F { href: Option, path: String, - size: usize, + size: String, } let torrent = Torrent::from_public(&t.bytes, t.time).map_err(|e| { error!("Torrent parse error: `{e}`"); @@ -143,7 +143,7 @@ fn info( F { href: public.href(&torrent.info_hash, &p), path: p, - size: f.length as usize, // required by `filesizeformat` impl + size: f.size(), } }) .collect::>() @@ -151,7 +151,7 @@ fn info( indexed: torrent.time.format(&meta.format_time).to_string(), magnet: torrent.magnet(meta.trackers.as_ref()), scrape: scrape::get(scrape, i.0), - size: torrent.size as usize, // required by `filesizeformat` impl + size: torrent.size(), torrent }, )) diff --git a/src/torrent.rs b/src/torrent.rs index cf7a668..b265025 100644 --- a/src/torrent.rs +++ b/src/torrent.rs @@ -80,6 +80,10 @@ impl Torrent { self.files.as_ref().map(|f| f.len()) } + pub fn size(&self) -> String { + size(self.size) + } + pub fn magnet(&self, trackers: Option<&Vec>) -> String { let mut b = format!("magnet:?xt=urn:btih:{}", self.info_hash); if let Some(ref n) = self.name { @@ -95,3 +99,21 @@ impl Torrent { b } } + +fn size(value: u64) -> String { + const KB: f32 = 1024.0; + const MB: f32 = KB * KB; + const GB: f32 = MB * KB; + + let f = value as f32; + + if f < KB { + format!("{value} B") + } else if f < MB { + format!("{:.2} KB", f / KB) + } else if f < GB { + format!("{:.2} MB", f / MB) + } else { + format!("{:.2} GB", f / GB) + } +} diff --git a/src/torrent/file.rs b/src/torrent/file.rs index c1b85b8..f3a7b30 100644 --- a/src/torrent/file.rs +++ b/src/torrent/file.rs @@ -6,10 +6,16 @@ pub struct File { } impl File { + // format getters + 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/index.html.tera b/templates/index.html.tera index e915bd4..fd8ba2c 100644 --- a/templates/index.html.tera +++ b/templates/index.html.tera @@ -10,7 +10,7 @@
  • {{ row.indexed }}
  • {% if row.created %}
  • ({{ row.created }})
  • {% endif %} -
  • {{ row.size | filesizeformat }}
  • +
  • {{ row.size }}
  • {% if row.files %}
  • {{ row.files }} file{{ row.files | pluralize(plural="s") }}
  • {% endif %} {% if row.scrape %}
  • {{ row.scrape.seeders }}
  • diff --git a/templates/info.html.tera b/templates/info.html.tera index 7a9f0c2..b1ba273 100644 --- a/templates/info.html.tera +++ b/templates/info.html.tera @@ -9,7 +9,7 @@ {% if created %}
  • ({{ created }})
  • {% endif %} -
  • {{ size | filesizeformat }}
  • +
  • {{ size }}
  • {% if files_total %}
  • {{ files_total }} file{{ files_total | pluralize(plural="s") }}
  • {% endif %} @@ -42,7 +42,7 @@ {{ file.path }} {% endif %} - {{ file.size | filesizeformat }} + {{ file.size }} {% endfor %}