replace filesizeformat filter with app implementation

This commit is contained in:
yggverse 2025-09-11 17:47:42 +03:00
parent fc8f9dbc3a
commit ba692f9d2a
6 changed files with 37 additions and 28 deletions

View file

@ -69,8 +69,7 @@ impl Feed {
));
self.buffer.push_str("<description>");
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('"', "&quot;")
.replace("'", "&apos;")
}
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)
}
}

View file

@ -33,7 +33,7 @@ fn index(
indexed: String,
magnet: String,
scrape: Option<scrape::Result>,
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<String>,
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::<Vec<F>>()
@ -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
},
))

View file

@ -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<url::Url>>) -> 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)
}
}

View file

@ -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)
}
}

View file

@ -10,7 +10,7 @@
<ul>
<li><span title="Indexed">{{ row.indexed }}</span></li>
{% if row.created %}<li><span title="Created">({{ row.created }})</span></li>{% endif %}
<li><span title="Size">{{ row.size | filesizeformat }}</span></li>
<li><span title="Size">{{ row.size }}</span></li>
{% if row.files %}<li><span title="Files">{{ row.files }} file{{ row.files | pluralize(plural="s") }}</span></li>{% endif %}
{% if row.scrape %}
<li><span title="Seeders" class="seeders">{{ row.scrape.seeders }}</span></li>

View file

@ -9,7 +9,7 @@
{% if created %}
<li><span title="Created">({{ created }})</span></li>
{% endif %}
<li><span title="Size">{{ size | filesizeformat }}</span></li>
<li><span title="Size">{{ size }}</span></li>
{% if files_total %}
<li><span title="Files">{{ files_total }} file{{ files_total | pluralize(plural="s") }}</span></li>
{% endif %}
@ -42,7 +42,7 @@
{{ file.path }}
{% endif %}
</td>
<td>{{ file.size | filesizeformat }}</td>
<td>{{ file.size }}</td>
</tr>
{% endfor %}
</tbody>