implement pagination

This commit is contained in:
yggverse 2025-08-05 17:59:45 +03:00
parent d014358028
commit 4e4f260190
5 changed files with 67 additions and 36 deletions

View file

@ -14,7 +14,7 @@ pub struct Config {
/// Default listing limit /// Default listing limit
#[arg(long, default_value_t = 50)] #[arg(long, default_value_t = 50)]
pub limit: usize, pub list_limit: usize,
/// Default capacity (estimated torrents in `storage`) /// Default capacity (estimated torrents in `storage`)
#[arg(long, default_value_t = 1000)] #[arg(long, default_value_t = 1000)]

View file

@ -30,8 +30,12 @@ pub struct Meta {
pub trackers: Option<HashSet<Url>>, pub trackers: Option<HashSet<Url>>,
} }
#[get("/")] #[get("/?<page>")]
fn index(storage: &State<Storage>, meta: &State<Meta>) -> Result<Template, Custom<String>> { fn index(
page: Option<usize>,
storage: &State<Storage>,
meta: &State<Meta>,
) -> Result<Template, Custom<String>> {
#[derive(Serialize)] #[derive(Serialize)]
#[serde(crate = "rocket::serde")] #[serde(crate = "rocket::serde")]
struct Row { struct Row {
@ -41,25 +45,32 @@ fn index(storage: &State<Storage>, meta: &State<Meta>) -> Result<Template, Custo
size: String, size: String,
torrent: Torrent, torrent: Torrent,
} }
Ok(Template::render( let rows = storage
"index",
context! {
meta: meta.inner(),
rows: storage
.torrents( .torrents(
Some((Sort::Modified, Order::Asc)), Some((Sort::Modified, Order::Asc)),
page.map(|p| if p > 0 { p - 1 } else { p } * storage.default_limit),
Some(storage.default_limit), Some(storage.default_limit),
) )
.map_err(|e| Custom(Status::InternalServerError, e.to_string()))? .map_err(|e| Custom(Status::InternalServerError, e.to_string()))?
.into_iter() .into_iter()
.map(|torrent| Row { .map(|torrent| Row {
created: torrent.creation_date.map(|t|t.format(&meta.format_time).to_string()), created: torrent
.creation_date
.map(|t| t.format(&meta.format_time).to_string()),
indexed: torrent.time.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: format::magnet(&torrent.info_hash, meta.trackers.as_ref()),
size: format::bytes(torrent.size), size: format::bytes(torrent.size),
torrent, torrent,
}) })
.collect::<Vec<Row>>() .collect::<Vec<Row>>();
Ok(Template::render(
"index",
context! {
meta: meta.inner(),
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<Feed>, storage: &State<Storage>) -> Result<RawXml<String>, C
for torrent in storage for torrent in storage
.torrents( .torrents(
Some((Sort::Modified, Order::Asc)), Some((Sort::Modified, Order::Asc)),
None,
Some(storage.default_limit), Some(storage.default_limit),
) )
.map_err(|e| Custom(Status::InternalServerError, e.to_string()))? .map_err(|e| Custom(Status::InternalServerError, e.to_string()))?
@ -89,7 +101,7 @@ fn rocket() -> _ {
config.link.clone(), config.link.clone(),
config.tracker.clone().map(|u| u.into_iter().collect()), 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() rocket::build()
.attach(Template::fairing()) .attach(Template::fairing())
.configure(rocket::Config { .configure(rocket::Config {

View file

@ -74,12 +74,13 @@ impl Storage {
pub fn torrents( pub fn torrents(
&self, &self,
sort_order: Option<(Sort, Order)>, sort_order: Option<(Sort, Order)>,
start: Option<usize>,
limit: Option<usize>, limit: Option<usize>,
) -> Result<Vec<Torrent>, String> { ) -> Result<Vec<Torrent>, String> {
let f = self.files(sort_order)?; let f = self.files(sort_order)?;
let l = limit.unwrap_or(f.len()); let l = limit.unwrap_or(f.len());
let mut b = Vec::with_capacity(l); 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 if file
.path() .path()
.extension() .extension()

View file

@ -1,5 +1,6 @@
{% extends "layout/default" %} {% extends "layout/default" %}
{% block content %} {% block content %}
{% if rows %}
{% for row in rows %} {% for row in rows %}
<div> <div>
<a name="{{ row.torrent.info_hash }}"></a> <a name="{{ row.torrent.info_hash }}"></a>
@ -19,4 +20,11 @@
</div> </div>
</div> </div>
{% endfor %} {% endfor %}
{% else %}
<div>
Nothing.
</div>
{% endif %}
{% if next %}<a href="{{ next }}">Next</a>{% endif %}
{% if back %}<a href="{{ back }}">Back</a>{% endif %}
{% endblock content %} {% endblock content %}

View file

@ -86,6 +86,16 @@
margin: 0 auto; margin: 0 auto;
} }
/* pagination */
main > a {
background: #34384f;
border-radius: 3px;
float: right;
margin-left: 8px;
opacity: .96;
padding: 8px;
}
/* item row */ /* item row */
main > div { main > div {
background-color: #34384f; background-color: #34384f;