diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index a9c602a..db732e6 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -18,7 +18,7 @@ jobs: steps: - uses: actions/checkout@v4 - name: Clippy - run: cargo clippy --verbose + run: cargo clippy --verbose --release test: runs-on: ubuntu-latest @@ -27,4 +27,4 @@ jobs: steps: - uses: actions/checkout@v4 - name: Run tests - run: cargo test --verbose + run: cargo test --verbose --release diff --git a/default.nix b/default.nix deleted file mode 100644 index f8a54f5..0000000 --- a/default.nix +++ /dev/null @@ -1,13 +0,0 @@ -{ pkgs ? import {} }: -pkgs.rustPlatform.buildRustPackage { - pname = "nyaa"; - version = - (builtins.fromTOML ( - builtins.readFile ./Cargo.toml - )) - .package - .version; - - cargoLock.lockFile = ./Cargo.lock; - src = pkgs.lib.cleanSource ./.; -} diff --git a/makefile b/makefile index fb37a26..4282bf2 100644 --- a/makefile +++ b/makefile @@ -16,7 +16,7 @@ win: cargo build --target $(WINDOWS_TARGET) --release linux: - cargo build --target $(LINUX_TARGET) --release + cargo build --target $(LINUX_TARGET) --profile=github fedora: @mkdir -p "release/$(VERSION)" diff --git a/shell.nix b/shell.nix deleted file mode 100644 index ba7a875..0000000 --- a/shell.nix +++ /dev/null @@ -1,7 +0,0 @@ -{ pkgs ? import {} }: -pkgs.mkShell { - inputsFrom = [(pkgs.callPackage ./default.nix {})]; - buildInputs = [ - pkgs.rustPackages.clippy - ]; -} diff --git a/src/client/qbit.rs b/src/client/qbit.rs index fc09547..16d07fa 100644 --- a/src/client/qbit.rs +++ b/src/client/qbit.rs @@ -1,9 +1,6 @@ use std::collections::HashMap; -use reqwest::{ - header::{COOKIE, REFERER, SET_COOKIE}, - Response, StatusCode, -}; +use reqwest::{header::REFERER, Response, StatusCode}; use serde::{Deserialize, Serialize}; use crate::{app::Context, source::Item, util::conv::add_protocol}; @@ -114,43 +111,28 @@ struct QbitForm { // rename: String // Disabled } -async fn login(qbit: &QbitConfig, client: &reqwest::Client) -> Result { +async fn login(qbit: &QbitConfig, client: &reqwest::Client) -> Result<(), String> { let base_url = add_protocol(qbit.base_url.clone(), false); let url = format!("{}/api/v2/auth/login", base_url); let mut params = HashMap::new(); params.insert("username", qbit.username.to_owned()); params.insert("password", qbit.password.to_owned()); let res = client.post(url).form(¶ms).send().await; - let res = res.map_err(|e| format!("Failed to send data to qBittorrent\n{}", e))?; - let headers = res.headers().clone(); - let cookie = headers.get(SET_COOKIE).ok_or(format!( - "Failed to get cookie from qBittorrent:\n{}", - res.text().await.unwrap_or_default() - ))?; - - let cookie = cookie - .to_str() - .map_err(|e| format!("Failed to parse cookie:\n{}", e))?; - cookie - .split(';') - .find(|c| c.split_once('=').is_some_and(|s| s.0 == "SID")) - .ok_or("No cookie returned by qBittorrent".to_owned()) - .map(|s| s.to_owned()) + let _ = res.map_err(|e| format!("Failed to send data to qBittorrent\n{}", e))?; + Ok(()) } -async fn logout(qbit: &QbitConfig, sid: String, client: &reqwest::Client) { +async fn logout(qbit: &QbitConfig, client: &reqwest::Client) { let base_url = add_protocol(qbit.base_url.clone(), false); let _ = client .get(format!("{}/api/v2/auth/logout", base_url)) .header(REFERER, base_url) - .header(COOKIE, sid) .send() .await; } async fn add_torrent( qbit: &QbitConfig, - sid: String, links: String, client: &reqwest::Client, ) -> Result { @@ -160,7 +142,6 @@ async fn add_torrent( client .post(url) .header(REFERER, base_url) - .header(COOKIE, sid) .form(&qbit.to_form(links)) .send() .await @@ -199,12 +180,9 @@ impl DownloadClient for QbitClient { )); } } - let sid = match login(&qbit, &client).await { - Ok(s) => s, - Err(e) => { - return DownloadResult::error(DownloadError(format!("Failed to get SID:\n{}", e))) - } - }; + if let Err(e) = login(&qbit, &client).await { + return DownloadResult::error(DownloadError(format!("Failed to get SID:\n{}", e))); + } let links = match qbit.use_magnet.unwrap_or(true) { true => items .iter() @@ -217,7 +195,7 @@ impl DownloadClient for QbitClient { .collect::>() .join("\n"), }; - let res = match add_torrent(&qbit, sid.to_owned(), links, &client).await { + let res = match add_torrent(&qbit, links, &client).await { Ok(res) => res, Err(e) => { return DownloadResult::error(DownloadError(format!( @@ -233,7 +211,7 @@ impl DownloadClient for QbitClient { ))); } - logout(&qbit, sid.clone(), &client).await; + logout(&qbit, &client).await; DownloadResult::new( format!("Successfully sent {} torrents to qBittorrent", items.len()), diff --git a/src/util/conv.rs b/src/util/conv.rs index 16bb1a4..fdf0d5e 100644 --- a/src/util/conv.rs +++ b/src/util/conv.rs @@ -1,5 +1,4 @@ use crossterm::event::{KeyCode, KeyModifiers, MediaKeyCode, ModifierKeyCode}; -use regex::Regex; pub fn add_protocol>(url: S, default_https: bool) -> String { let protocol = match default_https { @@ -7,12 +6,12 @@ pub fn add_protocol>(url: S, default_https: bool) -> String { false => "http", }; let url = url.into(); - let re = Regex::new(r"^(https?|socks5)?://.+$").unwrap(); - match re.is_match(&url) { - true => url, - // Assume http(s) if not present - false => format!("{}://{}", protocol, url), + if let Some((method, other)) = url.split_once(':') { + if matches!(method, "http" | "https" | "socks5") && matches!(other.get(..2), Some("//")) { + return url; + } } + format!("{}://{}", protocol, url) } pub fn to_bytes(size: &str) -> usize {