|
| 1 | +use reqwest::Client; |
| 2 | +use serde_json::Value; |
| 3 | +use std::path::Path; |
| 4 | + |
| 5 | +use crate::{settings, squire}; |
| 6 | + |
| 7 | +/// Constructs a default "Downloads" directory in the HOME folder. |
| 8 | +/// |
| 9 | +/// # Arguments |
| 10 | +/// |
| 11 | +/// * `child_dir` - Child directory that has to be appended to the default "Downloads" folder. |
| 12 | +/// |
| 13 | +/// # Returns |
| 14 | +/// |
| 15 | +/// Returns the constructed "Downloads" directory. |
| 16 | +fn default_download_path(child_dir: &str) -> String { |
| 17 | + match dirs::home_dir() { |
| 18 | + Some(home) => { |
| 19 | + let path = home.join("Downloads").join(child_dir); |
| 20 | + match path.to_str() { |
| 21 | + Some(path_str) => path_str.to_string(), |
| 22 | + None => { |
| 23 | + log::warn!("Downloads path contains invalid UTF-8, falling back to /tmp"); |
| 24 | + format!("/tmp/{}", child_dir) |
| 25 | + } |
| 26 | + } |
| 27 | + } |
| 28 | + None => { |
| 29 | + log::info!("Could not determine HOME directory, falling back to /tmp"); |
| 30 | + format!("/tmp/{}", child_dir) |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +/// Fetches the default save path configured in qBittorrent. |
| 36 | +/// |
| 37 | +/// # Arguments |
| 38 | +/// |
| 39 | +/// * `client` - Authenticated HTTP client for qBittorrent API requests. |
| 40 | +/// * `config` - Application configuration containing the qBittorrent URL. |
| 41 | +/// |
| 42 | +/// # Returns |
| 43 | +/// |
| 44 | +/// Returns the default save path as a `String`, or a fallback path if the |
| 45 | +/// request fails or the field is absent. |
| 46 | +pub async fn get_default_save_path( |
| 47 | + client: &Client, |
| 48 | + config: &settings::Config, |
| 49 | + child_dir: &String, |
| 50 | +) -> String { |
| 51 | + // 1. Check environment variable override |
| 52 | + let default_save_env = squire::get_env_var("save_path", None); |
| 53 | + if !default_save_env.is_empty() { |
| 54 | + match std::fs::create_dir_all(&default_save_env) { |
| 55 | + Ok(_) => { |
| 56 | + log::debug!( |
| 57 | + "Verified save_path environment directory exists: {}", |
| 58 | + default_save_env |
| 59 | + ); |
| 60 | + } |
| 61 | + Err(err) => { |
| 62 | + log::warn!( |
| 63 | + "Failed to create save_path (from env) directory '{}': {}", |
| 64 | + default_save_env, |
| 65 | + err |
| 66 | + ); |
| 67 | + } |
| 68 | + } |
| 69 | + let joined = Path::new(&default_save_env).join(child_dir); |
| 70 | + return joined.to_string_lossy().into_owned(); |
| 71 | + } |
| 72 | + |
| 73 | + // 2. Fetch qBittorrent preferences |
| 74 | + let response = match client |
| 75 | + .get(format!("{}/api/v2/app/preferences", config.qbit_url)) |
| 76 | + .send() |
| 77 | + .await |
| 78 | + { |
| 79 | + Ok(resp) => resp, |
| 80 | + Err(err) => { |
| 81 | + log::error!("Failed to fetch qBittorrent preferences: {}", err); |
| 82 | + return default_download_path(child_dir); |
| 83 | + } |
| 84 | + }; |
| 85 | + |
| 86 | + // 3. Parse JSON response |
| 87 | + let resp_json: Value = match response.json().await { |
| 88 | + Ok(json) => json, |
| 89 | + Err(err) => { |
| 90 | + log::error!("Failed to parse qBittorrent preferences JSON: {}", err); |
| 91 | + return default_download_path(child_dir); |
| 92 | + } |
| 93 | + }; |
| 94 | + |
| 95 | + // 4. Extract save_path |
| 96 | + match resp_json["save_path"].as_str() { |
| 97 | + Some(path) if !path.is_empty() => { |
| 98 | + log::info!("Using qBittorrent save path: {}", path); |
| 99 | + Path::new(path) |
| 100 | + .join(child_dir) |
| 101 | + .to_string_lossy() |
| 102 | + .into_owned() |
| 103 | + } |
| 104 | + Some(_) => { |
| 105 | + log::info!("qBittorrent save_path is empty, using fallback"); |
| 106 | + default_download_path(child_dir) |
| 107 | + } |
| 108 | + None => { |
| 109 | + log::info!("qBittorrent preferences missing save_path, using fallback"); |
| 110 | + default_download_path(child_dir) |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments