chore: bump version to 2.12.1
All checks were successful
Deploy with Docker Compose / deploy (push) Successful in 55s
All checks were successful
Deploy with Docker Compose / deploy (push) Successful in 55s
This commit is contained in:
10
Cargo.lock
generated
10
Cargo.lock
generated
@@ -64,7 +64,7 @@ checksum = "7f202df86484c868dbad7eaa557ef785d5c66295e41b460ef922eca0723b842c"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "api"
|
name = "api"
|
||||||
version = "2.12.0"
|
version = "2.12.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"argon2",
|
"argon2",
|
||||||
@@ -1233,7 +1233,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "indexer"
|
name = "indexer"
|
||||||
version = "2.12.0"
|
version = "2.12.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"axum",
|
"axum",
|
||||||
@@ -1667,7 +1667,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "notifications"
|
name = "notifications"
|
||||||
version = "2.12.0"
|
version = "2.12.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
@@ -1786,7 +1786,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "parsers"
|
name = "parsers"
|
||||||
version = "2.12.0"
|
version = "2.12.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"flate2",
|
"flate2",
|
||||||
@@ -2923,7 +2923,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "stripstream-core"
|
name = "stripstream-core"
|
||||||
version = "2.12.0"
|
version = "2.12.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"serde",
|
"serde",
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ resolver = "2"
|
|||||||
|
|
||||||
[workspace.package]
|
[workspace.package]
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
version = "2.12.0"
|
version = "2.12.1"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
|
||||||
[workspace.dependencies]
|
[workspace.dependencies]
|
||||||
|
|||||||
@@ -123,6 +123,7 @@ async fn main() -> anyhow::Result<()> {
|
|||||||
.route("/qbittorrent/add", axum::routing::post(qbittorrent::add_torrent))
|
.route("/qbittorrent/add", axum::routing::post(qbittorrent::add_torrent))
|
||||||
.route("/qbittorrent/test", get(qbittorrent::test_qbittorrent))
|
.route("/qbittorrent/test", get(qbittorrent::test_qbittorrent))
|
||||||
.route("/torrent-downloads", get(torrent_import::list_torrent_downloads))
|
.route("/torrent-downloads", get(torrent_import::list_torrent_downloads))
|
||||||
|
.route("/torrent-downloads/:id", axum::routing::delete(torrent_import::delete_torrent_download))
|
||||||
.route("/telegram/test", get(telegram::test_telegram))
|
.route("/telegram/test", get(telegram::test_telegram))
|
||||||
.route("/komga/sync", axum::routing::post(komga::sync_komga_read_books))
|
.route("/komga/sync", axum::routing::post(komga::sync_komga_read_books))
|
||||||
.route("/komga/reports", get(komga::list_sync_reports))
|
.route("/komga/reports", get(komga::list_sync_reports))
|
||||||
|
|||||||
@@ -143,10 +143,24 @@ pub async fn add_torrent(
|
|||||||
|
|
||||||
let sid = qbittorrent_login(&client, &base_url, &username, &password).await?;
|
let sid = qbittorrent_login(&client, &base_url, &username, &password).await?;
|
||||||
|
|
||||||
|
// Pre-generate the download ID so we can tag the torrent with it
|
||||||
|
let download_id = if is_managed { Some(Uuid::new_v4()) } else { None };
|
||||||
|
let tag = download_id.map(|id| format!("sl-{id}"));
|
||||||
|
|
||||||
|
// Snapshot existing torrents before adding (for hash resolution)
|
||||||
|
let torrents_before = if is_managed {
|
||||||
|
list_qbittorrent_torrents(&client, &base_url, &sid).await.unwrap_or_default()
|
||||||
|
} else {
|
||||||
|
Vec::new()
|
||||||
|
};
|
||||||
|
|
||||||
let mut form_params: Vec<(&str, &str)> = vec![("urls", &body.url)];
|
let mut form_params: Vec<(&str, &str)> = vec![("urls", &body.url)];
|
||||||
let savepath = "/downloads";
|
let savepath = "/downloads";
|
||||||
if is_managed {
|
if is_managed {
|
||||||
form_params.push(("savepath", savepath));
|
form_params.push(("savepath", savepath));
|
||||||
|
if let Some(ref t) = tag {
|
||||||
|
form_params.push(("tags", t));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let resp = client
|
let resp = client
|
||||||
@@ -172,9 +186,16 @@ pub async fn add_torrent(
|
|||||||
let library_id = body.library_id.unwrap();
|
let library_id = body.library_id.unwrap();
|
||||||
let series_name = body.series_name.as_deref().unwrap();
|
let series_name = body.series_name.as_deref().unwrap();
|
||||||
let expected_volumes = body.expected_volumes.as_deref().unwrap();
|
let expected_volumes = body.expected_volumes.as_deref().unwrap();
|
||||||
let qb_hash = extract_magnet_hash(&body.url);
|
|
||||||
|
|
||||||
let id = Uuid::new_v4();
|
// Try to resolve hash: first from magnet, then by querying qBittorrent
|
||||||
|
let mut qb_hash = extract_magnet_hash(&body.url);
|
||||||
|
if qb_hash.is_none() {
|
||||||
|
// For .torrent URLs: wait briefly then query qBittorrent to find the torrent
|
||||||
|
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
|
||||||
|
qb_hash = resolve_hash_from_qbittorrent(&client, &base_url, &sid, &torrents_before, series_name).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
let id = download_id.unwrap();
|
||||||
sqlx::query(
|
sqlx::query(
|
||||||
"INSERT INTO torrent_downloads (id, library_id, series_name, expected_volumes, qb_hash) \
|
"INSERT INTO torrent_downloads (id, library_id, series_name, expected_volumes, qb_hash) \
|
||||||
VALUES ($1, $2, $3, $4, $5)",
|
VALUES ($1, $2, $3, $4, $5)",
|
||||||
@@ -187,6 +208,8 @@ pub async fn add_torrent(
|
|||||||
.execute(&state.pool)
|
.execute(&state.pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
tracing::info!("Created torrent download {id} for {series_name}, qb_hash={qb_hash:?}");
|
||||||
|
|
||||||
Some(id)
|
Some(id)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
@@ -199,8 +222,9 @@ pub async fn add_torrent(
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Extract the info-hash from a magnet link (lowercased, hex or base32).
|
/// Extract the info-hash from a magnet link (lowercased hex).
|
||||||
/// magnet:?xt=urn:btih:HASH...
|
/// magnet:?xt=urn:btih:HASH...
|
||||||
|
/// Handles both hex (40 chars) and base32 (32 chars) encoded hashes.
|
||||||
fn extract_magnet_hash(url: &str) -> Option<String> {
|
fn extract_magnet_hash(url: &str) -> Option<String> {
|
||||||
let lower = url.to_lowercase();
|
let lower = url.to_lowercase();
|
||||||
let marker = "urn:btih:";
|
let marker = "urn:btih:";
|
||||||
@@ -210,7 +234,119 @@ fn extract_magnet_hash(url: &str) -> Option<String> {
|
|||||||
.find(|c: char| !c.is_alphanumeric())
|
.find(|c: char| !c.is_alphanumeric())
|
||||||
.unwrap_or(hash_part.len());
|
.unwrap_or(hash_part.len());
|
||||||
let hash = &hash_part[..end];
|
let hash = &hash_part[..end];
|
||||||
if hash.is_empty() { None } else { Some(hash.to_string()) }
|
if hash.is_empty() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
// 40-char hex hash: use as-is
|
||||||
|
if hash.len() == 40 && hash.chars().all(|c| c.is_ascii_hexdigit()) {
|
||||||
|
return Some(hash.to_string());
|
||||||
|
}
|
||||||
|
// 32-char base32 hash: decode to hex
|
||||||
|
if hash.len() == 32 {
|
||||||
|
if let Some(hex) = base32_to_hex(hash) {
|
||||||
|
return Some(hex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Fallback: return as-is (may not match qBittorrent)
|
||||||
|
Some(hash.to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Decode a base32-encoded string to a lowercase hex string.
|
||||||
|
fn base32_to_hex(input: &str) -> Option<String> {
|
||||||
|
let input = input.to_uppercase();
|
||||||
|
let alphabet = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
|
||||||
|
let mut bits: u64 = 0;
|
||||||
|
let mut bit_count = 0u32;
|
||||||
|
let mut bytes = Vec::with_capacity(20);
|
||||||
|
|
||||||
|
for ch in input.bytes() {
|
||||||
|
let val = alphabet.iter().position(|&c| c == ch)? as u64;
|
||||||
|
bits = (bits << 5) | val;
|
||||||
|
bit_count += 5;
|
||||||
|
if bit_count >= 8 {
|
||||||
|
bit_count -= 8;
|
||||||
|
bytes.push((bits >> bit_count) as u8);
|
||||||
|
bits &= (1u64 << bit_count) - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if bytes.len() != 20 {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
Some(bytes.iter().map(|b| format!("{b:02x}")).collect())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Torrent entry from qBittorrent API.
|
||||||
|
#[derive(Deserialize, Clone)]
|
||||||
|
struct QbTorrentEntry {
|
||||||
|
hash: String,
|
||||||
|
#[serde(default)]
|
||||||
|
name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// List all torrents currently in qBittorrent.
|
||||||
|
async fn list_qbittorrent_torrents(
|
||||||
|
client: &reqwest::Client,
|
||||||
|
base_url: &str,
|
||||||
|
sid: &str,
|
||||||
|
) -> Result<Vec<QbTorrentEntry>, ApiError> {
|
||||||
|
let resp = client
|
||||||
|
.get(format!("{base_url}/api/v2/torrents/info"))
|
||||||
|
.header("Cookie", format!("SID={sid}"))
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.map_err(|e| ApiError::internal(format!("qBittorrent list failed: {e}")))?;
|
||||||
|
|
||||||
|
if !resp.status().is_success() {
|
||||||
|
return Ok(Vec::new());
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(resp.json().await.unwrap_or_default())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Resolve the hash of a torrent after adding it to qBittorrent.
|
||||||
|
/// Strategy:
|
||||||
|
/// 1. Compare before/after snapshots to find the new torrent (works for new torrents)
|
||||||
|
/// 2. If no new torrent found (already existed), search by series name in torrent names
|
||||||
|
async fn resolve_hash_from_qbittorrent(
|
||||||
|
client: &reqwest::Client,
|
||||||
|
base_url: &str,
|
||||||
|
sid: &str,
|
||||||
|
torrents_before: &[QbTorrentEntry],
|
||||||
|
series_name: &str,
|
||||||
|
) -> Option<String> {
|
||||||
|
let torrents_after = list_qbittorrent_torrents(client, base_url, sid).await.ok()?;
|
||||||
|
let before_hashes: std::collections::HashSet<&str> = torrents_before.iter().map(|t| t.hash.as_str()).collect();
|
||||||
|
|
||||||
|
// Strategy 1: diff — find the one new torrent
|
||||||
|
let new_torrents: Vec<&QbTorrentEntry> = torrents_after.iter()
|
||||||
|
.filter(|t| !before_hashes.contains(t.hash.as_str()))
|
||||||
|
.collect();
|
||||||
|
if new_torrents.len() == 1 {
|
||||||
|
tracing::info!("[QBITTORRENT] Resolved hash {} via diff (new torrent: {})", new_torrents[0].hash, new_torrents[0].name);
|
||||||
|
return Some(new_torrents[0].hash.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Strategy 2: torrent already existed — search by series name in torrent names
|
||||||
|
let series_lower = series_name.to_lowercase();
|
||||||
|
// Normalize: "Dandadan" matches "Dandadan.T02.FRENCH.CBZ..."
|
||||||
|
let candidates: Vec<&QbTorrentEntry> = torrents_after.iter()
|
||||||
|
.filter(|t| t.name.to_lowercase().contains(&series_lower))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
if candidates.len() == 1 {
|
||||||
|
tracing::info!("[QBITTORRENT] Resolved hash {} via name match ({})", candidates[0].hash, candidates[0].name);
|
||||||
|
return Some(candidates[0].hash.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
if candidates.len() > 1 {
|
||||||
|
tracing::warn!("[QBITTORRENT] Multiple torrents match series '{}': {}", series_name,
|
||||||
|
candidates.iter().map(|c| c.name.as_str()).collect::<Vec<_>>().join(", "));
|
||||||
|
} else {
|
||||||
|
tracing::warn!("[QBITTORRENT] No torrent found matching series '{}'", series_name);
|
||||||
|
}
|
||||||
|
|
||||||
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Test connection to qBittorrent
|
/// Test connection to qBittorrent
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use axum::{extract::State, Json};
|
use axum::{extract::{Path, State}, Json};
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use sqlx::{PgPool, Row};
|
use sqlx::{PgPool, Row};
|
||||||
@@ -35,6 +35,9 @@ pub struct TorrentDownloadDto {
|
|||||||
pub status: String,
|
pub status: String,
|
||||||
pub imported_files: Option<serde_json::Value>,
|
pub imported_files: Option<serde_json::Value>,
|
||||||
pub error_message: Option<String>,
|
pub error_message: Option<String>,
|
||||||
|
pub progress: f32,
|
||||||
|
pub download_speed: i64,
|
||||||
|
pub eta: i64,
|
||||||
pub created_at: String,
|
pub created_at: String,
|
||||||
pub updated_at: String,
|
pub updated_at: String,
|
||||||
}
|
}
|
||||||
@@ -102,7 +105,7 @@ pub async fn list_torrent_downloads(
|
|||||||
) -> Result<Json<Vec<TorrentDownloadDto>>, ApiError> {
|
) -> Result<Json<Vec<TorrentDownloadDto>>, ApiError> {
|
||||||
let rows = sqlx::query(
|
let rows = sqlx::query(
|
||||||
"SELECT id, library_id, series_name, expected_volumes, qb_hash, content_path, \
|
"SELECT id, library_id, series_name, expected_volumes, qb_hash, content_path, \
|
||||||
status, imported_files, error_message, created_at, updated_at \
|
status, imported_files, error_message, progress, download_speed, eta, created_at, updated_at \
|
||||||
FROM torrent_downloads ORDER BY created_at DESC LIMIT 100",
|
FROM torrent_downloads ORDER BY created_at DESC LIMIT 100",
|
||||||
)
|
)
|
||||||
.fetch_all(&state.pool)
|
.fetch_all(&state.pool)
|
||||||
@@ -126,6 +129,9 @@ pub async fn list_torrent_downloads(
|
|||||||
status: row.get("status"),
|
status: row.get("status"),
|
||||||
imported_files: row.get("imported_files"),
|
imported_files: row.get("imported_files"),
|
||||||
error_message: row.get("error_message"),
|
error_message: row.get("error_message"),
|
||||||
|
progress: row.get("progress"),
|
||||||
|
download_speed: row.get("download_speed"),
|
||||||
|
eta: row.get("eta"),
|
||||||
created_at: created_at.to_rfc3339(),
|
created_at: created_at.to_rfc3339(),
|
||||||
updated_at: updated_at.to_rfc3339(),
|
updated_at: updated_at.to_rfc3339(),
|
||||||
}
|
}
|
||||||
@@ -135,6 +141,55 @@ pub async fn list_torrent_downloads(
|
|||||||
Ok(Json(dtos))
|
Ok(Json(dtos))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Delete a torrent download entry. If the torrent is still downloading, also remove it from qBittorrent.
|
||||||
|
pub async fn delete_torrent_download(
|
||||||
|
State(state): State<AppState>,
|
||||||
|
Path(id): Path<Uuid>,
|
||||||
|
) -> Result<Json<serde_json::Value>, ApiError> {
|
||||||
|
let row = sqlx::query("SELECT qb_hash, status FROM torrent_downloads WHERE id = $1")
|
||||||
|
.bind(id)
|
||||||
|
.fetch_optional(&state.pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let Some(row) = row else {
|
||||||
|
return Err(ApiError::not_found("torrent download not found"));
|
||||||
|
};
|
||||||
|
|
||||||
|
let qb_hash: Option<String> = row.get("qb_hash");
|
||||||
|
let status: String = row.get("status");
|
||||||
|
|
||||||
|
// If downloading, try to cancel in qBittorrent
|
||||||
|
if status == "downloading" {
|
||||||
|
if let Some(ref hash) = qb_hash {
|
||||||
|
if let Ok((base_url, username, password)) = load_qbittorrent_config(&state.pool).await {
|
||||||
|
let client = reqwest::Client::builder()
|
||||||
|
.timeout(Duration::from_secs(10))
|
||||||
|
.build()
|
||||||
|
.ok();
|
||||||
|
if let Some(client) = client {
|
||||||
|
if let Ok(sid) = qbittorrent_login(&client, &base_url, &username, &password).await {
|
||||||
|
let _ = client
|
||||||
|
.post(format!("{base_url}/api/v2/torrents/delete"))
|
||||||
|
.header("Cookie", format!("SID={sid}"))
|
||||||
|
.form(&[("hashes", hash.as_str()), ("deleteFiles", "true")])
|
||||||
|
.send()
|
||||||
|
.await;
|
||||||
|
info!("Deleted torrent {} from qBittorrent", hash);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sqlx::query("DELETE FROM torrent_downloads WHERE id = $1")
|
||||||
|
.bind(id)
|
||||||
|
.execute(&state.pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
info!("Deleted torrent download {id}");
|
||||||
|
Ok(Json(serde_json::json!({ "ok": true })))
|
||||||
|
}
|
||||||
|
|
||||||
// ─── Background poller ────────────────────────────────────────────────────────
|
// ─── Background poller ────────────────────────────────────────────────────────
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
@@ -144,6 +199,12 @@ struct QbTorrentInfo {
|
|||||||
content_path: Option<String>,
|
content_path: Option<String>,
|
||||||
save_path: Option<String>,
|
save_path: Option<String>,
|
||||||
name: Option<String>,
|
name: Option<String>,
|
||||||
|
#[serde(default)]
|
||||||
|
progress: f64,
|
||||||
|
#[serde(default)]
|
||||||
|
dlspeed: i64,
|
||||||
|
#[serde(default)]
|
||||||
|
eta: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Completed states in qBittorrent: torrent is fully downloaded and seeding.
|
/// Completed states in qBittorrent: torrent is fully downloaded and seeding.
|
||||||
@@ -152,29 +213,35 @@ const QB_COMPLETED_STATES: &[&str] = &[
|
|||||||
];
|
];
|
||||||
|
|
||||||
pub async fn run_torrent_poller(pool: PgPool, interval_seconds: u64) {
|
pub async fn run_torrent_poller(pool: PgPool, interval_seconds: u64) {
|
||||||
let wait = Duration::from_secs(interval_seconds.max(5));
|
let idle_wait = Duration::from_secs(interval_seconds.max(5));
|
||||||
|
let active_wait = Duration::from_secs(2);
|
||||||
loop {
|
loop {
|
||||||
if let Err(e) = poll_qbittorrent_downloads(&pool).await {
|
let has_active = match poll_qbittorrent_downloads(&pool).await {
|
||||||
|
Ok(active) => active,
|
||||||
|
Err(e) => {
|
||||||
warn!("[TORRENT_POLLER] {:#}", e);
|
warn!("[TORRENT_POLLER] {:#}", e);
|
||||||
|
false
|
||||||
}
|
}
|
||||||
tokio::time::sleep(wait).await;
|
};
|
||||||
|
tokio::time::sleep(if has_active { active_wait } else { idle_wait }).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn poll_qbittorrent_downloads(pool: &PgPool) -> anyhow::Result<()> {
|
/// Returns Ok(true) if there are active downloads, Ok(false) otherwise.
|
||||||
|
async fn poll_qbittorrent_downloads(pool: &PgPool) -> anyhow::Result<bool> {
|
||||||
if !is_torrent_import_enabled(pool).await {
|
if !is_torrent_import_enabled(pool).await {
|
||||||
return Ok(());
|
return Ok(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
let rows = sqlx::query(
|
let rows = sqlx::query(
|
||||||
"SELECT id, qb_hash FROM torrent_downloads WHERE status = 'downloading' AND qb_hash IS NOT NULL",
|
"SELECT id, qb_hash FROM torrent_downloads WHERE status = 'downloading'",
|
||||||
)
|
)
|
||||||
.fetch_all(pool)
|
.fetch_all(pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
if rows.is_empty() {
|
if rows.is_empty() {
|
||||||
trace!("[TORRENT_POLLER] No active downloads to poll");
|
trace!("[TORRENT_POLLER] No active downloads to poll");
|
||||||
return Ok(());
|
return Ok(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
let (base_url, username, password) = load_qbittorrent_config(pool)
|
let (base_url, username, password) = load_qbittorrent_config(pool)
|
||||||
@@ -189,6 +256,16 @@ async fn poll_qbittorrent_downloads(pool: &PgPool) -> anyhow::Result<()> {
|
|||||||
.await
|
.await
|
||||||
.map_err(|e| anyhow::anyhow!("qBittorrent login: {}", e.message))?;
|
.map_err(|e| anyhow::anyhow!("qBittorrent login: {}", e.message))?;
|
||||||
|
|
||||||
|
// Filter to rows that have a resolved hash
|
||||||
|
let rows: Vec<_> = rows.into_iter().filter(|r| {
|
||||||
|
let qb_hash: Option<String> = r.get("qb_hash");
|
||||||
|
qb_hash.is_some()
|
||||||
|
}).collect();
|
||||||
|
|
||||||
|
if rows.is_empty() {
|
||||||
|
return Ok(true);
|
||||||
|
}
|
||||||
|
|
||||||
let hashes: Vec<String> = rows
|
let hashes: Vec<String> = rows
|
||||||
.iter()
|
.iter()
|
||||||
.map(|r| { let h: String = r.get("qb_hash"); h })
|
.map(|r| { let h: String = r.get("qb_hash"); h })
|
||||||
@@ -209,6 +286,25 @@ async fn poll_qbittorrent_downloads(pool: &PgPool) -> anyhow::Result<()> {
|
|||||||
let infos: Vec<QbTorrentInfo> = resp.json().await?;
|
let infos: Vec<QbTorrentInfo> = resp.json().await?;
|
||||||
|
|
||||||
for info in &infos {
|
for info in &infos {
|
||||||
|
// Update progress for all active torrents
|
||||||
|
let row = rows.iter().find(|r| {
|
||||||
|
let h: String = r.get("qb_hash");
|
||||||
|
h == info.hash
|
||||||
|
});
|
||||||
|
if let Some(row) = row {
|
||||||
|
let tid: Uuid = row.get("id");
|
||||||
|
let _ = sqlx::query(
|
||||||
|
"UPDATE torrent_downloads SET progress = $1, download_speed = $2, eta = $3, updated_at = NOW() \
|
||||||
|
WHERE id = $4 AND status = 'downloading'",
|
||||||
|
)
|
||||||
|
.bind(info.progress as f32)
|
||||||
|
.bind(info.dlspeed)
|
||||||
|
.bind(info.eta)
|
||||||
|
.bind(tid)
|
||||||
|
.execute(pool)
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
|
||||||
if !QB_COMPLETED_STATES.contains(&info.state.as_str()) {
|
if !QB_COMPLETED_STATES.contains(&info.state.as_str()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -228,15 +324,15 @@ async fn poll_qbittorrent_downloads(pool: &PgPool) -> anyhow::Result<()> {
|
|||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
|
|
||||||
let row = rows.iter().find(|r| {
|
let Some(row) = rows.iter().find(|r| {
|
||||||
let h: String = r.get("qb_hash");
|
let h: String = r.get("qb_hash");
|
||||||
h == info.hash
|
h == info.hash
|
||||||
});
|
}) else { continue; };
|
||||||
let Some(row) = row else { continue; };
|
|
||||||
let torrent_id: Uuid = row.get("id");
|
let torrent_id: Uuid = row.get("id");
|
||||||
|
|
||||||
let updated = sqlx::query(
|
let updated = sqlx::query(
|
||||||
"UPDATE torrent_downloads SET status = 'completed', content_path = $1, updated_at = NOW() \
|
"UPDATE torrent_downloads SET status = 'completed', content_path = $1, progress = 1, \
|
||||||
|
download_speed = 0, eta = 0, updated_at = NOW() \
|
||||||
WHERE id = $2 AND status = 'downloading'",
|
WHERE id = $2 AND status = 'downloading'",
|
||||||
)
|
)
|
||||||
.bind(&content_path)
|
.bind(&content_path)
|
||||||
@@ -255,7 +351,15 @@ async fn poll_qbittorrent_downloads(pool: &PgPool) -> anyhow::Result<()> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
// Still active if any rows remain in 'downloading' status
|
||||||
|
let still_active = sqlx::query_scalar::<_, i64>(
|
||||||
|
"SELECT COUNT(*) FROM torrent_downloads WHERE status = 'downloading'",
|
||||||
|
)
|
||||||
|
.fetch_one(pool)
|
||||||
|
.await
|
||||||
|
.unwrap_or(0);
|
||||||
|
|
||||||
|
Ok(still_active > 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ─── Import processing ────────────────────────────────────────────────────────
|
// ─── Import processing ────────────────────────────────────────────────────────
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useState, useEffect, useCallback } from "react";
|
import { useState, useEffect, useCallback } from "react";
|
||||||
|
import { createPortal } from "react-dom";
|
||||||
import { TorrentDownloadDto } from "@/lib/api";
|
import { TorrentDownloadDto } from "@/lib/api";
|
||||||
import { Card, CardContent, Button, Icon } from "@/app/components/ui";
|
import { Card, CardContent, Button, Icon } from "@/app/components/ui";
|
||||||
import { useTranslation } from "@/lib/i18n/context";
|
import { useTranslation } from "@/lib/i18n/context";
|
||||||
@@ -43,6 +44,22 @@ function formatDate(iso: string): string {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function formatSpeed(bytesPerSec: number): string {
|
||||||
|
if (bytesPerSec < 1024) return `${bytesPerSec} B/s`;
|
||||||
|
if (bytesPerSec < 1024 * 1024) return `${(bytesPerSec / 1024).toFixed(1)} KB/s`;
|
||||||
|
return `${(bytesPerSec / 1024 / 1024).toFixed(1)} MB/s`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatEta(seconds: number): string {
|
||||||
|
if (seconds <= 0 || seconds >= 8640000) return "";
|
||||||
|
const h = Math.floor(seconds / 3600);
|
||||||
|
const m = Math.floor((seconds % 3600) / 60);
|
||||||
|
const s = seconds % 60;
|
||||||
|
if (h > 0) return `${h}h${String(m).padStart(2, "0")}m`;
|
||||||
|
if (m > 0) return `${m}m${String(s).padStart(2, "0")}s`;
|
||||||
|
return `${s}s`;
|
||||||
|
}
|
||||||
|
|
||||||
interface DownloadsPageProps {
|
interface DownloadsPageProps {
|
||||||
initialDownloads: TorrentDownloadDto[];
|
initialDownloads: TorrentDownloadDto[];
|
||||||
}
|
}
|
||||||
@@ -67,7 +84,7 @@ export function DownloadsPage({ initialDownloads }: DownloadsPageProps) {
|
|||||||
const hasActive = downloads.some(d => STATUS_ACTIVE.has(d.status));
|
const hasActive = downloads.some(d => STATUS_ACTIVE.has(d.status));
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!hasActive) return;
|
if (!hasActive) return;
|
||||||
const id = setInterval(() => refresh(false), 5000);
|
const id = setInterval(() => refresh(false), 2000);
|
||||||
return () => clearInterval(id);
|
return () => clearInterval(id);
|
||||||
}, [hasActive, refresh]);
|
}, [hasActive, refresh]);
|
||||||
|
|
||||||
@@ -133,7 +150,7 @@ export function DownloadsPage({ initialDownloads }: DownloadsPageProps) {
|
|||||||
) : (
|
) : (
|
||||||
<div className="space-y-3">
|
<div className="space-y-3">
|
||||||
{visible.map(dl => (
|
{visible.map(dl => (
|
||||||
<DownloadCard key={dl.id} dl={dl} />
|
<DownloadCard key={dl.id} dl={dl} onDeleted={() => refresh(false)} />
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
@@ -141,10 +158,23 @@ export function DownloadsPage({ initialDownloads }: DownloadsPageProps) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function DownloadCard({ dl }: { dl: TorrentDownloadDto }) {
|
function DownloadCard({ dl, onDeleted }: { dl: TorrentDownloadDto; onDeleted: () => void }) {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
const [deleting, setDeleting] = useState(false);
|
||||||
|
const [showConfirm, setShowConfirm] = useState(false);
|
||||||
const importedCount = Array.isArray(dl.imported_files) ? dl.imported_files.length : 0;
|
const importedCount = Array.isArray(dl.imported_files) ? dl.imported_files.length : 0;
|
||||||
|
|
||||||
|
async function handleDelete() {
|
||||||
|
setDeleting(true);
|
||||||
|
setShowConfirm(false);
|
||||||
|
try {
|
||||||
|
const resp = await fetch(`/api/torrent-downloads/${dl.id}`, { method: "DELETE" });
|
||||||
|
if (resp.ok) onDeleted();
|
||||||
|
} finally {
|
||||||
|
setDeleting(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Card>
|
<Card>
|
||||||
<CardContent className="pt-4">
|
<CardContent className="pt-4">
|
||||||
@@ -187,6 +217,28 @@ function DownloadCard({ dl }: { dl: TorrentDownloadDto }) {
|
|||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{dl.status === "downloading" && (
|
||||||
|
<div className="mt-2">
|
||||||
|
<div className="flex items-center gap-2 mb-1">
|
||||||
|
<div className="flex-1 h-2 bg-muted rounded-full overflow-hidden">
|
||||||
|
<div
|
||||||
|
className="h-full bg-primary rounded-full transition-all duration-500"
|
||||||
|
style={{ width: `${Math.round(dl.progress * 100)}%` }}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<span className="text-xs font-medium text-foreground tabular-nums w-10 text-right">
|
||||||
|
{Math.round(dl.progress * 100)}%
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
{(dl.download_speed > 0 || dl.eta > 0) && (
|
||||||
|
<div className="flex items-center gap-3 text-xs text-muted-foreground">
|
||||||
|
{dl.download_speed > 0 && <span>{formatSpeed(dl.download_speed)}</span>}
|
||||||
|
{dl.eta > 0 && dl.eta < 8640000 && <span>ETA {formatEta(dl.eta)}</span>}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
{dl.content_path && dl.status !== "imported" && (
|
{dl.content_path && dl.status !== "imported" && (
|
||||||
<p className="mt-1 text-xs font-mono text-muted-foreground truncate" title={dl.content_path}>
|
<p className="mt-1 text-xs font-mono text-muted-foreground truncate" title={dl.content_path}>
|
||||||
{dl.content_path}
|
{dl.content_path}
|
||||||
@@ -208,15 +260,57 @@ function DownloadCard({ dl }: { dl: TorrentDownloadDto }) {
|
|||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Timestamp */}
|
{/* Actions */}
|
||||||
<div className="text-xs text-muted-foreground shrink-0 text-right">
|
<div className="flex flex-col items-end gap-2 shrink-0">
|
||||||
|
<div className="text-xs text-muted-foreground text-right">
|
||||||
<p>{formatDate(dl.created_at)}</p>
|
<p>{formatDate(dl.created_at)}</p>
|
||||||
{dl.updated_at !== dl.created_at && (
|
{dl.updated_at !== dl.created_at && (
|
||||||
<p className="opacity-60">maj {formatDate(dl.updated_at)}</p>
|
<p className="opacity-60">maj {formatDate(dl.updated_at)}</p>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => setShowConfirm(true)}
|
||||||
|
disabled={deleting || dl.status === "importing"}
|
||||||
|
className="inline-flex items-center justify-center w-7 h-7 rounded-md text-muted-foreground hover:text-destructive hover:bg-destructive/10 transition-colors disabled:opacity-30"
|
||||||
|
title={dl.status === "downloading" ? t("downloads.cancel") : t("downloads.delete")}
|
||||||
|
>
|
||||||
|
{deleting ? (
|
||||||
|
<Icon name="spinner" size="sm" className="animate-spin" />
|
||||||
|
) : (
|
||||||
|
<Icon name="trash" size="sm" />
|
||||||
|
)}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</CardContent>
|
</CardContent>
|
||||||
|
|
||||||
|
{showConfirm && createPortal(
|
||||||
|
<>
|
||||||
|
<div className="fixed inset-0 bg-black/30 backdrop-blur-sm z-50" onClick={() => setShowConfirm(false)} />
|
||||||
|
<div className="fixed inset-0 flex items-center justify-center z-50 p-4">
|
||||||
|
<div className="bg-card border border-border/50 rounded-xl shadow-2xl w-full max-w-sm overflow-hidden animate-in fade-in zoom-in-95 duration-200">
|
||||||
|
<div className="p-6">
|
||||||
|
<h3 className="text-lg font-semibold text-foreground mb-2">
|
||||||
|
{dl.status === "downloading" ? t("downloads.cancel") : t("downloads.delete")}
|
||||||
|
</h3>
|
||||||
|
<p className="text-sm text-muted-foreground">
|
||||||
|
{dl.status === "downloading" ? t("downloads.confirmCancel") : t("downloads.confirmDelete")}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div className="flex justify-end gap-2 px-6 pb-6">
|
||||||
|
<Button variant="outline" size="sm" onClick={() => setShowConfirm(false)}>
|
||||||
|
{t("common.cancel")}
|
||||||
|
</Button>
|
||||||
|
<Button variant="destructive" size="sm" onClick={handleDelete}>
|
||||||
|
{dl.status === "downloading" ? t("downloads.cancel") : t("downloads.delete")}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</>,
|
||||||
|
document.body
|
||||||
|
)}
|
||||||
</Card>
|
</Card>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -106,7 +106,13 @@ export function DownloadDetectionResultsCard({ results, libraryId, t }: {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{release.download_url && (
|
{release.download_url && (
|
||||||
<QbittorrentDownloadButton downloadUrl={release.download_url} releaseId={`${r.id}-${idx}`} />
|
<QbittorrentDownloadButton
|
||||||
|
downloadUrl={release.download_url}
|
||||||
|
releaseId={`${r.id}-${idx}`}
|
||||||
|
libraryId={libraryId ?? undefined}
|
||||||
|
seriesName={r.series_name}
|
||||||
|
expectedVolumes={release.matched_missing_volumes}
|
||||||
|
/>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
|
|||||||
13
apps/backoffice/app/api/torrent-downloads/[id]/route.ts
Normal file
13
apps/backoffice/app/api/torrent-downloads/[id]/route.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import { NextResponse, NextRequest } from "next/server";
|
||||||
|
import { apiFetch } from "@/lib/api";
|
||||||
|
|
||||||
|
export async function DELETE(_request: NextRequest, { params }: { params: Promise<{ id: string }> }) {
|
||||||
|
try {
|
||||||
|
const { id } = await params;
|
||||||
|
const data = await apiFetch(`/torrent-downloads/${id}`, { method: "DELETE" });
|
||||||
|
return NextResponse.json(data);
|
||||||
|
} catch (error) {
|
||||||
|
const message = error instanceof Error ? error.message : "Failed to delete torrent download";
|
||||||
|
return NextResponse.json({ error: message }, { status: 500 });
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -21,7 +21,19 @@ export function QbittorrentProvider({ children }: { children: ReactNode }) {
|
|||||||
return <QbConfigContext.Provider value={configured}>{children}</QbConfigContext.Provider>;
|
return <QbConfigContext.Provider value={configured}>{children}</QbConfigContext.Provider>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function QbittorrentDownloadButton({ downloadUrl, releaseId }: { downloadUrl: string; releaseId: string }) {
|
export function QbittorrentDownloadButton({
|
||||||
|
downloadUrl,
|
||||||
|
releaseId,
|
||||||
|
libraryId,
|
||||||
|
seriesName,
|
||||||
|
expectedVolumes,
|
||||||
|
}: {
|
||||||
|
downloadUrl: string;
|
||||||
|
releaseId: string;
|
||||||
|
libraryId?: string;
|
||||||
|
seriesName?: string;
|
||||||
|
expectedVolumes?: number[];
|
||||||
|
}) {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const configured = useContext(QbConfigContext);
|
const configured = useContext(QbConfigContext);
|
||||||
const [sending, setSending] = useState(false);
|
const [sending, setSending] = useState(false);
|
||||||
@@ -37,7 +49,12 @@ export function QbittorrentDownloadButton({ downloadUrl, releaseId }: { download
|
|||||||
const resp = await fetch("/api/qbittorrent/add", {
|
const resp = await fetch("/api/qbittorrent/add", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
body: JSON.stringify({ url: downloadUrl }),
|
body: JSON.stringify({
|
||||||
|
url: downloadUrl,
|
||||||
|
...(libraryId && { library_id: libraryId }),
|
||||||
|
...(seriesName && { series_name: seriesName }),
|
||||||
|
...(expectedVolumes && { expected_volumes: expectedVolumes }),
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
const data = await resp.json();
|
const data = await resp.json();
|
||||||
if (data.error) {
|
if (data.error) {
|
||||||
|
|||||||
@@ -1295,6 +1295,9 @@ export type TorrentDownloadDto = {
|
|||||||
status: "downloading" | "completed" | "importing" | "imported" | "error";
|
status: "downloading" | "completed" | "importing" | "imported" | "error";
|
||||||
imported_files: Array<{ volume: number; source: string; destination: string }> | null;
|
imported_files: Array<{ volume: number; source: string; destination: string }> | null;
|
||||||
error_message: string | null;
|
error_message: string | null;
|
||||||
|
progress: number;
|
||||||
|
download_speed: number;
|
||||||
|
eta: number;
|
||||||
created_at: string;
|
created_at: string;
|
||||||
updated_at: string;
|
updated_at: string;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -898,6 +898,10 @@ const en: Record<TranslationKey, string> = {
|
|||||||
"downloads.status.importing": "Importing",
|
"downloads.status.importing": "Importing",
|
||||||
"downloads.status.imported": "Imported",
|
"downloads.status.imported": "Imported",
|
||||||
"downloads.status.error": "Error",
|
"downloads.status.error": "Error",
|
||||||
|
"downloads.delete": "Delete",
|
||||||
|
"downloads.cancel": "Cancel download",
|
||||||
|
"downloads.confirmDelete": "Delete this download?",
|
||||||
|
"downloads.confirmCancel": "Cancel this download? The torrent will also be removed from qBittorrent.",
|
||||||
|
|
||||||
// Settings - Torrent Import
|
// Settings - Torrent Import
|
||||||
"settings.torrentImport": "Auto import",
|
"settings.torrentImport": "Auto import",
|
||||||
|
|||||||
@@ -896,6 +896,10 @@ const fr = {
|
|||||||
"downloads.status.importing": "Import en cours",
|
"downloads.status.importing": "Import en cours",
|
||||||
"downloads.status.imported": "Importé",
|
"downloads.status.imported": "Importé",
|
||||||
"downloads.status.error": "Erreur",
|
"downloads.status.error": "Erreur",
|
||||||
|
"downloads.delete": "Supprimer",
|
||||||
|
"downloads.cancel": "Annuler le téléchargement",
|
||||||
|
"downloads.confirmDelete": "Supprimer ce téléchargement ?",
|
||||||
|
"downloads.confirmCancel": "Annuler ce téléchargement ? Le torrent sera aussi supprimé de qBittorrent.",
|
||||||
|
|
||||||
// Settings - Torrent Import
|
// Settings - Torrent Import
|
||||||
"settings.torrentImport": "Import automatique",
|
"settings.torrentImport": "Import automatique",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "stripstream-backoffice",
|
"name": "stripstream-backoffice",
|
||||||
"version": "2.12.0",
|
"version": "2.12.1",
|
||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "next dev -p 7082",
|
"dev": "next dev -p 7082",
|
||||||
|
|||||||
4
infra/migrations/0065_add_torrent_progress.sql
Normal file
4
infra/migrations/0065_add_torrent_progress.sql
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
ALTER TABLE torrent_downloads
|
||||||
|
ADD COLUMN progress REAL NOT NULL DEFAULT 0,
|
||||||
|
ADD COLUMN download_speed BIGINT NOT NULL DEFAULT 0,
|
||||||
|
ADD COLUMN eta BIGINT NOT NULL DEFAULT 0;
|
||||||
Reference in New Issue
Block a user