Skip to content

Commit 1bdfb08

Browse files
Gabatxo1312amateurforger
authored andcommitted
Add Waiting to import button for unimported resolved magnet
1 parent 1aa9bd4 commit 1bdfb08

10 files changed

Lines changed: 78 additions & 90 deletions

File tree

src/database/magnet.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,17 @@ impl MagnetOperator {
6161
.context(DBSnafu)
6262
}
6363

64+
/// List unresolved magnet
65+
///
66+
/// Should not fail, unless SQLite was corrupted for some reason.
67+
pub async fn list_resolved(&self) -> Result<Vec<Model>, MagnetError> {
68+
Entity::find()
69+
.filter(Column::Resolved.eq(true))
70+
.all(&self.state.database)
71+
.await
72+
.context(DBSnafu)
73+
}
74+
6475
pub async fn get(&self, id: i32) -> Result<Model, MagnetError> {
6576
let db = &self.state.database;
6677

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ pub fn router(state: state::AppState) -> Router {
3434
.route("/magnet/upload", post(routes::magnet::upload))
3535
.route("/magnet/upload", get(routes::magnet::get_upload))
3636
.route("/magnet", get(routes::magnet::list))
37-
.route("/magnet/{id}", get(routes::magnet::show))
3837
// Register static assets routes
3938
.nest("/assets", static_router())
4039
// Insert request timing

src/routes/category.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ use axum_extra::extract::CookieJar;
77
use serde::{Deserialize, Serialize};
88
use snafu::prelude::*;
99

10-
use crate::database::category;
1110
use crate::database::category::CategoryError;
12-
use crate::database::content_folder;
11+
use crate::database::{category, content_folder};
1312
use crate::extractors::normalized_path::*;
1413
use crate::state::flash_message::{OperationStatus, get_cookie};
1514
use crate::state::{AppStateContext, error::*};
@@ -31,11 +30,9 @@ pub struct NewCategoryTemplate {
3130
pub category_form: Option<CategoryForm>,
3231
}
3332

34-
pub async fn new(
35-
app_state_context: AppStateContext,
36-
) -> Result<impl axum::response::IntoResponse, AppStateError> {
33+
pub async fn new(context: AppStateContext) -> Result<NewCategoryTemplate, AppStateError> {
3734
Ok(NewCategoryTemplate {
38-
state: app_state_context,
35+
state: context,
3936
category_form: None,
4037
error: None,
4138
})

src/routes/magnet.rs

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use askama::Template;
22
use askama_web::WebTemplate;
3-
use axum::extract::{Form, Path};
4-
use axum::response::{IntoResponse, Response};
3+
use axum::extract::Form;
4+
use axum::response::{IntoResponse, Redirect, Response};
55
use serde::{Deserialize, Serialize};
66
use snafu::prelude::*;
77

8-
use crate::database::category;
9-
use crate::database::magnet;
8+
use crate::database::{category, magnet};
109
use crate::state::{AppStateContext, error::*};
1110

1211
/// Multipart form submitted to /magnet/upload:
@@ -17,55 +16,25 @@ pub struct MagnetForm {
1716
pub magnet: String,
1817
}
1918

20-
#[derive(Template, WebTemplate)]
21-
#[template(path = "magnet/show.html")]
22-
pub struct MagnetTemplate {
23-
/// Global application state (errors/warnings)
24-
pub state: AppStateContext,
25-
/// Parsed magnet from form
26-
pub magnet: magnet::Model,
27-
}
28-
29-
pub async fn show(
30-
context: AppStateContext,
31-
Path(id): Path<i32>,
32-
) -> Result<impl IntoResponse, AppStateError> {
33-
let magnet = context
34-
.db
35-
.magnet()
36-
.get(id)
37-
.await
38-
.boxed()
39-
.context(OtherSnafu)?;
40-
41-
Ok(MagnetTemplate {
42-
state: context,
43-
magnet,
44-
})
45-
}
46-
4719
pub async fn upload(
4820
context: AppStateContext,
4921
Form(form): Form<MagnetForm>,
5022
) -> Result<Response, AppStateError> {
51-
// Parse magnet
52-
match context
23+
// TODO: proper error type
24+
if let Err(e) = context
5325
.db
5426
.magnet()
5527
.create(&form)
5628
.await
5729
.context(MagnetUploadSnafu)
5830
{
59-
Ok(magnet_model) => Ok(MagnetTemplate {
60-
state: context,
61-
magnet: magnet_model,
62-
}
63-
.into_response()),
64-
Err(e) => Ok(UploadMagnetTemplate::new(context)
31+
return Ok(UploadMagnetTemplate::new(context)
6532
.await?
6633
.with_errored_form(form, e)
67-
.into_response()),
34+
.into_response());
6835
}
36+
37+
Ok(Redirect::to("/magnet").into_response())
6938
}
7039

7140
#[derive(Template, WebTemplate)]

src/state/context.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub struct AppStateContext {
1414
pub db: DatabaseOperator,
1515
pub errors: Vec<AppStateError>,
1616
pub free_space: FreeSpace,
17+
pub resolved_magnets_count: usize,
1718
pub state: AppState,
1819
pub user: Option<User>,
1920
}
@@ -26,11 +27,19 @@ impl FromRequestParts<AppState> for AppStateContext {
2627
state: &AppState,
2728
) -> Result<Self, Self::Rejection> {
2829
let user = User::from_request_parts(parts, state).await?;
30+
let db = DatabaseOperator::new(state.clone(), user.clone());
31+
let resolved_magnets_count = db
32+
.magnet()
33+
.list_resolved()
34+
.await
35+
.context(MagnetUploadSnafu)?
36+
.len();
2937

3038
Ok(Self {
31-
db: DatabaseOperator::new(state.clone(), user.clone()),
39+
db,
3240
errors: vec![],
3341
free_space: state.free_space()?,
42+
resolved_magnets_count,
3443
state: state.clone(),
3544
user,
3645
})

src/state/error.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,18 @@ pub struct AppStateErrorContextInner {
7777
// all errors to strings. Maybe related to:
7878
// https://github.com/askama-rs/askama/issues/393
7979
errors: Vec<AppStateError>,
80+
resolved_magnets_count: usize,
8081
}
8182

8283
impl From<AppStateError> for AppStateErrorContext {
8384
fn from(e: AppStateError) -> Self {
8485
// An error is being displayed to the user, make sure it's also written in the logs
8586
e.log();
8687
Self {
87-
state: AppStateErrorContextInner { errors: vec![e] },
88+
state: AppStateErrorContextInner {
89+
errors: vec![e],
90+
resolved_magnets_count: 0,
91+
},
8892
}
8993
}
9094
}

templates/content_folders/dropdown_actions.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
</button>
55
<ul class="dropdown-menu">
66
<li><a class="dropdown-item" href="#" data-bs-toggle="modal" data-bs-target="#createSubFolder">Create a subfolder</a></li>
7-
<li><a class="dropdown-item" href="#">Import magnet link</a></li>
7+
<li><a class="dropdown-item" href="/magnet/upload">Import magnet link</a></li>
88
<li><a class="dropdown-item" href="#">Import torrent file</a></li>
99
</ul>
1010
</div>

templates/magnet/list.html

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,38 @@
11
{% extends "base.html" %}
22

33
{% block main %}
4-
<div class="card mb-4">
5-
<h2>List of uploaded magnets not yet resolved</h2>
6-
<div class="table-responsive-lg">
7-
<table class="table">
8-
<thead>
9-
<tr>
10-
<th>Resolved</th>
11-
<th>Name</th>
12-
<th>Actions</th>
13-
</tr>
14-
</thead>
15-
{% for magnet in magnets %}
16-
<tr>
17-
<td class="py-3">{{ magnet.resolved }}</td>
18-
<td class="is-left" align="left"><a href="/magnet/{{ magnet.id }}">{{ magnet.name }}</a></td>
19-
<td>
20-
<div class="dropdown">
21-
<button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
22-
Actions
23-
</button>
24-
<ul class="dropdown-menu">
25-
<li><a class="dropdown-item" href="/magnet/{{ magnet.id }}">Voir</a></li>
26-
</ul>
4+
<div class="container">
5+
<h1>List of uploaded magnets not yet resolved</h1>
6+
7+
<div class="card my-3">
8+
<div class="card-body">
9+
<div class="table-responsive-lg">
10+
<table class="table align-middle">
11+
<thead>
12+
<tr>
13+
<th>Name</th>
14+
<th>Actions</th>
15+
</tr>
16+
</thead>
17+
{% for magnet in magnets %}
18+
<tr>
19+
<td class="is-left" align="left"><a href="/magnet/{{ magnet.id }}">{{ magnet.name }}</a></td>
20+
<td>
21+
{% if magnet.resolved %}
22+
<!-- TODO: mettre le lien vers la page des d'import d'un torrent -->
23+
<a href="#" class="btn btn-success">
24+
Importer
25+
</a>
26+
{% else %}
27+
<i class="fa fa-spinner fa-pulse me-2"></i>
28+
Resolving…
29+
{% endif %}
30+
</td>
31+
</tr>
32+
{% endfor %}
33+
</table>
2734
</div>
28-
</td>
29-
</tr>
30-
{% endfor %}
31-
</table>
32-
</div>
35+
</div>
36+
</div>
37+
</div>
3338
{% endblock main %}

templates/magnet/show.html

Lines changed: 0 additions & 5 deletions
This file was deleted.

templates/menus/header.html

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
<li class="nav-item">
1414
<a class="nav-link" href="/progress/everything">Torrents list</a>
1515
</li>
16-
<li class="nav-item">
17-
<a class="nav-link" href="/magnet">Magnets</a>
18-
</li>
1916
<li class="nav-item dropdown">
2017
<a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
2118
Categories
@@ -30,10 +27,12 @@
3027
</li>
3128
</ul>
3229
<span class="navbar-text">
33-
<a class="btn btn-success text-white" href="/magnet/upload">
34-
<i class="fa fa-download me-2"></i>
35-
Add magnet
36-
</a>
30+
{% if state.resolved_magnets_count != 0 %}
31+
<a class="btn btn-success text-white" href="/magnet">
32+
<i class="fa fa-upload me-2"></i>
33+
Waiting to be imported ({{ state.resolved_magnets_count }})
34+
</a>
35+
{% endif %}
3736
</span>
3837
</div>
3938
</div>

0 commit comments

Comments
 (0)