feat: Implement backend commands for plugin data management and enhance frontend synchronization with updated plugin interfaces

This commit is contained in:
Rbanh committed 2025-03-30 19:31:00 -04:00
1 parent 78f22f65f4
commit 83be780243
35 files changed
+5261 -2208

No files matched your search

+229
View File
@@ -0,0 +1,229 @@
use std::error::Error;
use std::path::Path;
use serde::{Serialize, Deserialize};
use crate::{HttpClient, RepositorySource};
use urlencoding;
use async_trait::async_trait;
use std::sync::Arc;
use crate::models::repository::RepositoryPlugin;
use crate::crawlers::Repository;
// GitHub API response structures (Based on https://docs.github.com/en/rest/releases/releases)
#[derive(Debug, Serialize, Deserialize)]
struct GitHubRelease {
// Structure for release details
tag_name: String,
name: Option<String>,
body: Option<String>,
published_at: String,
assets: Vec<GitHubAsset>,
html_url: String, // URL to the release page
prerelease: bool,
draft: bool,
}
#[derive(Debug, Serialize, Deserialize)]
struct GitHubAsset {
// Structure for release asset details
name: String,
browser_download_url: String,
size: u64,
updated_at: String,
download_count: u64,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
struct GitHubRepo {
// Structure for repository details (used for searching potentially)
id: u64,
name: String,
full_name: String, // "owner/repo"
owner: GitHubUser,
description: Option<String>,
html_url: String,
stargazers_count: u64, // Can use this as a proxy for rating/popularity
updated_at: String,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
struct GitHubUser {
login: String,
avatar_url: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
struct GitHubSearchResponse {
// Structure for repository search results
items: Vec<GitHubRepo>,
// Ignoring total_count, incomplete_results for now
}
// GitHub crawler implementation
#[derive(Clone)]
pub struct GitHubCrawler {
client: Arc<HttpClient>,
api_base_url: String,
}
// Inherent methods for GitHubCrawler
impl GitHubCrawler {
pub fn new() -> Self {
GitHubCrawler {
client: Arc::new(HttpClient::new()),
api_base_url: "https://api.github.com".to_string(),
}
}
// Helper function to get the latest release for a repo, now async
async fn get_latest_release_details(&self, repo_full_name: &str) -> Result<GitHubRelease, Box<dyn Error + Send + Sync>> {
let url = format!("{}/repos/{}/releases/latest", self.api_base_url, repo_full_name);
let response_body = self.client.get(&url).await?;
let release: GitHubRelease = serde_json::from_str(&response_body)?;
Ok(release)
}
// Helper function to get all releases, now async
async fn get_all_release_details(&self, repo_full_name: &str) -> Result<Vec<GitHubRelease>, Box<dyn Error + Send + Sync>> {
let url = format!("{}/repos/{}/releases?per_page=100", self.api_base_url, repo_full_name);
let response_body = self.client.get(&url).await?;
let releases: Vec<GitHubRelease> = serde_json::from_str(&response_body)?;
Ok(releases)
}
// Helper function to get repo details, now async
async fn get_repo_details(&self, repo_full_name: &str) -> Result<GitHubRepo, Box<dyn Error + Send + Sync>> {
let repo_url = format!("{}/repos/{}", self.api_base_url, repo_full_name);
let repo_response_body = self.client.get(&repo_url).await?;
let repo: GitHubRepo = serde_json::from_str(&repo_response_body)?;
Ok(repo)
}
async fn get_plugin_versions(&self, repo_full_name: &str) -> Result<Vec<String>, String> {
let releases = match self.get_all_release_details(repo_full_name).await {
Ok(r) => r,
Err(e) => return Err(format!("Failed GitHub get releases for versions: {}", e)),
};
let versions = releases.into_iter()
.filter(|r| !r.draft && !r.prerelease && r.assets.iter().any(|a| a.name.ends_with(".jar")))
.map(|r| r.tag_name)
.collect();
Ok(versions)
}
}
#[async_trait]
impl Repository for GitHubCrawler {
fn get_repository_name(&self) -> String {
"GitHub".to_string()
}
async fn search(&self, query: &str) -> Result<Vec<RepositoryPlugin>, String> {
let search_terms = format!("{} topic:minecraft-plugin topic:spigot topic:paper topic:bukkit fork:false", query);
let encoded_query = urlencoding::encode(&search_terms);
let url = format!(
"{}/search/repositories?q={}&sort=stars&order=desc",
self.api_base_url,
encoded_query
);
let response_body = match self.client.get(&url).await {
Ok(body) => body,
Err(e) => return Err(format!("GitHub search request failed: {}", e)),
};
let search_response: GitHubSearchResponse = match serde_json::from_str(&response_body) {
Ok(res) => res,
Err(e) => return Err(format!("Failed to parse GitHub search results: {}", e)),
};
let fetch_tasks = search_response.items.into_iter().map(|repo| {
let self_clone = self.clone();
async move {
self_clone.get_plugin_details(&repo.full_name).await
}
});
let results: Vec<RepositoryPlugin> = futures::future::join_all(fetch_tasks).await
.into_iter()
.filter_map(|result| match result {
Ok(plugin) => Some(plugin),
Err(e) => {
println!("Error fetching details during search: {}", e);
None
}
})
.collect();
Ok(results)
}
async fn get_plugin_details(&self, plugin_id: &str) -> Result<RepositoryPlugin, String> {
let repo_full_name = plugin_id;
let repo = match self.get_repo_details(repo_full_name).await {
Ok(r) => r,
Err(e) => return Err(format!("Failed GitHub get repo details: {}", e)),
};
let mut releases = match self.get_all_release_details(repo_full_name).await {
Ok(r) => r,
Err(e) => return Err(format!("Failed GitHub get releases: {}", e)),
};
releases.sort_by(|a, b| b.published_at.cmp(&a.published_at));
let latest_valid_release = releases.into_iter()
.filter(|r| !r.draft && !r.prerelease)
.find(|r| r.assets.iter().any(|a| a.name.ends_with(".jar")));
if let Some(release) = latest_valid_release {
if let Some(asset) = release.assets.iter().find(|a| a.name.ends_with(".jar")) {
Ok(RepositoryPlugin {
id: repo_full_name.to_string(),
name: repo.name,
version: release.tag_name,
description: repo.description,
authors: vec![repo.owner.login],
download_url: asset.browser_download_url.clone(),
repository: RepositorySource::GitHub,
page_url: repo.html_url,
download_count: Some(asset.download_count),
last_updated: Some(release.published_at),
icon_url: repo.owner.avatar_url,
minecraft_versions: Vec::new(),
categories: Vec::new(),
rating: Some(repo.stargazers_count as f32),
file_size: Some(asset.size),
file_hash: None,
changelog: release.body,
})
} else {
Err(format!("No suitable JAR asset found in the latest valid release for {}", repo_full_name))
}
} else {
Err(format!("No valid release with a JAR asset found for {}", repo_full_name))
}
}
async fn download_plugin(&self, plugin_id: &str, version_number_str: &str, destination: &Path) -> Result<String, String> {
let repo_full_name = plugin_id;
let tag_name = version_number_str;
let release_url = format!("{}/repos/{}/releases/tags/{}", self.api_base_url, repo_full_name, tag_name);
let release_response_body = match self.client.get(&release_url).await {
Ok(body) => body,
Err(e) => return Err(format!("GitHub get release by tag failed: {}", e)),
};
let release: GitHubRelease = match serde_json::from_str(&release_response_body) {
Ok(r) => r,
Err(e) => return Err(format!("Failed to parse GitHub release by tag: {}", e)),
};
let asset = release.assets.iter()
.find(|a| a.name.ends_with(".jar"))
.ok_or_else(|| format!("No suitable JAR asset found in release tag '{}' for {}", tag_name, repo_full_name))?;
match self.client.download(&asset.browser_download_url, destination).await {
Ok(_) => Ok(destination.to_string_lossy().to_string()),
Err(e) => Err(format!("Failed to download from GitHub: {}", e))
}
}
}
+114 -97
View File
@@ -1,7 +1,12 @@
use std::error::Error;
use std::path::Path;
use serde::{Serialize, Deserialize};
use crate::{HttpClient, RepositoryCrawler, RepositoryPlugin, RepositorySource};
use crate::HttpClient;
use crate::models::repository::{RepositoryPlugin, RepositorySource};
use crate::crawlers::Repository;
use urlencoding;
use async_trait::async_trait;
use std::sync::Arc;
// HangarMC API response structures
#[derive(Debug, Serialize, Deserialize)]
@@ -63,141 +68,153 @@ struct HangarVersion {
platform_versions: Vec<String>,
}
// HangarMC crawler implementation
// Hangar crawler implementation
pub struct HangarCrawler {
client: HttpClient,
client: Arc<HttpClient>,
api_base_url: String,
web_base_url: String,
}
impl HangarCrawler {
pub fn new() -> Self {
HangarCrawler {
client: HttpClient::new(),
client: Arc::new(HttpClient::new()),
api_base_url: "https://hangar.papermc.io/api/v1".to_string(),
web_base_url: "https://hangar.papermc.io".to_string(),
}
}
fn get_project_details(&self, owner: &str, slug: &str) -> Result<HangarProject, Box<dyn Error>> {
let url = format!("{}/projects/{}/{}", self.api_base_url, owner, slug);
let response = self.client.get(&url)?;
let project: HangarProject = serde_json::from_str(&response)?;
async fn get_project_details_internal(&self, author: &str, slug: &str) -> Result<HangarProject, Box<dyn Error + Send + Sync>> {
let url = format!("{}/projects/{}/{}", self.api_base_url, author, slug);
let response_body = self.client.get(&url).await?;
let project: HangarProject = serde_json::from_str(&response_body)?;
Ok(project)
}
fn get_project_versions(&self, owner: &str, slug: &str) -> Result<Vec<HangarVersion>, Box<dyn Error>> {
let url = format!("{}/projects/{}/{}/versions", self.api_base_url, owner, slug);
let response = self.client.get(&url)?;
let versions_response: HangarVersionsResponse = serde_json::from_str(&response)?;
Ok(versions_response.result)
}
fn build_download_url(&self, owner: &str, slug: &str, version: &str) -> String {
format!("https://hangar.papermc.io/api/v1/projects/{}/{}/versions/{}/download", owner, slug, version)
async fn get_project_versions_internal(&self, author: &str, slug: &str) -> Result<Vec<HangarVersion>, Box<dyn Error + Send + Sync>> {
let url = format!("{}/projects/{}/{}/versions?limit=25&offset=0", self.api_base_url, author, slug);
let response_body = self.client.get(&url).await?;
let versions_result: HangarVersionsResponse = serde_json::from_str(&response_body)?;
Ok(versions_result.result)
}
}
impl RepositoryCrawler for HangarCrawler {
fn search(&self, query: &str) -> Result<Vec<RepositoryPlugin>, Box<dyn Error>> {
let url = format!("{}/projects?query={}&limit=20", self.api_base_url, query);
let response = self.client.get(&url)?;
#[async_trait]
impl Repository for HangarCrawler {
fn get_repository_name(&self) -> String {
"HangarMC".to_string()
}
let projects_response: HangarProjectsResponse = serde_json::from_str(&response)?;
let mut results = Vec::new();
async fn search(&self, query: &str) -> Result<Vec<RepositoryPlugin>, String> {
let encoded_query = urlencoding::encode(query);
let url = format!(
"{}/projects?q={}",
self.api_base_url,
encoded_query
);
for project in projects_response.result {
// For each project, get the latest version
let versions = self.get_project_versions(&project.namespace.owner, &project.namespace.slug)?;
let response_body = match self.client.get(&url).await {
Ok(body) => body,
Err(e) => return Err(format!("Failed to search HangarMC: {}", e)),
};
if let Some(latest_version) = versions.first() {
results.push(RepositoryPlugin {
id: format!("{}/{}", project.namespace.owner, project.namespace.slug),
name: project.name,
version: latest_version.name.clone(),
description: project.description,
authors: vec![project.namespace.owner.clone()],
download_url: self.build_download_url(&project.namespace.owner, &project.namespace.slug, &latest_version.name),
repository: RepositorySource::HangarMC,
page_url: format!("https://hangar.papermc.io/{}/{}", project.namespace.owner, project.namespace.slug),
download_count: Some(project.stats.downloads),
last_updated: Some(project.last_updated),
icon_url: project.icon_url,
minecraft_versions: latest_version.platform_versions.clone(),
categories: vec![project.category],
rating: None, // HangarMC uses stars, not ratings
file_size: Some(latest_version.file_size),
file_hash: None, // HangarMC API doesn't provide file hashes
});
let search_response: HangarProjectsResponse = match serde_json::from_str(&response_body) {
Ok(res) => res,
Err(e) => return Err(format!("Failed to parse HangarMC search results: {}", e)),
};
let results: Vec<RepositoryPlugin> = search_response.result.into_iter().map(|proj| {
let page_url = format!("{}/{}/{}", self.web_base_url, proj.namespace.owner, proj.namespace.slug);
let version = "Unknown".to_string();
RepositoryPlugin {
id: format!("{}/{}", proj.namespace.owner, proj.namespace.slug),
name: proj.name,
version,
description: proj.description.clone(),
authors: vec![proj.namespace.owner],
download_url: String::new(),
repository: RepositorySource::HangarMC,
page_url,
download_count: Some(proj.stats.downloads),
last_updated: Some(proj.last_updated),
icon_url: proj.icon_url.clone(),
minecraft_versions: Vec::new(),
categories: vec![proj.category.to_string()],
rating: Some(proj.stats.stars as f32),
file_size: None,
file_hash: None,
changelog: None,
}
}
}).collect();
Ok(results)
}
fn get_plugin_details(&self, plugin_id: &str) -> Result<RepositoryPlugin, Box<dyn Error>> {
async fn get_plugin_details(&self, plugin_id: &str) -> Result<RepositoryPlugin, String> {
let parts: Vec<&str> = plugin_id.split('/').collect();
if parts.len() != 2 {
return Err("Invalid plugin ID format for HangarMC. Expected 'owner/slug'".into());
return Err(format!("Invalid Hangar plugin ID format: {}. Expected 'author/slug'.", plugin_id));
}
let owner = parts[0];
let author = parts[0];
let slug = parts[1];
let project = self.get_project_details(owner, slug)?;
let versions = self.get_project_versions(owner, slug)?;
let project = match self.get_project_details_internal(author, slug).await {
Ok(p) => p,
Err(e) => return Err(format!("Failed to get Hangar project details: {}", e)),
};
if let Some(latest_version) = versions.first() {
Ok(RepositoryPlugin {
id: plugin_id.to_string(),
name: project.name,
version: latest_version.name.clone(),
description: project.description,
authors: vec![project.namespace.owner.clone()],
download_url: self.build_download_url(owner, slug, &latest_version.name),
repository: RepositorySource::HangarMC,
page_url: format!("https://hangar.papermc.io/{}/{}", owner, slug),
download_count: Some(project.stats.downloads),
last_updated: Some(project.last_updated),
icon_url: project.icon_url,
minecraft_versions: latest_version.platform_versions.clone(),
categories: vec![project.category],
rating: None,
file_size: Some(latest_version.file_size),
file_hash: None,
})
} else {
Err("No versions found for this plugin".into())
}
let versions = match self.get_project_versions_internal(author, slug).await {
Ok(v) => v,
Err(e) => return Err(format!("Failed to get Hangar project versions: {}", e)),
};
let latest_version_name = versions.first().map_or("Unknown".to_string(), |v| v.name.clone());
let page_url = format!("{}/{}/{}", self.web_base_url, author, slug);
Ok(RepositoryPlugin {
id: plugin_id.to_string(),
name: project.name,
version: latest_version_name,
description: project.description.clone(),
authors: vec![project.namespace.owner],
download_url: String::new(),
repository: RepositorySource::HangarMC,
page_url,
download_count: Some(project.stats.downloads),
last_updated: Some(project.last_updated),
icon_url: project.icon_url.clone(),
minecraft_versions: versions.first().map_or(Vec::new(), |v| v.platform_versions.clone()),
categories: vec![project.category.to_string()],
rating: Some(project.stats.stars as f32),
file_size: versions.first().map(|v| v.file_size),
file_hash: None,
changelog: None,
})
}
fn get_plugin_versions(&self, plugin_id: &str) -> Result<Vec<String>, Box<dyn Error>> {
async fn download_plugin(&self, plugin_id: &str, version: &str, destination: &Path) -> Result<String, String> {
let parts: Vec<&str> = plugin_id.split('/').collect();
if parts.len() != 2 {
return Err("Invalid plugin ID format for HangarMC. Expected 'owner/slug'".into());
return Err(format!("Invalid Hangar plugin ID format: {}. Expected 'author/slug'.", plugin_id));
}
let owner = parts[0];
let author = parts[0];
let slug = parts[1];
let versions = self.get_project_versions(owner, slug)?;
Ok(versions.into_iter().map(|v| v.name).collect())
}
let platform_str = "PAPER";
fn download_plugin(&self, plugin_id: &str, version: &str, destination: &Path) -> Result<String, Box<dyn Error>> {
let parts: Vec<&str> = plugin_id.split('/').collect();
if parts.len() != 2 {
return Err("Invalid plugin ID format for HangarMC. Expected 'owner/slug'".into());
let download_url = format!(
"{}/projects/{}/{}/versions/{}/{}/download",
self.api_base_url,
author,
slug,
version,
platform_str
);
match self.client.download(&download_url, destination).await {
Ok(_) => Ok(destination.to_string_lossy().to_string()),
Err(e) => Err(format!("Failed to download plugin: {}", e))
}
let owner = parts[0];
let slug = parts[1];
let download_url = self.build_download_url(owner, slug, version);
self.client.download(&download_url, destination)?;
Ok(destination.to_string_lossy().to_string())
}
fn get_repository_name(&self) -> RepositorySource {
RepositorySource::HangarMC
}
}
+40 -1
View File
@@ -1,4 +1,43 @@
pub mod hangar;
pub mod spigotmc;
pub mod modrinth;
pub mod github;
use std::path::Path;
use crate::models::repository::RepositoryPlugin;
use crate::models::server::ServerType;
use async_trait::async_trait;
/// Common interface for all repository crawlers
#[async_trait]
pub trait Repository {
/// Get the name of the repository
fn get_repository_name(&self) -> String;
/// Search for plugins in the repository
async fn search(&self, query: &str) -> Result<Vec<RepositoryPlugin>, String>;
/// Get plugin details from the repository
async fn get_plugin_details(&self, plugin_id: &str) -> Result<RepositoryPlugin, String>;
/// Get plugin details with server type consideration
async fn get_plugin_details_with_server_type(&self, plugin_id: &str, server_type: Option<&ServerType>) -> Result<RepositoryPlugin, String> {
// Default implementation just calls the regular get_plugin_details
self.get_plugin_details(plugin_id).await
}
/// Download a plugin from the repository
async fn download_plugin(&self, plugin_id: &str, version: &str, destination: &Path) -> Result<String, String>;
/// Download a plugin with server type consideration
async fn download_plugin_with_server_type(&self, plugin_id: &str, version: &str, destination: &Path, server_type: Option<&ServerType>) -> Result<String, String> {
// Default implementation calls the regular download_plugin
self.download_plugin(plugin_id, version, destination).await
}
}
// Re-export the crawler implementations
pub use hangar::HangarCrawler;
pub use hangar::HangarCrawler;
pub use spigotmc::SpigotMCCrawler;
pub use modrinth::ModrinthCrawler;
pub use github::GitHubCrawler;
+371
View File
@@ -0,0 +1,371 @@
use serde::{Deserialize, Serialize}; // Added Serialize for potential use, Deserialize is essential
use std::error::Error;
use std::path::Path;
use crate::{HttpClient};
use crate::models::repository::{RepositoryPlugin, RepositorySource};
use urlencoding;
use async_trait::async_trait;
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH}; // For converting timestamp
use base64::{Engine as _, engine::general_purpose::STANDARD}; // Correct import with Engine trait
use serde_json::Value; // Import Value
use crate::crawlers::Repository; // Use the correct trait import
// --- Structs for SpiGet API Deserialization ---
#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
struct SpiGetIcon {
url: Option<String>,
// data: Option<String>, // Base64 data not typically needed if URL is present
}
#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
struct SpiGetRating {
average: f32,
}
#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
struct SpiGetAuthor {
id: u32,
name: Option<String>, // SpiGet might not always return name in all contexts
// icon: Option<SpiGetIcon>, // Icon data might be available in author details endpoint
}
#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
struct SpiGetFile {
// #[serde(rename = "type")]
// file_type: Option<String>, // e.g. ".jar"
size: Option<Value>, // Use Value to accept string or number from API
size_unit: Option<String>, // e.g. "MB"
url: Option<String>, // Link to the spigotmc resource page, *not* a direct download
// externalUrl: Option<String> // Field from docs, maybe add if needed later
}
// Represents a version summary, often nested or in arrays
#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
struct SpiGetVersion {
id: u32,
name: Option<String>, // Make optional: The actual version string e.g., "1.19.4" or "v2.1"
uuid: Option<String>,
release_date: Option<u64>, // Timestamp
downloads: Option<u32>,
}
#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
struct SpiGetUpdate {
description: String, // Base64 encoded HTML description
}
#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
struct IdReference { // Used for arrays containing only IDs
id: u32,
}
#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
struct SpiGetResource {
id: u32,
name: Option<String>, // Make name optional to handle potential missing field in search results
tag: Option<String>, // Make tag optional as well for robustness
version: SpiGetVersion, // Represents the *latest* version details here
author: SpiGetAuthor, // Contains author ID, maybe name
downloads: u32,
tested_versions: Vec<String>, // List of MC versions plugin is tested against
rating: SpiGetRating,
icon: SpiGetIcon, // Contains URL to icon
update_date: u64, // Timestamp
file: SpiGetFile, // Details about the main file download
external: bool, // If true, download link points externally
}
// --- SpigotMC Crawler Implementation (using SpiGet API) ---
pub struct SpigotMCCrawler {
client: Arc<HttpClient>,
base_url: String,
}
impl SpigotMCCrawler {
pub fn new() -> Self {
SpigotMCCrawler {
client: Arc::new(HttpClient::new()),
base_url: "https://api.spiget.org/v2".to_string(), // Use SpiGet API base URL
}
}
// Helper to convert SpiGetResource to RepositoryPlugin
fn map_resource_to_plugin(&self, resource: &SpiGetResource) -> RepositoryPlugin {
// Construct SpigotMC page URL
let page_url = format!("https://www.spigotmc.org/resources/{}", resource.id);
// Construct potential download URL (may differ for external resources)
let download_url = if resource.external {
// For external resources, the 'url' in file info is the download link
resource.file.url.clone().unwrap_or_default()
} else {
// For internal resources, use the SpiGet download endpoint for the *latest* version
format!("{}/resources/{}/download", self.base_url, resource.id)
};
// Format update date
let last_updated = SystemTime::UNIX_EPOCH
.checked_add(std::time::Duration::from_secs(resource.update_date))
.map(|_st| { // Remove chrono formatting for now
// Simple ISO 8601 format or similar - requires chrono for better formatting
// For now, just return the timestamp as string for simplicity
format!("{}", resource.update_date)
});
// Safely get author name
let author_name = resource.author.name.clone().unwrap_or_else(|| format!("#{}", resource.author.id));
// Convert file size, handling potential string in 'size' field
let file_size_bytes = resource.file.size.as_ref().and_then(|s| s.as_f64()).map(|s_num| {
match resource.file.size_unit.as_deref() {
Some("KB") => (s_num * 1024.0) as u64,
Some("MB") => (s_num * 1024.0 * 1024.0) as u64,
Some("GB") => (s_num * 1024.0 * 1024.0 * 1024.0) as u64,
_ => s_num as u64, // Assume bytes if unit is missing or unknown
}
});
// Prepend base URL to icon URL if it's relative (SpiGet usually provides full URL)
let icon_url = resource.icon.url.clone();
// Use id as fallback if name is missing
let plugin_name = resource.name.clone().unwrap_or_else(|| format!("Unnamed Resource #{}", resource.id));
// Extract version information properly
let version_name = resource.version.name.clone().unwrap_or_else(|| {
println!("[SpigotMCCrawler::map_resource_to_plugin] Warning: Missing version name for resource ID: {}", resource.id);
// DO NOT use tested Minecraft versions as fallbacks for plugin versions
// since they are different types of versions
"Unknown".to_string()
});
println!("[SpigotMCCrawler::map_resource_to_plugin] Version for {}: {}", plugin_name, version_name);
RepositoryPlugin {
id: resource.id.to_string(),
name: plugin_name, // Use the potentially fallback name
version: version_name, // Use the potentially fallback version name
description: resource.tag.clone(), // Use tagline as description (already Option<String>)
authors: vec![author_name],
download_url,
repository: RepositorySource::SpigotMC,
page_url,
download_count: Some(resource.downloads as u64),
last_updated,
icon_url, // Use the potentially prefixed URL
minecraft_versions: resource.tested_versions.clone(),
categories: Vec::new(), // SpiGet only gives category ID, fetching name requires another call
rating: Some(resource.rating.average),
file_size: file_size_bytes,
file_hash: None, // SpiGet does not provide hashes
changelog: None, // Needs separate call to /updates/latest
}
}
// Helper to fetch and decode the latest update's description as changelog
async fn get_latest_changelog(&self, resource_id: &str) -> Result<Option<String>, Box<dyn Error + Send + Sync>> {
let url = format!("{}/resources/{}/updates/latest", self.base_url, resource_id);
match self.client.get(&url).await {
Ok(body) => {
match serde_json::from_str::<SpiGetUpdate>(&body) {
Ok(update) => {
// Description is Base64 encoded HTML
match STANDARD.decode(&update.description) {
Ok(decoded_bytes) => {
// Convert bytes to string (assuming UTF-8)
// decoded_bytes is now a Vec<u8>, which implements Sized
Ok(Some(String::from_utf8_lossy(&decoded_bytes).to_string()))
}
Err(e) => {
println!("Failed to decode base64 changelog for {}: {}", resource_id, e);
Ok(None) // Return None if decoding fails
}
}
}
Err(e) => {
println!("Failed to parse latest update JSON for {}: {}", resource_id, e);
Ok(None) // Return None if parsing fails
}
}
}
Err(e) => {
// If the request itself fails (e.g., 404 if no updates), treat as no changelog
println!("Failed to fetch latest update for {}: {}", resource_id, e);
Ok(None)
}
}
}
// Moved get_plugin_versions back here as it's not part of the Repository trait
async fn get_plugin_versions(&self, plugin_id: &str) -> Result<Vec<String>, String> {
println!("Fetching versions for resource ID: {}", plugin_id);
let url = format!("{}/resources/{}/versions?sort=-releaseDate&size=10", self.base_url, plugin_id);
let body = match self.client.get(&url).await {
Ok(b) => b,
Err(e) => return Err(format!("Failed SpiGet versions request: {}", e))
};
match serde_json::from_str::<Vec<SpiGetVersion>>(&body) {
Ok(versions) => {
let version_names: Vec<String> = versions.into_iter()
.filter_map(|v| v.name)
.collect();
if version_names.is_empty() {
// If no version names available, try to extract from the version ID
println!("No named versions found for resource {}. Trying alternate methods.", plugin_id);
// Try to get full resource details for version info
let resource_url = format!("{}/resources/{}", self.base_url, plugin_id);
match self.client.get(&resource_url).await {
Ok(resource_body) => {
match serde_json::from_str::<SpiGetResource>(&resource_body) {
Ok(resource) => {
// Check if we can get a version from the resource directly
if let Some(name) = resource.version.name {
println!("Found version from resource details: {}", name);
return Ok(vec![name]);
}
},
Err(e) => println!("Failed to parse resource details: {}", e)
}
},
Err(e) => println!("Failed to fetch resource details: {}", e)
}
// If still no version, return a fallback
println!("Using fallback version for resource ID: {}", plugin_id);
Ok(vec!["Unknown".to_string()])
} else {
println!("Found {} versions for resource ID: {}", version_names.len(), plugin_id);
Ok(version_names)
}
},
Err(e) => {
println!("Failed to parse versions JSON: {}. Body: {}", e, body);
Err(format!("Failed to parse SpiGet versions: {}", e))
}
}
}
}
#[async_trait]
impl Repository for SpigotMCCrawler {
fn get_repository_name(&self) -> String {
"SpigotMC".to_string()
}
async fn search(&self, query: &str) -> Result<Vec<RepositoryPlugin>, String> {
let encoded_query = urlencoding::encode(query);
let url = format!(
"{}/search/resources/{}?field=name&fields=name,tag,author,version,downloads,rating,icon,updateDate,premium,file,external,testedVersions",
self.base_url,
encoded_query
);
let body = match self.client.get(&url).await {
Ok(b) => b,
Err(e) => return Err(format!("Failed SpiGet search request: {}", e))
};
match serde_json::from_str::<Vec<SpiGetResource>>(&body) {
Ok(resources) => {
let mut results = Vec::new();
for res in &resources {
let mut plugin = self.map_resource_to_plugin(res);
// Try fetching versions if missing
if plugin.version == "Unknown" {
println!("Searching for version information for resource ID: {}", plugin.id);
match self.get_plugin_versions(&plugin.id).await {
Ok(versions) => {
if let Some(latest) = versions.first() {
println!("Found version for {}: {}", plugin.name, latest);
plugin.version = latest.clone();
}
},
Err(e) => println!("Failed to fetch versions for {}: {}", plugin.id, e),
}
}
results.push(plugin);
}
Ok(results)
}
Err(e) => {
// Handle case where search returns a single object instead of array (e.g., direct ID match?)
// Or just return the parsing error
Err(format!("Failed to parse SpiGet search results: {}. Body: {}", e, body))
}
}
}
async fn get_plugin_details(&self, plugin_id: &str) -> Result<RepositoryPlugin, String> {
let url = format!(
"{}/resources/{}?fields=name,tag,author,version,downloads,rating,icon,updateDate,premium,file,external,testedVersions",
self.base_url,
plugin_id
);
let body = match self.client.get(&url).await {
Ok(b) => b,
Err(e) => return Err(format!("Failed SpiGet details request: {}", e))
};
match serde_json::from_str::<SpiGetResource>(&body) {
Ok(resource) => {
let mut plugin = self.map_resource_to_plugin(&resource);
// Fetch changelog
match self.get_latest_changelog(&plugin.id).await {
Ok(changelog_opt) => plugin.changelog = changelog_opt,
Err(e) => println!("Failed to fetch changelog for {}: {}", plugin.id, e),
}
// Try fetching versions if missing
if plugin.version == "Unknown" {
println!("Fetching versions for detail view of resource ID: {}", plugin.id);
match self.get_plugin_versions(&plugin.id).await {
Ok(versions) => {
if let Some(latest) = versions.first() {
println!("Found version for {} in detail view: {}", plugin.name, latest);
plugin.version = latest.clone();
}
},
Err(e) => println!("Failed to fetch versions for {} in detail view: {}", plugin.id, e),
}
}
Ok(plugin)
}
Err(e) => Err(format!("Failed to parse SpiGet resource details: {}. Body: {}", e, body))
}
}
async fn download_plugin(&self, plugin_id: &str, version: &str, destination: &Path) -> Result<String, String> {
// Going with Option 1: Download latest version associated with the resource ID
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));
}
// 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);
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))
}
}
}