mirror of
https://github.com/YGGverse/btracker.git
synced 2026-03-31 09:05:30 +00:00
replace filesizeformat filter with app implementation
This commit is contained in:
parent
fc8f9dbc3a
commit
ba692f9d2a
6 changed files with 37 additions and 28 deletions
21
src/feed.rs
21
src/feed.rs
|
|
@ -69,8 +69,7 @@ impl Feed {
|
||||||
));
|
));
|
||||||
|
|
||||||
self.buffer.push_str("<description>");
|
self.buffer.push_str("<description>");
|
||||||
self.buffer
|
self.buffer.push_str(&format!("size: {}", torrent.size()));
|
||||||
.push_str(&format!("size: {}", size(torrent.size)));
|
|
||||||
if let Some(f) = torrent.files() {
|
if let Some(f) = torrent.files() {
|
||||||
self.buffer.push_str(&format!(" / files: {f}"));
|
self.buffer.push_str(&format!(" / files: {f}"));
|
||||||
}
|
}
|
||||||
|
|
@ -101,21 +100,3 @@ fn escape(value: &str) -> String {
|
||||||
.replace('"', """)
|
.replace('"', """)
|
||||||
.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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
10
src/main.rs
10
src/main.rs
|
|
@ -33,7 +33,7 @@ fn index(
|
||||||
indexed: String,
|
indexed: String,
|
||||||
magnet: String,
|
magnet: String,
|
||||||
scrape: Option<scrape::Result>,
|
scrape: Option<scrape::Result>,
|
||||||
size: usize,
|
size: String,
|
||||||
torrent: Torrent,
|
torrent: Torrent,
|
||||||
}
|
}
|
||||||
let (total, torrents) = public
|
let (total, torrents) = public
|
||||||
|
|
@ -83,7 +83,7 @@ fn index(
|
||||||
indexed: torrent.time.format(&meta.format_time).to_string(),
|
indexed: torrent.time.format(&meta.format_time).to_string(),
|
||||||
magnet: torrent.magnet(meta.trackers.as_ref()),
|
magnet: torrent.magnet(meta.trackers.as_ref()),
|
||||||
scrape: scrape::get(scrape, torrent.id.0),
|
scrape: scrape::get(scrape, torrent.id.0),
|
||||||
size: torrent.size as usize, // required by `filesizeformat` impl
|
size: torrent.size(),
|
||||||
torrent
|
torrent
|
||||||
}),
|
}),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
|
@ -115,7 +115,7 @@ fn info(
|
||||||
struct F {
|
struct F {
|
||||||
href: Option<String>,
|
href: Option<String>,
|
||||||
path: String,
|
path: String,
|
||||||
size: usize,
|
size: String,
|
||||||
}
|
}
|
||||||
let torrent = Torrent::from_public(&t.bytes, t.time).map_err(|e| {
|
let torrent = Torrent::from_public(&t.bytes, t.time).map_err(|e| {
|
||||||
error!("Torrent parse error: `{e}`");
|
error!("Torrent parse error: `{e}`");
|
||||||
|
|
@ -143,7 +143,7 @@ fn info(
|
||||||
F {
|
F {
|
||||||
href: public.href(&torrent.info_hash, &p),
|
href: public.href(&torrent.info_hash, &p),
|
||||||
path: p,
|
path: p,
|
||||||
size: f.length as usize, // required by `filesizeformat` impl
|
size: f.size(),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect::<Vec<F>>()
|
.collect::<Vec<F>>()
|
||||||
|
|
@ -151,7 +151,7 @@ fn info(
|
||||||
indexed: torrent.time.format(&meta.format_time).to_string(),
|
indexed: torrent.time.format(&meta.format_time).to_string(),
|
||||||
magnet: torrent.magnet(meta.trackers.as_ref()),
|
magnet: torrent.magnet(meta.trackers.as_ref()),
|
||||||
scrape: scrape::get(scrape, i.0),
|
scrape: scrape::get(scrape, i.0),
|
||||||
size: torrent.size as usize, // required by `filesizeformat` impl
|
size: torrent.size(),
|
||||||
torrent
|
torrent
|
||||||
},
|
},
|
||||||
))
|
))
|
||||||
|
|
|
||||||
|
|
@ -80,6 +80,10 @@ impl Torrent {
|
||||||
self.files.as_ref().map(|f| f.len())
|
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 {
|
pub fn magnet(&self, trackers: Option<&Vec<url::Url>>) -> String {
|
||||||
let mut b = format!("magnet:?xt=urn:btih:{}", self.info_hash);
|
let mut b = format!("magnet:?xt=urn:btih:{}", self.info_hash);
|
||||||
if let Some(ref n) = self.name {
|
if let Some(ref n) = self.name {
|
||||||
|
|
@ -95,3 +99,21 @@ impl Torrent {
|
||||||
b
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,10 +6,16 @@ pub struct File {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl File {
|
impl File {
|
||||||
|
// format getters
|
||||||
|
|
||||||
pub fn path(&self) -> String {
|
pub fn path(&self) -> String {
|
||||||
self.path
|
self.path
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|p| p.to_string_lossy().into())
|
.map(|p| p.to_string_lossy().into())
|
||||||
.unwrap_or("?".into())
|
.unwrap_or("?".into())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn size(&self) -> String {
|
||||||
|
super::size(self.length)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@
|
||||||
<ul>
|
<ul>
|
||||||
<li><span title="Indexed">{{ row.indexed }}</span></li>
|
<li><span title="Indexed">{{ row.indexed }}</span></li>
|
||||||
{% if row.created %}<li><span title="Created">({{ row.created }})</span></li>{% endif %}
|
{% 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.files %}<li><span title="Files">{{ row.files }} file{{ row.files | pluralize(plural="s") }}</span></li>{% endif %}
|
||||||
{% if row.scrape %}
|
{% if row.scrape %}
|
||||||
<li><span title="Seeders" class="seeders">{{ row.scrape.seeders }}</span></li>
|
<li><span title="Seeders" class="seeders">{{ row.scrape.seeders }}</span></li>
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@
|
||||||
{% if created %}
|
{% if created %}
|
||||||
<li><span title="Created">({{ created }})</span></li>
|
<li><span title="Created">({{ created }})</span></li>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<li><span title="Size">{{ size | filesizeformat }}</span></li>
|
<li><span title="Size">{{ size }}</span></li>
|
||||||
{% if files_total %}
|
{% if files_total %}
|
||||||
<li><span title="Files">{{ files_total }} file{{ files_total | pluralize(plural="s") }}</span></li>
|
<li><span title="Files">{{ files_total }} file{{ files_total | pluralize(plural="s") }}</span></li>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
@ -42,7 +42,7 @@
|
||||||
{{ file.path }}
|
{{ file.path }}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</td>
|
</td>
|
||||||
<td>{{ file.size | filesizeformat }}</td>
|
<td>{{ file.size }}</td>
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</tbody>
|
</tbody>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue