From a5e7b766ac0fd402f425523ff4f7678d6fbe124d Mon Sep 17 00:00:00 2001 From: Rbanh Date: Sun, 30 Mar 2025 19:52:47 -0400 Subject: [PATCH] Fix SpigotMC plugin downloads by using direct URLs --- src-tauri/src/crawlers/spigotmc.rs | 39 ++++++++++++++++++++------- src-tauri/src/services/http/client.rs | 23 +++++++++++++++- 2 files changed, 51 insertions(+), 11 deletions(-) diff --git a/src-tauri/src/crawlers/spigotmc.rs b/src-tauri/src/crawlers/spigotmc.rs index 29c818a..1de75ef 100644 --- a/src-tauri/src/crawlers/spigotmc.rs +++ b/src-tauri/src/crawlers/spigotmc.rs @@ -352,20 +352,39 @@ impl Repository for SpigotMCCrawler { } async fn download_plugin(&self, plugin_id: &str, version: &str, destination: &Path) -> Result { - // Going with Option 1: Download latest version associated with the resource ID + // First, get the plugin details let details = self.get_plugin_details(plugin_id).await?; - let download_url = &details.download_url; - if download_url.is_empty() { - return Err(format!("No download URL found for SpigotMC resource {}", plugin_id)); - } + // Use the SpigotMC direct download URL instead of SpiGet + // SpigotMC has a direct download URL pattern for resources + let direct_download_url = format!("https://www.spigotmc.org/resources/{}/download", plugin_id); - // We ignore the 'version' parameter here because SpiGet usually only provides the latest download - println!("Warning: SpigotMC download via SpiGet usually fetches the LATEST version, requested version '{}' might be ignored.", version); + // Log the download attempt + println!("Attempting to download plugin from SpigotMC direct URL: {}", direct_download_url); + println!("Requested version: {} (Note: SpigotMC usually provides latest version only)", version); - match self.client.download(download_url, destination).await { - Ok(_) => Ok(destination.to_string_lossy().to_string()), // Restore returning path - Err(e) => Err(format!("Failed to download from SpiGet: {}", e)) + // Try to download using direct SpigotMC URL + match self.client.download(&direct_download_url, destination).await { + Ok(_) => { + println!("Successfully downloaded plugin from SpigotMC direct URL"); + Ok(destination.to_string_lossy().to_string()) + }, + Err(e) => { + // If direct download fails, try the SpiGet URL as fallback + println!("Direct SpigotMC download failed: {}. Trying SpiGet URL as fallback...", e); + + let download_url = &details.download_url; + if download_url.is_empty() { + return Err(format!("No download URL found for SpigotMC resource {}", plugin_id)); + } + + println!("Falling back to SpiGet download URL: {}", download_url); + + match self.client.download(download_url, destination).await { + Ok(_) => Ok(destination.to_string_lossy().to_string()), + Err(e) => Err(format!("Failed to download from SpiGet: {}", e)) + } + } } } } \ No newline at end of file diff --git a/src-tauri/src/services/http/client.rs b/src-tauri/src/services/http/client.rs index a82477b..b832d82 100644 --- a/src-tauri/src/services/http/client.rs +++ b/src-tauri/src/services/http/client.rs @@ -154,7 +154,22 @@ impl HttpClient { .build()?; let mut headers = HeaderMap::new(); - headers.insert(USER_AGENT, HeaderValue::from_static("PlugSnatcherApp/0.1.0")); + headers.insert(USER_AGENT, HeaderValue::from_static("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36")); // More browser-like + + // Add Accept header for everything + headers.insert(ACCEPT, HeaderValue::from_static("*/*")); + + // Add headers for better browser simulation when dealing with SpigotMC + if url.contains("spigotmc.org") { + headers.insert(ACCEPT, HeaderValue::from_static("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")); + // Add custom headers as strings + let _ = headers.insert("Sec-Fetch-Dest", HeaderValue::from_static("document")); + let _ = headers.insert("Sec-Fetch-Mode", HeaderValue::from_static("navigate")); + let _ = headers.insert("Sec-Fetch-Site", HeaderValue::from_static("none")); + let _ = headers.insert("Sec-Fetch-User", HeaderValue::from_static("?1")); + headers.insert(reqwest::header::UPGRADE_INSECURE_REQUESTS, HeaderValue::from_static("1")); + println!("Added browser simulation headers for SpigotMC download"); + } // Add GitHub token if URL is GitHub and we have a token if url.contains("github.com") && self.github_token.is_some() { @@ -166,8 +181,12 @@ impl HttpClient { } // Get response + println!("Sending download request to: {}", url); let response = client.get(url).headers(headers).send().await?; + // Log response status + println!("Download response status: {}", response.status()); + // Check if request was successful if !response.status().is_success() { return Err(format!("Failed to download: Status {}", response.status()).into()); @@ -175,6 +194,7 @@ impl HttpClient { // Get response bytes let bytes = response.bytes().await?; + println!("Downloaded {} bytes", bytes.len()); // Create parent directories if needed if let Some(parent) = destination.parent() { @@ -185,6 +205,7 @@ impl HttpClient { // Write to file std::fs::write(destination, bytes)?; + println!("Successfully wrote file to: {}", destination.display()); Ok(()) }