diff --git a/src/feed.rs b/src/feed.rs
index f20b0fe..64c4e21 100644
--- a/src/feed.rs
+++ b/src/feed.rs
@@ -1,4 +1,4 @@
-use crate::{Torrent, format};
+use crate::Torrent;
use url::Url;
/// Export crawl index to the RSS file
@@ -68,11 +68,11 @@ impl Feed {
.map(|b| b.to_string())
.unwrap_or("?".into()) // @TODO
),
- escape(format::magnet(&torrent.info_hash, self.trackers.as_ref()))
+ escape(torrent.magnet(self.trackers.as_ref()))
));
buffer.push_str("");
- buffer.push_str(&escape(format::bytes(torrent.size)));
+ buffer.push_str(&format!("{}\n{}", torrent.size(), torrent.files()));
buffer.push_str("");
buffer.push_str("");
diff --git a/src/format.rs b/src/format.rs
deleted file mode 100644
index 5f5b440..0000000
--- a/src/format.rs
+++ /dev/null
@@ -1,32 +0,0 @@
-pub fn bytes(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)
- }
-}
-
-pub fn magnet(info_hash: &str, trackers: Option<&Vec>) -> String {
- let mut b = if info_hash.len() == 40 {
- format!("magnet:?xt=urn:btih:{info_hash}")
- } else {
- todo!("info-hash v2 yet not supported") // librqbit_core::hash_id::Id
- };
- if let Some(t) = trackers {
- for tracker in t {
- b.push_str("&tr=");
- b.push_str(&urlencoding::encode(tracker.as_str()))
- }
- }
- b
-}
diff --git a/src/main.rs b/src/main.rs
index 6fd186a..7f3cc91 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -3,13 +3,13 @@ extern crate rocket;
mod config;
mod feed;
-mod format;
mod scraper;
mod storage;
mod torrent;
use config::Config;
use feed::Feed;
+use plurify::Plurify;
use rocket::{
State,
http::Status,
@@ -41,8 +41,6 @@ fn index(
storage: &State,
meta: &State,
) -> Result> {
- use plurify::Plurify;
-
#[derive(Serialize)]
#[serde(crate = "rocket::serde")]
struct Row {
@@ -81,13 +79,10 @@ fn index(
.creation_date
.map(|t| t.format(&meta.format_time).to_string()),
indexed: torrent.time.format(&meta.format_time).to_string(),
- magnet: format::magnet(&torrent.info_hash, meta.trackers.as_ref()),
+ magnet: torrent.magnet(meta.trackers.as_ref()),
scrape: scraper.scrape(&torrent.info_hash),
- size: format::bytes(torrent.size),
- files: torrent.files.as_ref().map_or("1 file".into(), |f| {
- let l = f.len();
- format!("{l} {}", l.plurify(&["file", "files", "files"]))
- }),
+ size: torrent.size(),
+ files: torrent.files(),
torrent,
}),
Err(e) => {
diff --git a/src/torrent.rs b/src/torrent.rs
index 7d06a77..61956cd 100644
--- a/src/torrent.rs
+++ b/src/torrent.rs
@@ -76,4 +76,47 @@ impl Torrent {
time,
})
}
+
+ // Format getters
+
+ pub fn files(&self) -> String {
+ use plurify::Plurify;
+ self.files.as_ref().map_or("1 file".into(), |f| {
+ let l = f.len();
+ format!("{l} {}", l.plurify(&["file", "files", "files"]))
+ })
+ }
+
+ pub fn size(&self) -> String {
+ const KB: f32 = 1024.0;
+ const MB: f32 = KB * KB;
+ const GB: f32 = MB * KB;
+
+ let f = self.size as f32;
+
+ if f < KB {
+ format!("{} B", self.size)
+ } else if f < MB {
+ format!("{:.2} KB", f / KB)
+ } else if f < GB {
+ format!("{:.2} MB", f / MB)
+ } else {
+ format!("{:.2} GB", f / GB)
+ }
+ }
+
+ pub fn magnet(&self, trackers: Option<&Vec>) -> String {
+ let mut b = if self.info_hash.len() == 40 {
+ format!("magnet:?xt=urn:btih:{}", self.info_hash)
+ } else {
+ todo!("info-hash v2 yet not supported") // librqbit_core::hash_id::Id
+ };
+ if let Some(t) = trackers {
+ for tracker in t {
+ b.push_str("&tr=");
+ b.push_str(&urlencoding::encode(tracker.as_str()))
+ }
+ }
+ b
+ }
}