diff --git a/src/config.rs b/src/config.rs index dbd4c6a..a330145 100644 --- a/src/config.rs +++ b/src/config.rs @@ -14,7 +14,7 @@ pub struct Config { /// Default listing limit #[arg(long, default_value_t = 50)] - pub limit: usize, + pub list_limit: usize, /// Default capacity (estimated torrents in `storage`) #[arg(long, default_value_t = 1000)] diff --git a/src/main.rs b/src/main.rs index 9b4fbbd..e1d5e89 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,8 +30,12 @@ pub struct Meta { pub trackers: Option>, } -#[get("/")] -fn index(storage: &State, meta: &State) -> Result> { +#[get("/?")] +fn index( + page: Option, + storage: &State, + meta: &State, +) -> Result> { #[derive(Serialize)] #[serde(crate = "rocket::serde")] struct Row { @@ -41,25 +45,32 @@ fn index(storage: &State, meta: &State) -> Result 0 { p - 1 } else { p } * storage.default_limit), + Some(storage.default_limit), + ) + .map_err(|e| Custom(Status::InternalServerError, e.to_string()))? + .into_iter() + .map(|torrent| Row { + created: torrent + .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()), + size: format::bytes(torrent.size), + torrent, + }) + .collect::>(); Ok(Template::render( "index", context! { meta: meta.inner(), - rows: storage - .torrents( - Some((Sort::Modified, Order::Asc)), - Some(storage.default_limit), - ) - .map_err(|e| Custom(Status::InternalServerError, e.to_string()))? - .into_iter() - .map(|torrent| Row { - created: torrent.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()), - size: format::bytes(torrent.size), - torrent, - }) - .collect::>() + back: page.map(|p| uri!(index(if p > 2 { Some(p - 1) } else { None }))), + next: if rows.len() < storage.default_limit { None } + else { Some(uri!(index(Some(page.map_or(2, |p| p + 1))))) }, + rows }, )) } @@ -70,6 +81,7 @@ fn rss(feed: &State, storage: &State) -> Result, C for torrent in storage .torrents( Some((Sort::Modified, Order::Asc)), + None, Some(storage.default_limit), ) .map_err(|e| Custom(Status::InternalServerError, e.to_string()))? @@ -89,7 +101,7 @@ fn rocket() -> _ { config.link.clone(), config.tracker.clone().map(|u| u.into_iter().collect()), ); - let storage = Storage::init(config.storage, config.limit, config.capacity).unwrap(); // @TODO handle + let storage = Storage::init(config.storage, config.list_limit, config.capacity).unwrap(); // @TODO handle rocket::build() .attach(Template::fairing()) .configure(rocket::Config { diff --git a/src/storage.rs b/src/storage.rs index 22893da..f77d0eb 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -74,12 +74,13 @@ impl Storage { pub fn torrents( &self, sort_order: Option<(Sort, Order)>, + start: Option, limit: Option, ) -> Result, String> { let f = self.files(sort_order)?; let l = limit.unwrap_or(f.len()); let mut b = Vec::with_capacity(l); - for file in f.into_iter().take(l) { + for file in f.into_iter().skip(start.unwrap_or_default()).take(l) { if file .path() .extension() diff --git a/templates/index.html.tera b/templates/index.html.tera index a7c91f1..ab4b54c 100644 --- a/templates/index.html.tera +++ b/templates/index.html.tera @@ -1,22 +1,30 @@ {% extends "layout/default" %} {% block content %} -{% for row in rows %} -
- -

{{ row.torrent.name }}

- {% if row.torrent.comment %}

{{ row.torrent.comment }}

{% endif %} +{% if rows %} + {% for row in rows %}
-
    -
  • {{ row.indexed }}{% - if row.created %} ({{ row.created }}){% endif %}
  • {% - if row.size %}
  • {{ row.size }}
  • {% endif %} -
- - - - - + +

{{ row.torrent.name }}

+ {% if row.torrent.comment %}

{{ row.torrent.comment }}

{% endif %} +
+
    +
  • {{ row.indexed }}{% + if row.created %} ({{ row.created }}){% endif %}
  • {% + if row.size %}
  • {{ row.size }}
  • {% endif %} +
+ + + + + +
+ {% endfor %} +{% else %} +
+ Nothing.
-{% endfor %} +{% endif %} +{% if next %}Next{% endif %} +{% if back %}Back{% endif %} {% endblock content %} \ No newline at end of file diff --git a/templates/layout/default.html.tera b/templates/layout/default.html.tera index 2242a10..1e23cf7 100644 --- a/templates/layout/default.html.tera +++ b/templates/layout/default.html.tera @@ -86,6 +86,16 @@ margin: 0 auto; } + /* pagination */ + main > a { + background: #34384f; + border-radius: 3px; + float: right; + margin-left: 8px; + opacity: .96; + padding: 8px; + } + /* item row */ main > div { background-color: #34384f;