diff --git a/src/format.rs b/src/format.rs deleted file mode 100644 index e70e158..0000000 --- a/src/format.rs +++ /dev/null @@ -1,30 +0,0 @@ -use crate::{Meta, Scrape, Scraper, Torrent}; -use rocket::{State, serde::Serialize}; - -#[derive(Serialize)] -#[serde(crate = "rocket::serde")] -pub struct Format { - pub created: Option, - pub files: String, - pub indexed: String, - pub magnet: String, - pub scrape: Option, - pub size: String, - pub torrent: Torrent, -} - -impl Format { - pub fn from_torrent(torrent: Torrent, scraper: &State, meta: &State) -> Self { - Self { - created: torrent - .creation_date - .map(|t| t.format(&meta.format_time).to_string()), - indexed: torrent.time.format(&meta.format_time).to_string(), - magnet: torrent.magnet(meta.trackers.as_ref()), - scrape: scraper.scrape(&torrent.info_hash), - size: torrent.size(), - files: torrent.files(), - torrent, - } - } -} diff --git a/src/main.rs b/src/main.rs index 23db5d1..2e1c281 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,6 @@ extern crate rocket; mod config; mod feed; -mod format; mod meta; mod scraper; mod storage; @@ -11,13 +10,13 @@ mod torrent; use config::Config; use feed::Feed; -use format::Format; use meta::Meta; use plurify::Plurify; use rocket::{ State, http::Status, response::{content::RawXml, status::Custom}, + serde::Serialize, }; use rocket_dyn_templates::{Template, context}; use scraper::{Scrape, Scraper}; @@ -32,6 +31,17 @@ fn index( storage: &State, meta: &State, ) -> Result> { + #[derive(Serialize)] + #[serde(crate = "rocket::serde")] + struct R { + created: Option, + files: String, + indexed: String, + magnet: String, + scrape: Option, + size: String, + torrent: Torrent, + } let (total, torrents) = storage .torrents( Some((Sort::Modified, Order::Desc)), @@ -42,7 +52,6 @@ fn index( error!("Torrents storage read error: `{e}`"); Custom(Status::InternalServerError, E.to_string()) })?; - Ok(Template::render( "index", context! { @@ -53,13 +62,21 @@ fn index( rows: torrents .into_iter() .filter_map(|t| match Torrent::from_storage(&t.bytes, t.time) { - Ok(torrent) => Some(Format::from_torrent(torrent, scraper, meta)), + Ok(torrent) => Some(R { + created: torrent.creation_date.map(|t| t.format(&meta.format_time).to_string()), + files: torrent.files(), + indexed: torrent.time.format(&meta.format_time).to_string(), + magnet: torrent.magnet(meta.trackers.as_ref()), + scrape: scraper.scrape(&torrent.info_hash), + size: torrent.size(), + torrent + }), Err(e) => { error!("Torrent storage read error: `{e}`"); None } }) - .collect::>(), + .collect::>(), pagination_totals: format!( "Page {} / {} ({total} {} total)", page.unwrap_or(1), @@ -81,19 +98,39 @@ fn info( warn!("Torrent info-hash parse error: `{e}`"); Custom(Status::BadRequest, Status::BadRequest.to_string()) })?) { - Some(t) => Ok(Template::render( - "info", - context! { - meta: meta.inner(), - torrent: Format::from_torrent( - Torrent::from_storage(&t.bytes, t.time).map_err(|e| { - error!("Torrent parse error: `{e}`"); - Custom(Status::InternalServerError, E.to_string()) - })?, scraper, meta - ), - info_hash - }, - )), + Some(t) => { + #[derive(Serialize)] + #[serde(crate = "rocket::serde")] + struct F { + name: String, + size: String, + } + let torrent = Torrent::from_storage(&t.bytes, t.time).map_err(|e| { + error!("Torrent parse error: `{e}`"); + Custom(Status::InternalServerError, E.to_string()) + })?; + Ok(Template::render( + "info", + context! { + meta: meta.inner(), + created: torrent.creation_date.map(|t| t.format(&meta.format_time).to_string()), + files_total: torrent.files(), + files_list: torrent.files.as_ref().map(|f| { + f.iter() + .map(|f| F { + name: f.name(), + size: f.size(), + }) + .collect::>() + }), + indexed: torrent.time.format(&meta.format_time).to_string(), + magnet: torrent.magnet(meta.trackers.as_ref()), + scrape: scraper.scrape(info_hash), + size: torrent.size(), + torrent + }, + )) + } None => Err(Custom(Status::NotFound, E.to_string())), } } diff --git a/src/torrent.rs b/src/torrent.rs index 61956cd..fdbbdbf 100644 --- a/src/torrent.rs +++ b/src/torrent.rs @@ -88,21 +88,7 @@ impl Torrent { } 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) - } + size(self.size) } pub fn magnet(&self, trackers: Option<&Vec>) -> String { @@ -120,3 +106,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 744c0d3..b83c585 100644 --- a/src/torrent/file.rs +++ b/src/torrent/file.rs @@ -6,3 +6,12 @@ pub struct File { pub name: Option, pub length: u64, } + +impl File { + pub fn name(&self) -> String { + self.name.as_deref().unwrap_or("?").into() + } + pub fn size(&self) -> String { + super::size(self.length) + } +} diff --git a/static/theme/default.css b/static/theme/default.css index 25c705f..ad34f5e 100644 --- a/static/theme/default.css +++ b/static/theme/default.css @@ -20,9 +20,9 @@ --background: #282b3c; --default: #ccc; --item: #34384f; + --separator: #4f536a; } - body { background: var(--background); color: var(--default); @@ -47,6 +47,24 @@ h1, h2 { font-size: 14px; } +table { + border-collapse: collapse; + width: 100%; +} + +table td, +table th { + padding: 4px 8px; +} + +table > thead > tr > th { + text-align: left; +} + +table > tbody > tr:hover > td { + background: var(--background) +} + body > * { position: relative; overflow: hidden; @@ -108,7 +126,7 @@ main > div > p { /* item row meta, controls */ main > div > div { - border-top: 1px #4f536a solid; + border-top: 1px solid var(--separator); margin-top: 16px; overflow: hidden; padding-top: 16px; diff --git a/templates/index.html.tera b/templates/index.html.tera index 5e20cf5..fcd91a7 100644 --- a/templates/index.html.tera +++ b/templates/index.html.tera @@ -11,7 +11,7 @@
  • {{ row.indexed }}
  • {% if row.created %}
  • ({{ row.created }})
  • {% endif %} {% if row.size %}
  • {{ row.size }}
  • {% endif %} -
  • {{ row.files }}
  • +
  • {{ row.files }}
  • {% if row.scrape %}
  • {{ row.scrape.seeders }}
  • {{ row.scrape.peers }}
  • diff --git a/templates/info.html.tera b/templates/info.html.tera index 8125c44..476bee8 100644 --- a/templates/info.html.tera +++ b/templates/info.html.tera @@ -1,23 +1,42 @@ {% extends "layout/default" %} {% block content %}
    -

    {% if torrent.name %}{{ torrent.name }}{% else %}{{ info_hash }}{% endif %}

    +

    {% if torrent.name %}{{ torrent.name }}{% else %}{{ torrent.info_hash }}{% endif %}

    {% if torrent.comment %}

    {{ torrent.comment }}

    {% endif %}
      -
    • {{ torrent.indexed }}
    • - {% if torrent.created %}
    • ({{ torrent.created }})
    • {% endif %} - {% if torrent.size %}
    • {{ torrent.size }}
    • {% endif %} -
    • {{ torrent.files }}
    • - {% if torrent.scrape %} -
    • {{ torrent.scrape.seeders }}
    • -
    • {{ torrent.scrape.peers }}
    • -
    • {{ torrent.scrape.leechers }}
    • +
    • {{ indexed }}
    • + {% if created %}
    • ({{ created }})
    • {% endif %} +
    • {{ size }}
    • +
    • {{ files_total }}
    • + {% if scrape %} +
    • {{ scrape.seeders }}
    • +
    • {{ scrape.peers }}
    • +
    • {{ scrape.leechers }}
    • {% endif %}
    - +
    +
    + {% if files_list %} + + + + + + + + + {% for file in files_list %} + + + + + {% endfor %} + +
    FileSize
    {{ file.name }}{{ file.size }}
    + {% endif %}
    {% endblock content %} \ No newline at end of file