Skip to content

Commit 5e086a2

Browse files
committed
feat: Display pending torrents in content folders
1 parent c7dee48 commit 5e086a2

4 files changed

Lines changed: 57 additions & 1 deletion

File tree

src/database/content_folder/folder_view.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use crate::database::torrent;
2+
13
use super::*;
24

35
/// A loaded folder, with all surrounding entities loaded as well:
@@ -11,6 +13,7 @@ pub struct FolderView {
1113
pub ancestors: Vec<Model>,
1214
pub children: Vec<Model>,
1315
pub folder: Option<Model>,
16+
pub torrents: Vec<torrent::Model>,
1417
}
1518

1619
impl FolderView {
@@ -27,6 +30,7 @@ impl FolderView {
2730
ancestors: vec![],
2831
folder: None,
2932
children,
33+
torrents: vec![],
3034
})
3135
}
3236

@@ -43,6 +47,8 @@ impl FolderView {
4347
let list = operator.list().await?;
4448

4549
if let Some(folder) = list.iter().find(|x| x.id == id) {
50+
let torrents = operator.torrent().list_for_folder(folder).await.unwrap();
51+
4652
Ok(Self {
4753
ancestors: folder
4854
.ancestors_from_list(&list)
@@ -55,6 +61,7 @@ impl FolderView {
5561
.cloned()
5662
.collect(),
5763
folder: Some(folder.clone()),
64+
torrents,
5865
})
5966
} else {
6067
Err(ContentFolderError::NotFound { id })

src/database/torrent/operator.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,22 @@ impl TorrentOperator<'_> {
5252
.context(DBSnafu)
5353
}
5454

55+
/// List torrents for a given content folder
56+
///
57+
/// Should not fail, unless SQLite was corrupted for some reason.
58+
pub async fn list_for_folder(
59+
&self,
60+
folder: &content_folder::Model,
61+
) -> Result<Vec<Model>, TorrentError> {
62+
// TODO: optimization
63+
Ok(self
64+
.list()
65+
.await?
66+
.into_iter()
67+
.filter(|x| x.content_folder_id == folder.id)
68+
.collect())
69+
}
70+
5571
/// Count torrents
5672
///
5773
/// Should not fail, unless SQLite was corrupted for some reason.

src/routes/content_folder.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use axum::extract::Path;
55
use axum_extra::extract::CookieJar;
66
use serde::{Deserialize, Serialize};
77

8-
use crate::database::content_folder;
8+
use crate::database::{content_folder, torrent};
99
use crate::state::AppStateContext;
1010
use crate::state::error::AppStateError;
1111
use crate::state::flash_message::{
@@ -24,6 +24,8 @@ pub struct ContentFolderShowTemplate {
2424
pub state: AppStateContext,
2525
/// Current folder, unless we're on the index page
2626
pub folder: Option<content_folder::Model>,
27+
/// Torrents associated with the content folder (empty on index page)
28+
pub torrents: Vec<torrent::Model>,
2729
/// Folders with parent_id set to current folder
2830
// TODO: order by alphanumeric
2931
pub children: Vec<content_folder::Model>,
@@ -39,12 +41,14 @@ impl ContentFolderShowTemplate {
3941
ancestors,
4042
children,
4143
folder,
44+
torrents,
4245
} = folder;
4346

4447
Self {
4548
children,
4649
flash: None,
4750
folder,
51+
torrents,
4852
ancestors,
4953
state: context,
5054
}

templates/content_folders/show.html

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,35 @@ <h4 class="mb-0">{% block folder_title %}{% if let Some(folder) = folder %}{{ fo
4545
</div>
4646
</div>
4747

48+
{% if torrents.len() > 0 %}
49+
<div class="card mt-4">
50+
<div class="card-body">
51+
<div class="p-4">
52+
<div class="d-flex align-items-center mb-4">
53+
<h4 class="mb-0">{{ torrents.len() }} torrents waiting for confirmation in this folder</h4>
54+
</div>
55+
</div>
56+
<ul class="list-group">
57+
{% for torrent in torrents %}
58+
{% if let Some(torrent_file) = torrent.torrent_file %}
59+
<li>
60+
<span>{{ torrent.name }} will create the following folders/files in this folder:</span>
61+
{# TODO: file tree in hightorrent #}
62+
<ul>
63+
{% for file in torrent_file.decoded.files().unwrap() %}
64+
<li>({{ file.size | filesizeformat }}) {{ file.path.to_str().unwrap() }}</li>
65+
{% endfor %}
66+
</ul>
67+
</li>
68+
{% else %}
69+
<li><span>{{ torrent.name }} is still resolving so we can't know the files yet</span></li>
70+
{% endif %}
71+
{% endfor %}
72+
</ul>
73+
</div>
74+
</div>
75+
{% endif %}
76+
4877
<ul class="list-group">
4978
{% block system_list %}
5079
{% if children.is_empty() %}

0 commit comments

Comments
 (0)