From 87b2bbac0a2e74408e607f88b10e2f94f398ed9a Mon Sep 17 00:00:00 2001 From: Chikage Date: Fri, 13 Oct 2023 16:10:56 +0900 Subject: [PATCH] auto download and run qbittorrent in windows --- plugins/qbittorrent/src/lib.rs | 2 +- plugins/qbittorrent/src/qbit/binary.rs | 47 +++++++++++++++++++++++--- plugins/webhook/src/setting.rs | 8 ++--- plugins/yande_popular/src/lib.rs | 8 ++--- plugins/yande_popular/src/setting.rs | 6 ++-- 5 files changed, 54 insertions(+), 17 deletions(-) diff --git a/plugins/qbittorrent/src/lib.rs b/plugins/qbittorrent/src/lib.rs index d90116c..8cea2c2 100644 --- a/plugins/qbittorrent/src/lib.rs +++ b/plugins/qbittorrent/src/lib.rs @@ -10,7 +10,7 @@ pub async fn run(client: Client, plugin_folder: impl AsRef) -> let setting = setting::get_or_init(plugin_folder)?; - if setting.use_internal_qbit {} + Ok(()) } diff --git a/plugins/qbittorrent/src/qbit/binary.rs b/plugins/qbittorrent/src/qbit/binary.rs index 3e2a0c8..9a074b0 100644 --- a/plugins/qbittorrent/src/qbit/binary.rs +++ b/plugins/qbittorrent/src/qbit/binary.rs @@ -1,10 +1,13 @@ use anyhow::Result; -use std::{path::Path, process::Child}; +use std::{ + path::Path, + process::{Child, Stdio}, +}; #[allow(dead_code)] fn get_download_link() -> Result { - if !(cfg!(target_os = "linux")) { + if cfg!(target_os = "linux") != true { return Err(anyhow::anyhow!("only support linux")); } @@ -21,10 +24,10 @@ fn get_download_link() -> Result { } Ok(link) } - +#[allow(dead_code)] fn download_binary(path: impl AsRef) -> Result<()> { let link = get_download_link()?; - let resp = reqwest::blocking::get(&link)?; + let resp = reqwest::blocking::get(link)?; let binary = resp.bytes()?; std::fs::create_dir_all(path.as_ref().parent().unwrap())?; std::fs::write(&path, binary)?; @@ -35,7 +38,7 @@ fn download_binary(path: impl AsRef) -> Result<()> { } Ok(()) } - +#[allow(dead_code)] pub fn run(runtime_folder: impl AsRef, port: u16) -> Result { let binary_path = runtime_folder.as_ref().join("qbittorrent-nox"); if !binary_path.exists() { @@ -73,8 +76,42 @@ pub fn run(runtime_folder: impl AsRef, port: u16) -> Result { "--profile={}", qbittorrent_folder.to_string_lossy() )); + cmd.stdout(Stdio::null()); let child = cmd.spawn()?; Ok(child) } + +#[cfg(test)] +mod test { + + use std::path::PathBuf; + + use super::*; + + #[test] + fn test_running() { + let runtime_folder = PathBuf::from("/tmp/qbit"); + let mut child = run(&runtime_folder, 8080).unwrap(); + std::thread::sleep(std::time::Duration::from_secs(10)); + + // is still running + assert!(child.try_wait().unwrap().is_none()); + let logs = runtime_folder + .join("config") + .join("qBittorrent") + .join("data") + .join("logs") + .join("qbittorrent.log"); + + if let Ok(logs) = std::fs::read_to_string(logs) { + assert!(logs.contains("Web UI: Now listening on IP")); + } else { + panic!("no logs"); + } + + child.kill().unwrap(); + std::fs::remove_dir_all("/tmp/qbit").unwrap(); + } +} diff --git a/plugins/webhook/src/setting.rs b/plugins/webhook/src/setting.rs index 82d1878..3473478 100644 --- a/plugins/webhook/src/setting.rs +++ b/plugins/webhook/src/setting.rs @@ -12,11 +12,11 @@ pub struct Setting { } impl Setting { - pub async fn to_hashmap(self, client: &Client) -> Result> { + pub async fn to_hashmap(&self, client: &Client) -> Result> { let mut hashmap = HashMap::new(); - for room_id in self.room_id { - let room = Room::new(&client, &room_id).await?; - hashmap.insert(room_id, room); + for room_id in &self.room_id { + let room = Room::new(client, room_id).await?; + hashmap.insert(room_id.clone(), room); } Ok(hashmap) } diff --git a/plugins/yande_popular/src/lib.rs b/plugins/yande_popular/src/lib.rs index cfc182c..e7819e8 100644 --- a/plugins/yande_popular/src/lib.rs +++ b/plugins/yande_popular/src/lib.rs @@ -38,7 +38,7 @@ pub async fn sync(setting_hashmap: &HashMap) -> Result< image_list.extend(list); } - let download_list = yande::get_download_list(&image_list, &db).await?; + let download_list = yande::get_download_list(&image_list, db).await?; for (id, img_data) in download_list { for (id, url) in img_data.url.iter() { @@ -47,7 +47,7 @@ pub async fn sync(setting_hashmap: &HashMap) -> Result< Ok(path) => path, Err(e) => { log::error!("download failed: {}", e); - return Err(e.into()); + return Err(e); } }; @@ -56,7 +56,7 @@ pub async fn sync(setting_hashmap: &HashMap) -> Result< Ok(path) => path, Err(e) => { log::error!("resize {id} failed: {}", e); - return Err(e.into()); + return Err(e); } } } else { @@ -72,7 +72,7 @@ pub async fn sync(setting_hashmap: &HashMap) -> Result< } Err(e) => { log::error!("upload failed: {}", e); - return Err(e.into()); + return Err(e); } } diff --git a/plugins/yande_popular/src/setting.rs b/plugins/yande_popular/src/setting.rs index 27d0d4a..5c38a53 100644 --- a/plugins/yande_popular/src/setting.rs +++ b/plugins/yande_popular/src/setting.rs @@ -23,14 +23,14 @@ pub struct Setting { } impl Setting { - pub async fn to_hashmap(self, client: &Client) -> Result> { + pub async fn to_hashmap(&self, client: &Client) -> Result> { let mut hashmap = HashMap::new(); - for setting in self.room { + for setting in &self.room { let db = DB::open(&setting.db_path); std::fs::create_dir_all(Path::new(&setting.tmp_path)).unwrap_or_else(|e| { log::error!("create tmp dir failed: {}", e); }); - let room = Room::new(&client, &setting.room_id).await?; + let room = Room::new(client, &setting.room_id).await?; hashmap.insert(setting.clone(), (db, room)); } Ok(hashmap)