|
| 1 | +use crate::settings::{PutItem, RsyncTrack, Status}; |
| 2 | +use rusqlite::{params, Connection}; |
| 3 | +use std::collections::HashMap; |
| 4 | + |
| 5 | +/// Opens (or creates) the SQLite database and ensures the schema exists. |
| 6 | +pub fn open() -> Connection { |
| 7 | + let conn = Connection::open("rutorrent.db").expect("Failed to open database"); |
| 8 | + conn.execute_batch( |
| 9 | + "CREATE TABLE IF NOT EXISTS state ( |
| 10 | + hash TEXT PRIMARY KEY, |
| 11 | + name TEXT NOT NULL, |
| 12 | + status TEXT NOT NULL, |
| 13 | + progress REAL NOT NULL DEFAULT 0.0, |
| 14 | + url TEXT NOT NULL, |
| 15 | + save_path TEXT NOT NULL, |
| 16 | + remote_host TEXT NOT NULL, |
| 17 | + remote_user TEXT NOT NULL, |
| 18 | + remote_path TEXT NOT NULL, |
| 19 | + delete_after_copy INTEGER NOT NULL DEFAULT 0 |
| 20 | + );", |
| 21 | + ) |
| 22 | + .expect("Failed to create schema"); |
| 23 | + conn |
| 24 | +} |
| 25 | + |
| 26 | +/// Inserts or replaces a tracked torrent entry. |
| 27 | +pub fn upsert(conn: &Connection, hash: &str, entry: &RsyncTrack) { |
| 28 | + let (status, progress) = encode_status(&entry.status); |
| 29 | + conn.execute( |
| 30 | + "INSERT OR REPLACE INTO state |
| 31 | + (hash, name, status, progress, url, save_path, remote_host, remote_user, remote_path, delete_after_copy) |
| 32 | + VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10)", |
| 33 | + params![ |
| 34 | + hash, |
| 35 | + entry.name, |
| 36 | + status, |
| 37 | + progress, |
| 38 | + entry.put_item.url, |
| 39 | + entry.put_item.save_path, |
| 40 | + entry.put_item.remote_host, |
| 41 | + entry.put_item.remote_username, |
| 42 | + entry.put_item.remote_path, |
| 43 | + entry.put_item.delete_after_copy as i32, |
| 44 | + ], |
| 45 | + ) |
| 46 | + .expect("Failed to upsert state"); |
| 47 | +} |
| 48 | + |
| 49 | +/// Removes a torrent entry by hash. |
| 50 | +pub fn remove(conn: &Connection, hash: &str) { |
| 51 | + conn.execute("DELETE FROM state WHERE hash = ?1", params![hash]) |
| 52 | + .expect("Failed to remove state"); |
| 53 | +} |
| 54 | + |
| 55 | +/// Loads all persisted entries back into a HashMap on startup. |
| 56 | +pub fn load_all(conn: &Connection) -> HashMap<String, RsyncTrack> { |
| 57 | + let mut stmt = conn |
| 58 | + .prepare("SELECT hash, name, status, progress, url, save_path, remote_host, remote_user, remote_path, delete_after_copy FROM state") |
| 59 | + .expect("Failed to prepare load query"); |
| 60 | + |
| 61 | + stmt.query_map([], |row| { |
| 62 | + let hash: String = row.get(0)?; |
| 63 | + let name: String = row.get(1)?; |
| 64 | + let status_str: String = row.get(2)?; |
| 65 | + let progress: f64 = row.get(3)?; |
| 66 | + let url: String = row.get(4)?; |
| 67 | + let save_path: String = row.get(5)?; |
| 68 | + let remote_host: String = row.get(6)?; |
| 69 | + let remote_username: String = row.get(7)?; |
| 70 | + let remote_path: String = row.get(8)?; |
| 71 | + let delete_after_copy: i32 = row.get(9)?; |
| 72 | + |
| 73 | + let status = decode_status(&status_str, progress); |
| 74 | + let put_item = PutItem { |
| 75 | + url, |
| 76 | + name: None, |
| 77 | + hash: None, |
| 78 | + trackers: None, |
| 79 | + save_path, |
| 80 | + remote_host, |
| 81 | + remote_username, |
| 82 | + remote_path, |
| 83 | + delete_after_copy: delete_after_copy != 0, |
| 84 | + }; |
| 85 | + |
| 86 | + Ok((hash, RsyncTrack { name, status, put_item })) |
| 87 | + }) |
| 88 | + .expect("Failed to query state") |
| 89 | + .filter_map(|r| r.ok()) |
| 90 | + .collect() |
| 91 | +} |
| 92 | + |
| 93 | +fn encode_status(status: &Status) -> (&'static str, f64) { |
| 94 | + match status { |
| 95 | + Status::Downloading(p) => ("Downloading", *p), |
| 96 | + Status::Copying(p) => ("Copying", *p), |
| 97 | + Status::Completed => ("Completed", 1.0), |
| 98 | + Status::Failed => ("Failed", 0.0), |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +fn decode_status(s: &str, progress: f64) -> Status { |
| 103 | + match s { |
| 104 | + "Copying" => Status::Copying(progress), |
| 105 | + "Completed" => Status::Completed, |
| 106 | + "Failed" => Status::Failed, |
| 107 | + _ => Status::Downloading(progress), |
| 108 | + } |
| 109 | +} |
0 commit comments