From 281c65b5a984018f694a18bc9773e1410e82b446 Mon Sep 17 00:00:00 2001 From: TheAlan404 Date: Sat, 23 Sep 2023 23:03:37 +0300 Subject: [PATCH] this is better --- src/main.rs | 48 +++--- src/model/downloadable/import_url.rs | 27 ++-- src/model/downloadable/markdown.rs | 34 ++-- src/model/downloadable/mod.rs | 202 ++++-------------------- src/model/downloadable/packwiz.rs | 34 +--- src/model/servertype/mod.rs | 223 +++------------------------ src/sources/curserinth.rs | 32 ++-- src/sources/fabric.rs | 35 ++--- src/sources/forge.rs | 4 +- src/sources/github.rs | 38 ++--- src/sources/hangar.rs | 40 ++--- src/sources/jenkins.rs | 42 ++--- src/sources/maven.rs | 31 ++-- src/sources/modrinth.rs | 34 ++-- src/sources/neoforge.rs | 4 +- src/sources/papermc.rs | 39 ++--- src/sources/purpur.rs | 43 ++---- src/sources/quilt.rs | 6 +- src/sources/spigot.rs | 39 ++--- src/sources/vanilla.rs | 37 ++--- 20 files changed, 267 insertions(+), 725 deletions(-) diff --git a/src/main.rs b/src/main.rs index 368462c..b5b4eba 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ #![allow(clippy::struct_excessive_bools)] #![allow(unknown_lints)] -use std::{path::PathBuf, collections::HashMap}; +use std::collections::HashMap; use anyhow::{Context, Result}; use async_trait::async_trait; @@ -111,7 +111,7 @@ pub trait Source { async fn resolve_source( &self, app: &App, - ) -> Result; + ) -> Result; } pub struct BaseApp { @@ -188,6 +188,10 @@ impl<'a> App { sources::maven::MavenAPI(&self) } + pub fn quilt(&'a self) -> sources::quilt::QuiltAPI<'a> { + sources::quilt::QuiltAPI(&self) + } + pub fn forge(&'a self) -> sources::forge::ForgeAPI<'a> { sources::forge::ForgeAPI(&self) } @@ -196,32 +200,40 @@ impl<'a> App { sources::neoforge::NeoforgeAPI(&self) } - -} + pub fn papermc(&'a self) -> sources::papermc::PaperMCAPI<'a> { + sources::papermc::PaperMCAPI(&self) + } -pub enum FileSource { - Download { - url: String, - filename: String, - cache: CacheStrategy, - size: Option, - hashes: HashMap, - }, + pub fn purpurmc(&'a self) -> sources::purpur::PurpurAPI<'a> { + sources::purpur::PurpurAPI(&self) + } + + pub fn spigot(&'a self) -> sources::spigot::SpigotAPI<'a> { + sources::spigot::SpigotAPI(&self) + } - Cached { - path: PathBuf, - filename: String, + pub fn vanilla(&'a self) -> sources::vanilla::VanillaAPI<'a> { + sources::vanilla::VanillaAPI(&self) } } +pub struct ResolvedFile { + url: String, + filename: String, + cache: CacheStrategy, + size: Option, + hashes: HashMap, +} + pub enum CacheStrategy { File { - path: PathBuf, + namespace: String, + path: String, }, Indexed { - index_path: PathBuf, + index_path: String, key: String, - value: PathBuf, + value: String, }, None, } diff --git a/src/model/downloadable/import_url.rs b/src/model/downloadable/import_url.rs index f9a46a9..45f3b92 100644 --- a/src/model/downloadable/import_url.rs +++ b/src/model/downloadable/import_url.rs @@ -3,12 +3,10 @@ use dialoguer::{theme::ColorfulTheme, Confirm, Input, Select}; use reqwest::Url; use crate::{ - model::Server, sources::{ - curserinth::{fetch_curserinth_versions, CurseRinthVersion}, - github::fetch_github_releases, - modrinth::{fetch_modrinth_versions, ModrinthVersion}, - }, + curserinth::CurseRinthVersion, + modrinth::ModrinthVersion, + }, App, }; use super::Downloadable; @@ -16,8 +14,7 @@ use super::Downloadable; impl Downloadable { #[allow(clippy::too_many_lines)] pub async fn from_url_interactive( - client: &reqwest::Client, - _server: &Server, + app: &App, urlstr: &str, datapack_mode: bool, ) -> Result { @@ -61,11 +58,9 @@ impl Downloadable { .to_owned() .to_owned(); - let versions: Vec = fetch_modrinth_versions(client, &id, None) + let versions: Vec = app.modrinth().fetch_versions(&id) .await? .into_iter() - // TODO: better filtering, commented out because proxy server versioning is complex..? - //.filter(|v| v.game_versions.contains(&server.mc_version)) .filter(|v| { if datapack_mode { v.loaders.contains(&"datapack".to_owned()) @@ -134,8 +129,7 @@ impl Downloadable { .to_owned() .to_owned(); - let versions: Vec = - fetch_curserinth_versions(client, &id, None).await?; + let (versions, _) = app.curserinth().fetch_versions(&id).await?; let version = if let Some(&"version") = segments.get(2) { let ver_num = segments @@ -204,10 +198,8 @@ impl Downloadable { let version = if let Some(v) = segments.get(4) { v.to_owned().to_owned() } else { - let versions: Vec = fetch_modrinth_versions(client, &id, None) - .await? - .into_iter() - .collect(); + let (versions, _) = app.curserinth().fetch_versions(&id) + .await?; if versions.is_empty() { bail!("No compatible versions in curseforge/rinth project"); @@ -254,6 +246,7 @@ impl Downloadable { Ok(Downloadable::Spigot { id: id.to_owned().to_owned(), + version: "latest".to_owned() }) } // the code under this domain is awful.. srry @@ -307,7 +300,7 @@ impl Downloadable { } }; - let fetched_tags = fetch_github_releases(&repo, client).await?; + let fetched_tags = app.github().fetch_releases(&repo).await?; let tag = if let Some(t) = tag_opt { t diff --git a/src/model/downloadable/markdown.rs b/src/model/downloadable/markdown.rs index 164343b..1ac0ac3 100644 --- a/src/model/downloadable/markdown.rs +++ b/src/model/downloadable/markdown.rs @@ -4,13 +4,7 @@ use regex::Regex; use crate::{ model::Downloadable, - sources::{ - curserinth::fetch_curserinth_project, - github::fetch_repo_description, - jenkins::{fetch_jenkins_description, str_process_job}, - modrinth::fetch_modrinth_project, - spigot::fetch_spigot_info, - }, + sources::jenkins::{fetch_jenkins_description, str_process_job}, App, }; impl Downloadable { @@ -38,7 +32,7 @@ impl Downloadable { Self::Maven { url, group, .. } => { format!("[{g}]({url}/{g})", g = group.replace('.', "/")) } - Self::Spigot { id } => { + Self::Spigot { id, .. } => { format!("[{id}](https://www.spigotmc.org/resources/{id})") } Self::Modrinth { id, .. } => { @@ -52,13 +46,13 @@ impl Downloadable { pub async fn fetch_info_to_map( &self, - client: &reqwest::Client, + app: &App, ) -> Result> { let mut map: IndexMap = IndexMap::new(); match self { Self::Modrinth { id, version } => { - let proj = fetch_modrinth_project(client, id).await?; + let proj = app.modrinth().fetch_project(id).await?; map.insert( "Name".to_owned(), @@ -69,7 +63,7 @@ impl Downloadable { } Self::CurseRinth { id, version } => { - let proj = fetch_curserinth_project(client, id).await?; + let proj = app.curserinth().fetch_project(id).await?; map.insert( "Name".to_owned(), @@ -79,18 +73,19 @@ impl Downloadable { map.insert("Version".to_owned(), version.clone()); } - Self::Spigot { id } => { - let (name, desc) = fetch_spigot_info(client, id).await?; + Self::Spigot { id, version } => { + let (name, desc) = app.spigot().fetch_info(id).await?; map.insert( "Name".to_owned(), format!("[{name}](https://www.spigotmc.org/resources/{id})"), ); map.insert("Description".to_owned(), sanitize(&desc)?); + map.insert("Version".to_owned(), version.clone()); } Self::Hangar { id, version } => { - let proj = mcapi::hangar::fetch_project(client, id).await?; + let proj = mcapi::hangar::fetch_project(&app.http_client, id).await?; map.insert( "Name".to_owned(), @@ -101,7 +96,7 @@ impl Downloadable { } Self::GithubRelease { repo, tag, asset } => { - let desc = fetch_repo_description(client, repo).await?; + let desc = app.github().fetch_repo_description(repo).await?; map.insert("Name".to_owned(), self.get_md_link()); map.insert("Description".to_owned(), sanitize(&desc)?); @@ -114,7 +109,7 @@ impl Downloadable { build, artifact, } => { - let desc = fetch_jenkins_description(client, url, job).await?; + let desc = fetch_jenkins_description(&app.http_client, url, job).await?; map.insert("Name".to_owned(), self.get_md_link()); map.insert("Description".to_owned(), sanitize(&desc)?); @@ -190,8 +185,9 @@ impl Downloadable { map.insert("Version/Release".to_owned(), version.clone()); } - Self::Spigot { id } => { + Self::Spigot { id, version } => { map.insert("Project/URL".to_owned(), id.clone()); + map.insert("Version/Release".to_owned(), version.clone()); } Self::Jenkins { @@ -229,9 +225,9 @@ impl Downloadable { Self::Modrinth { id, .. } => format!("Modrinth:{id}"), Self::Hangar { id, .. } => format!("Hangar:{id}"), Self::CurseRinth { id, .. } => format!("CurseRinth:{id}"), - Self::Spigot { id } => format!("Spigot:{id}"), + Self::Spigot { id, .. } => format!("Spigot:{id}"), Self::GithubRelease { repo, .. } => format!("Github:{repo}"), - Self::Jenkins { job, .. } => format!("Jenkins:{job}"), + Self::Jenkins { job, .. } => format!("Jenkins:{job}"), Self::Url { filename, .. } => { if let Some(f) = filename { format!("URL:{f}") diff --git a/src/model/downloadable/mod.rs b/src/model/downloadable/mod.rs index c64920a..046d743 100644 --- a/src/model/downloadable/mod.rs +++ b/src/model/downloadable/mod.rs @@ -1,18 +1,13 @@ +use std::collections::HashMap; + use anyhow::Result; use async_trait::async_trait; use serde::{Deserialize, Serialize}; -use crate::sources::hangar::{get_hangar_url, get_hangar_filename}; -use crate::sources::maven::{self, get_maven_url}; -use crate::{model::Server, Source}; +use crate::sources::jenkins; +use crate::{ResolvedFile, CacheStrategy, App}; +use crate::Source; -use crate::sources::{ - curserinth::{fetch_curserinth_filename, get_curserinth_url}, - github::{fetch_github_release_filename, get_github_release_url}, - jenkins::{get_jenkins_download_url, get_jenkins_filename}, - modrinth::{fetch_modrinth_filename, get_modrinth_url}, - spigot::{fetch_spigot_resource_latest_ver, get_spigot_url}, -}; mod import_url; mod markdown; mod meta; @@ -36,18 +31,20 @@ pub enum Downloadable { Modrinth { id: String, #[serde(default = "latest")] - version: String + version: String, }, #[serde(alias = "cr")] CurseRinth { id: String, #[serde(default = "latest")] - version: String + version: String, }, Spigot { - id: String, // weird ass api + id: String, + #[serde(default = "latest")] + version: String, }, Hangar { @@ -96,184 +93,39 @@ pub fn artifact() -> String { "artifact".to_owned() } -impl Downloadable { - pub async fn get_url( - &self, - client: &reqwest::Client, - server: &Server, - ) -> Result { - let mcver = &server.mc_version; - - match self { - Self::Url { url, .. } => Ok(url.clone()), - - Self::Modrinth { id, version } => { - Ok(get_modrinth_url(id, version, client, None).await?) - } - Self::CurseRinth { id, version } => { - Ok(get_curserinth_url(id, version, client, None).await?) - } - Self::Hangar { id, version } => { - Ok(get_hangar_url(client, id, version, mcver, server.jar.get_hangar_versions_filter(mcver)).await?) - } - Self::Spigot { id } => Ok(get_spigot_url(id)), - Self::GithubRelease { repo, tag, asset } => { - Ok(get_github_release_url(repo, tag, asset, mcver, client).await?) - } - - Self::Jenkins { - url, - job, - build, - artifact, - } => Ok(get_jenkins_download_url(client, url, job, build, artifact).await?), - - Self::Maven { - url, - group, - artifact, - version, - filename, - } => Ok(get_maven_url(client, url, group, artifact, version, filename, mcver).await?), - } - } -} - #[async_trait] impl Source for Downloadable { - async fn download( - &self, - server: &Server, - client: &reqwest::Client, - ) -> Result { - Ok(client - .get(self.get_url(client, server).await?) - .send() - .await? - .error_for_status()?) - } - - async fn get_filename(&self, server: &Server, client: &reqwest::Client) -> Result { - let mcver = &server.mc_version; - + async fn resolve_source(&self, app: &App) -> Result { match self { - Self::Url { url, filename, .. } => { - if let Some(filename) = filename { - return Ok(filename.clone()); - } - - let url_clean = url.split('?').next().unwrap_or(url); - Ok(url_clean.split('/').last().unwrap().to_string()) - } - - Self::Modrinth { id, version } => { - // nvm - let filename = fetch_modrinth_filename(id, version, client, None).await?; - Ok(filename) - } - - Self::CurseRinth { id, version } => { - let filename = fetch_curserinth_filename(id, version, client, None).await?; - Ok(filename) - } - - Self::Hangar { id, version } => { - Ok(get_hangar_filename(client, id, version, mcver, server.jar.get_hangar_versions_filter(mcver)).await?) - } - - Self::Spigot { id } => { - let ver = fetch_spigot_resource_latest_ver(id, client).await?; - // amazing.. bruh... - Ok(format!("{id}-{ver}.jar")) - } - - // problematic stuff part 2345 - Self::GithubRelease { repo, tag, asset } => { - Ok(fetch_github_release_filename(repo, tag, asset, mcver, client).await?) - } - - Self::Jenkins { - url, - job, - build, - artifact, - } => Ok(get_jenkins_filename(client, url, job, build, artifact) - .await? - .1), - - Self::Maven { + Self::Url { url, - group, - artifact, - version, filename, - } => Ok(maven::get_maven_filename( - client, url, group, artifact, version, filename, mcver, - ) - .await?), - } - } -} - -impl std::fmt::Display for Downloadable { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Self::Url { url, .. } => f.write_fmt(format_args!("Custom URL: {url}")), - - Self::Modrinth { id, version } => f - .debug_struct("Modrinth") - .field("id", id) - .field("version", version) - .finish(), - - Self::Hangar { id, version } => f - .debug_struct("Hangar") - .field("id", id) - .field("version", version) - .finish(), - - Self::CurseRinth { id, version } => f - .debug_struct("CurseRinth") - .field("id", id) - .field("version", version) - .finish(), - - Self::Spigot { id } => f.write_fmt(format_args!("Spigot: {id}")), - - Self::GithubRelease { repo, tag, asset } => f - .debug_struct("GithubRelease") - .field("Repository", repo) - .field("Tag/Release", tag) - .field("Asset", asset) - .finish(), - + .. + } => Ok(ResolvedFile { + url: url.clone(), + filename: filename.clone(), + cache: CacheStrategy::None, + size: None, + hashes: HashMap::new(), + }), + Self::Modrinth { id, version } => app.modrinth().resolve_source(id, version).await, + Self::CurseRinth { id, version } => app.curserinth().resolve_source(id, version).await, + Self::Spigot { id, version } => app.spigot().resolve_source(id, version).await, + Self::Hangar { id, version } => app.hangar().resolve_source(id, version).await, + Self::GithubRelease { repo, tag, asset } => app.github().resolve_source(repo, tag, asset).await, Self::Jenkins { url, job, build, artifact, - } => f - .debug_struct("Jenkins") - .field("Instance URL", url) - .field("Job", job) - .field("Build ID", build) - .field("Artifact", artifact) - .finish(), - + } => jenkins::resolve_source(app, url, job, build, artifact).await, Self::Maven { url, group, artifact, version, filename, - } => f - .debug_struct("Maven") - .field("Instance URL", url) - .field("Group", group) - .field("Artifact", artifact) - .field("Version", version) - .field("Filename", filename) - .finish(), + } => app.maven().resolve_source(url, group, artifact, version, filename).await, } } } diff --git a/src/model/downloadable/packwiz.rs b/src/model/downloadable/packwiz.rs index 6264b6c..c0c5805 100644 --- a/src/model/downloadable/packwiz.rs +++ b/src/model/downloadable/packwiz.rs @@ -6,21 +6,17 @@ use rpackwiz::model::{ use crate::{ model::Server, - sources::{ - curserinth::{fetch_curserinth_project, fetch_curserinth_versions}, - modrinth::{fetch_modrinth_project, fetch_modrinth_versions, DependencyType}, - }, + sources::modrinth::DependencyType, util::{hash::get_hash_url, packwiz::PackwizExportOptions}, - Source, + Source, App, }; use super::Downloadable; impl Downloadable { pub async fn from_pw_mod( + app: &App, m: &Mod, - http_client: &reqwest::Client, - server: &Server, ) -> Result> { Ok(Some(if let Some(upd) = &m.update { if let Some(mr) = &upd.modrinth { @@ -39,8 +35,7 @@ impl Downloadable { } } else { Self::from_url_interactive( - http_client, - server, + app, &m.download .url .clone() @@ -55,15 +50,14 @@ impl Downloadable { #[allow(clippy::too_many_lines)] // xd pub async fn to_pw_mod( &self, - http_client: &reqwest::Client, - server: &Server, + app: &App, opts: &PackwizExportOptions, is_opt: Option, desc_override: &str, ) -> Result> { Ok(match &self { Self::Modrinth { id, version } => { - let proj = fetch_modrinth_project(http_client, id).await?; + let proj = app.modrinth().fetch_project(id).await?; let side = match (proj.server_side.clone(), proj.client_side.clone()) { (DependencyType::Incompatible | DependencyType::Unsupported, _) => Side::Client, @@ -71,21 +65,7 @@ impl Downloadable { _ => Side::Both, }; - let versions = fetch_modrinth_versions(http_client, id, None).await?; - - let verdata = if version == "latest" { - versions.first() - } else { - versions.iter().find(|&v| v.id == version.clone()) - }; - - let Some(verdata) = verdata else { - bail!("Release '{version}' for project '{id}' not found"); - }; - - let Some(file) = verdata.files.first() else { - bail!("No files for project '{id}' version '{version}'"); - }; + let (file, ver) = app.modrinth().fetch_file(id, version).await?; let hash = file .hashes diff --git a/src/model/servertype/mod.rs b/src/model/servertype/mod.rs index 980d472..d11a38c 100644 --- a/src/model/servertype/mod.rs +++ b/src/model/servertype/mod.rs @@ -2,12 +2,12 @@ use anyhow::{Context, Result}; use async_trait::async_trait; use serde::{Deserialize, Deserializer, Serialize, Serializer}; -use crate::Source; +use crate::{Source, App, ResolvedFile}; use crate::model::Downloadable; -use crate::sources::{fabric, forge, jenkins, neoforge, papermc, purpur, quilt, vanilla}; +use crate::sources::quilt; -use super::{Server, StartupMethod}; +use super::StartupMethod; pub mod interactive; pub mod meta; @@ -159,7 +159,7 @@ impl ServerType { pub async fn get_install_method( &self, - http_client: &reqwest::Client, + app: &App, mcver: &str, ) -> Result { Ok(match self.clone() { @@ -180,7 +180,7 @@ impl ServerType { rename_from: Some("quilt-server-launch.jar".to_owned()), jar_name: format!( "quilt-server-launch-{mcver}-{}.jar", - quilt::map_quilt_loader_version(http_client, &loader) + quilt::map_quilt_loader_version(&app.http_client, &loader) .await .context("resolving quilt loader version id (latest/latest-beta)")? ), @@ -193,7 +193,7 @@ impl ServerType { rename_from: None, jar_name: format!( "libraries/net/neoforged/forge/{mcver}-{0}/forge-{mcver}-{0}-server.jar", - neoforge::map_neoforge_version(&loader, mcver, http_client).await? + app.neoforge().resolve_version(&loader).await? ) }, Self::Forge { loader } => InstallMethod::Installer { @@ -203,7 +203,7 @@ impl ServerType { rename_from: None, jar_name: format!( "libraries/net/minecraftforge/forge/{mcver}-{0}/forge-{mcver}-{0}-server.jar", - forge::map_forge_version(&loader, mcver, http_client).await? + app.forge().resolve_version(&loader).await? ) }, Self::BuildTools { args, software } => { @@ -240,13 +240,13 @@ impl ServerType { pub async fn get_startup_method( &self, - http_client: &reqwest::Client, + app: &App, serverjar_name: &str, mcver: &str, ) -> Result { Ok(match self { Self::NeoForge { loader } => { - let l = neoforge::map_neoforge_version(loader, mcver, http_client).await?; + let l = app.neoforge().resolve_version(loader).await?; StartupMethod::Custom { windows: vec![format!( @@ -258,7 +258,7 @@ impl ServerType { } } Self::Forge { loader } => { - let l = forge::map_forge_version(loader, mcver, http_client).await?; + let l = app.forge().resolve_version(loader).await?; StartupMethod::Custom { windows: vec![format!( @@ -340,200 +340,27 @@ impl ServerType { #[async_trait] impl Source for ServerType { - async fn download( - &self, - server: &Server, - client: &reqwest::Client, - ) -> Result { - let mcver = server.mc_version.clone(); - match self { - Self::Vanilla {} => Ok(vanilla::fetch_vanilla(&mcver, client).await?), - Self::PaperMC { project, build } => { - Ok(papermc::download_papermc_build(project, &mcver, build, client).await?) - } - Self::Purpur { build } => { - Ok(purpur::download_purpurmc_build(&mcver, build, client).await?) - } - - Self::Paper {} => { - Ok(papermc::download_papermc_build("paper", &mcver, "latest", client).await?) - } - Self::Velocity {} => { - Ok(papermc::download_papermc_build("velocity", &mcver, "latest", client).await?) - } - Self::Waterfall {} => { - Ok(papermc::download_papermc_build("waterfall", &mcver, "latest", client).await?) - } - - Self::Fabric { loader, installer } => { - Ok(fabric::download_fabric(client, &mcver, loader, installer).await?) - } - - Self::Quilt { installer, .. } => { - Ok(quilt::download_quilt_installer(client, installer).await?) - } - - Self::BungeeCord {} => Ok(bungeecord().download(server, client).await?), - Self::BuildTools { .. } => { - Ok(buildtools().download(server, client).await?) - } - Self::NeoForge { loader } => Ok(client - .get(neoforge::get_neoforge_installer_url(loader, &mcver, client).await?) - .send() - .await? - .error_for_status()?), - - Self::Forge { loader } => Ok(client - .get(forge::get_forge_installer_url(loader, &mcver, client).await?) - .send() - .await? - .error_for_status()?), - - Self::Downloadable { inner } => inner.download(server, client).await, - } - } + async fn resolve_source(&self, app: &App) -> Result { + let version = &app.mc_version(); - async fn get_filename(&self, server: &Server, client: &reqwest::Client) -> Result { - let mcver = server.mc_version.clone(); match self { - Self::Vanilla {} => Ok(format!("server-{mcver}.jar")), - Self::PaperMC { project, build } => { - Ok(get_filename_papermc(project, &mcver, build, client).await?) - } - - Self::Purpur { build } => { - if build == "latest" { - let last_build = purpur::fetch_purpurmc_builds(&mcver, client) - .await? - .last() - .cloned() - .unwrap_or("latest".to_owned()); - Ok(format!("purpur-{mcver}-{last_build}.jar")) - } else { - Ok(format!("purpur-{mcver}-{build}.jar")) - } - } - - Self::BungeeCord {} => { - let build = jenkins::get_jenkins_filename( - client, - BUNGEECORD_JENKINS, - BUNGEECORD_JOB, - "latest", - BUNGEECORD_ARTIFACT, - ) - .await? - .3; - Ok(format!("BungeeCord-{build}.jar")) - } - - Self::BuildTools { .. } => { - let build = jenkins::get_jenkins_filename( - client, - BUILDTOOLS_JENKINS, - "BuildTools", - "latest", - "BuildTools", - ) - .await? - .3; - Ok(format!("BuildTools-{build}.jar")) - } - - Self::Paper {} => Ok(get_filename_papermc("paper", &mcver, "latest", client).await?), - Self::Velocity {} => { - Ok(get_filename_papermc("velocity", &mcver, "latest", client).await?) - } - - Self::Waterfall {} => { - Ok(get_filename_papermc("waterfall", &mcver, "latest", client).await?) - } - - Self::Fabric { loader, installer } => { - let l = match loader.as_str() { - "latest" => fabric::fetch_fabric_latest_loader(client).await?, - id => id.to_owned(), - }; - - let i = match installer.as_str() { - "latest" => fabric::fetch_fabric_latest_installer(client).await?, - id => id.to_owned(), - }; - - Ok(format!("fabric-server-{mcver}-{l}-{i}.jar")) - } - - Self::Quilt { installer, .. } => { - Ok(quilt::get_installer_filename(client, installer).await?) - } - - Self::NeoForge { loader } => { - Ok(neoforge::get_neoforge_installer_filename(loader, &mcver, client).await?) - } - - Self::Forge { loader } => { - Ok(forge::get_forge_installer_filename(loader, &mcver, client).await?) - } - - Self::Downloadable { inner } => inner.get_filename(server, client).await, + ServerType::Vanilla { } => app.vanilla().resolve_source(version).await, + ServerType::PaperMC { project, build } => app.papermc().resolve_source(project, version, build).await, + ServerType::Purpur { build } => app.purpurmc().resolve_source(version, build).await, + ServerType::Fabric { loader, installer } => app.fabric().resolve_source(loader, installer).await, + ServerType::Quilt { loader, installer } => app.quilt().resolve_installer(installer).await, + ServerType::NeoForge { loader } => app.neoforge().resolve_source(loader).await, + ServerType::Forge { loader } => app.forge().resolve_source(loader).await, + ServerType::BuildTools { software, args } => buildtools().resolve_source(app).await, + ServerType::Paper { } => app.papermc().resolve_source("paper", version, "latest").await, + ServerType::Velocity { } => app.papermc().resolve_source("velocity", version, "latest").await, + ServerType::Waterfall { } => app.papermc().resolve_source("waterfall", version, "latest").await, + ServerType::BungeeCord { } => bungeecord().resolve_source(app).await, + ServerType::Downloadable { inner } => inner.resolve_source(app).await, } } } -impl std::fmt::Display for ServerType { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Self::Vanilla {} => f.write_str("Vanilla"), - - Self::Fabric { loader, installer } => f - .debug_struct("Fabric") - .field("loader", loader) - .field("installer", installer) - .finish(), - - Self::Quilt { loader, installer } => f - .debug_struct("Quilt") - .field("loader", loader) - .field("installer", installer) - .finish(), - - Self::NeoForge { loader } => { - f.debug_struct("NeoForged").field("loader", loader).finish() - } - - Self::Forge { loader } => f.debug_struct("Forge").field("loader", loader).finish(), - - Self::BungeeCord {} => f.write_str("BungeeCord"), - Self::BuildTools { .. } => f.write_str("BuildTools"), - Self::Paper {} => f.write_str("Paper, latest"), - Self::Velocity {} => f.write_str("Velocity, latest"), - Self::Waterfall {} => f.write_str("Waterfall, latest"), - Self::PaperMC { project, build } => { - f.write_str(&format!("PaperMC/{project}, build {build}")) - } - Self::Purpur { build } => f.write_str(&format!("Purpur, build {build}")), - Self::Downloadable { inner } => inner.fmt(f), - } - } -} - -async fn get_filename_papermc( - project: &str, - mcver: &str, - build: &str, - client: &reqwest::Client, -) -> Result { - if build == "latest" { - let build_id = papermc::fetch_papermc_build(project, mcver, build, client) - .await? - .build - .to_string(); - Ok(format!("{project}-{mcver}-{build_id}.jar")) - } else { - Ok(format!("{project}-{mcver}-{build}.jar")) - } -} - pub fn bungeecord() -> Downloadable { Downloadable::Jenkins { url: BUNGEECORD_JENKINS.to_owned(), diff --git a/src/sources/curserinth.rs b/src/sources/curserinth.rs index 4eb22f8..563d520 100644 --- a/src/sources/curserinth.rs +++ b/src/sources/curserinth.rs @@ -1,7 +1,7 @@ use anyhow::{Result, anyhow}; use serde::{Deserialize, Serialize, de::DeserializeOwned}; -use crate::{App, FileSource, CacheStrategy}; +use crate::{App, ResolvedFile, CacheStrategy}; use super::modrinth::{ModrinthFile, ModrinthProject, VersionType}; @@ -114,28 +114,20 @@ impl<'a> CurserinthAPI<'a> { )) } - pub async fn resolve_source(&self, id: &str, version: &str) -> Result { + pub async fn resolve_source(&self, id: &str, version: &str) -> Result { let (file, version) = self.fetch_file(id, version).await?; let cached_file_path = format!("{id}/{}/{}", version.id, file.filename); - if self.0.has_in_cache("curserinth", &cached_file_path) { - Ok(FileSource::Cached { - path: self.0.get_cache("curserinth").unwrap().0.join(cached_file_path), - filename: file.filename, - }) - } else { - Ok(FileSource::Download { - url: file.url, - filename: file.filename, - cache: if let Some(cache) = self.0.get_cache("curserinth") { - CacheStrategy::File { path: cache.0.join(cached_file_path) } - } else { - CacheStrategy::None - }, - size: Some(file.size), - hashes: file.hashes, - }) - } + Ok(ResolvedFile { + url: file.url, + filename: file.filename, + cache: CacheStrategy::File { + namespace: String::from("curserinth"), + path: cached_file_path + }, + size: Some(file.size), + hashes: file.hashes, + }) } } diff --git a/src/sources/fabric.rs b/src/sources/fabric.rs index fb4a833..33dc37c 100644 --- a/src/sources/fabric.rs +++ b/src/sources/fabric.rs @@ -3,7 +3,7 @@ use std::collections::HashMap; use anyhow::{anyhow, Result}; use mcapi::fabric::{FabricLoader, FabricInstaller, FABRIC_META_URL}; -use crate::{App, FileSource, CacheStrategy}; +use crate::{App, ResolvedFile, CacheStrategy}; pub struct FabricAPI<'a>(pub &'a App); @@ -24,7 +24,7 @@ impl<'a> FabricAPI<'a> { Ok(self.fetch_installers().await?.first().ok_or(anyhow!("No fabric installers???"))?.version.clone()) } - pub async fn resolve_source(&self, loader: &str, installer: &str) -> Result { + pub async fn resolve_source(&self, loader: &str, installer: &str) -> Result { let loader = match loader { "latest" => self.fetch_latest_loader().await?, id => id.to_owned(), @@ -37,26 +37,15 @@ impl<'a> FabricAPI<'a> { let cached_file_path = format!("fabric-server-{}-{installer}-{loader}.jar", self.0.mc_version()); - if self.0.has_in_cache("fabric", &cached_file_path) { - Ok(FileSource::Cached { - path: self.0.get_cache("fabric").unwrap().0.join(&cached_file_path), - filename: cached_file_path.clone(), - }) - } else { - Ok(FileSource::Download { - url: format!( - "{FABRIC_META_URL}/v2/versions/loader/{}/{loader}/{installer}/server/jar", - self.0.mc_version() - ), - filename: cached_file_path.clone(), - cache: if let Some(cache) = self.0.get_cache("fabric") { - CacheStrategy::File { path: cache.0.join(cached_file_path) } - } else { - CacheStrategy::None - }, - size: None, - hashes: HashMap::new(), - }) - } + Ok(ResolvedFile { + url: format!( + "{FABRIC_META_URL}/v2/versions/loader/{}/{loader}/{installer}/server/jar", + self.0.mc_version() + ), + filename: cached_file_path.clone(), + cache: CacheStrategy::File { namespace: String::from("fabric"), path: cached_file_path }, + size: None, + hashes: HashMap::new(), + }) } } diff --git a/src/sources/forge.rs b/src/sources/forge.rs index 602dbb6..5443179 100644 --- a/src/sources/forge.rs +++ b/src/sources/forge.rs @@ -1,6 +1,6 @@ use anyhow::{anyhow, Context, Result}; -use crate::{util, App, FileSource}; +use crate::{util, App, ResolvedFile}; pub static FORGE_MAVEN: &str = "https://maven.minecraftforge.net"; pub static FORGE_GROUP: &str = "net.minecraftforge"; @@ -43,7 +43,7 @@ impl<'a> ForgeAPI<'a> { }) } - pub async fn resolve_source(&self, loader: &str) -> Result { + pub async fn resolve_source(&self, loader: &str) -> Result { self.0.maven().resolve_source(FORGE_MAVEN, FORGE_GROUP, FORGE_ARTIFACT, &format!( "{}-{}", self.0.mc_version(), diff --git a/src/sources/github.rs b/src/sources/github.rs index d794b31..9b48c56 100644 --- a/src/sources/github.rs +++ b/src/sources/github.rs @@ -6,7 +6,7 @@ use reqwest::{header::{HeaderMap, HeaderValue}, StatusCode}; use serde::{Deserialize, Serialize, de::DeserializeOwned}; use tokio::time::sleep; -use crate::{App, FileSource, CacheStrategy}; +use crate::{App, ResolvedFile, CacheStrategy}; pub trait GithubRequestExt { fn with_token(self, token: Option) -> Self; @@ -197,34 +197,26 @@ impl<'a> GithubAPI<'a> { Ok((release, asset)) } - pub async fn resolve_source(&self, repo: &str, release_tag: &str, asset_name: &str) -> Result { + pub async fn resolve_source(&self, repo: &str, release_tag: &str, asset_name: &str) -> Result { let (release, asset) = self.fetch_asset(repo, release_tag, asset_name).await?; let cached_file_path = format!("{repo}/releases/{}/{}", release.tag_name, asset.name); let has_in_cache = self.0.has_in_cache(CACHE_DIR, &cached_file_path); - if has_in_cache { - Ok(FileSource::Cached { - path: self.0.get_cache(CACHE_DIR).unwrap().0.join(cached_file_path), - filename: asset.name, - }) - } else { - Ok(FileSource::Download { - url: format!( - "https://github.com/{repo}/releases/download/{}/{}", - release.tag_name, asset.name - ), - filename: asset.name, - cache: if let Some(cache) = self.0.get_cache(CACHE_DIR) { - CacheStrategy::File { path: cache.0.join(cached_file_path) } - } else { - CacheStrategy::None - }, - size: Some(asset.size), - hashes: HashMap::new(), - }) - } + Ok(ResolvedFile { + url: format!( + "https://github.com/{repo}/releases/download/{}/{}", + release.tag_name, asset.name + ), + filename: asset.name, + cache: CacheStrategy::File { + namespace: CACHE_DIR.to_owned(), + path: cached_file_path + }, + size: Some(asset.size), + hashes: HashMap::new(), + }) } } diff --git a/src/sources/hangar.rs b/src/sources/hangar.rs index 13977af..eaca2dd 100644 --- a/src/sources/hangar.rs +++ b/src/sources/hangar.rs @@ -3,7 +3,7 @@ use std::collections::HashMap; use anyhow::{anyhow, Context, Result}; use mcapi::hangar::{Platform, ProjectVersion}; -use crate::{App, CacheStrategy, FileSource}; +use crate::{App, CacheStrategy, ResolvedFile}; pub struct HangarAPI<'a>(pub &'a App); @@ -55,7 +55,7 @@ impl<'a> HangarAPI<'a> { Ok(version) } - pub async fn resolve_source(&self, id: &str, version: &str) -> Result { + pub async fn resolve_source(&self, id: &str, version: &str) -> Result { let version = self .fetch_hangar_version(id, version) .await @@ -78,28 +78,18 @@ impl<'a> HangarAPI<'a> { let cached_file_path = format!("{id}/{}/{}", version.name, download.get_file_info().name); - if self.0.has_in_cache("hangar", &cached_file_path) { - Ok(FileSource::Cached { - path: self.0.get_cache("hangar").unwrap().0.join(cached_file_path), - filename: download.get_file_info().name, - }) - } else { - Ok(FileSource::Download { - url: download.get_url(), - filename: download.get_file_info().name, - cache: if let Some(cache) = self.0.get_cache("hangar") { - CacheStrategy::File { - path: cache.0.join(cached_file_path), - } - } else { - CacheStrategy::None - }, - size: Some(download.get_file_info().size_bytes as i32), - hashes: HashMap::from([( - "sha256".to_owned(), - download.get_file_info().sha256_hash, - )]), - }) - } + Ok(ResolvedFile { + url: download.get_url(), + filename: download.get_file_info().name, + cache: CacheStrategy::File { + namespace: String::from("hangar"), + path: cached_file_path, + }, + size: Some(download.get_file_info().size_bytes as i32), + hashes: HashMap::from([( + "sha256".to_owned(), + download.get_file_info().sha256_hash, + )]), + }) } } diff --git a/src/sources/jenkins.rs b/src/sources/jenkins.rs index c29b181..b204bc6 100644 --- a/src/sources/jenkins.rs +++ b/src/sources/jenkins.rs @@ -5,7 +5,7 @@ use std::collections::HashMap; use anyhow::{anyhow, Result}; -use crate::{App, CacheStrategy, FileSource}; +use crate::{App, CacheStrategy, ResolvedFile}; //pub static API_MAGIC_JOB: &str = "/api/json?tree=url,name,builds[*[url,number,result,artifacts[relativePath,fileName]]]"; static API_MAGIC_JOB: &str = "/api/json?tree=builds[*[url,number,result]]"; @@ -18,7 +18,7 @@ pub async fn resolve_source( job: &str, build: &str, artifact: &str, -) -> Result { +) -> Result { let (build_url, filename, relative_path, build_number, md5hash) = get_jenkins_filename(&app.http_client, url, job, build, artifact).await?; @@ -31,30 +31,20 @@ pub async fn resolve_source( let cached_file_path = format!("{folder}/{job}/{build_number}/{filename}"); - if app.has_in_cache("jenkins", &cached_file_path) { - Ok(FileSource::Cached { - path: app.get_cache("jenkins").unwrap().0.join(cached_file_path), - filename, - }) - } else { - Ok(FileSource::Download { - url: format!("{build_url}artifact/{relative_path}"), - filename, - cache: if let Some(cache) = app.get_cache("jenkins") { - CacheStrategy::File { - path: cache.0.join(cached_file_path), - } - } else { - CacheStrategy::None - }, - size: None, - hashes: if let Some(md5) = md5hash { - HashMap::from([("md5".to_owned(), md5.clone())]) - } else { - HashMap::new() - }, - }) - } + Ok(ResolvedFile { + url: format!("{build_url}artifact/{relative_path}"), + filename, + cache: CacheStrategy::File { + namespace: String::from("jenkins"), + path: cached_file_path, + }, + size: None, + hashes: if let Some(md5) = md5hash { + HashMap::from([("md5".to_owned(), md5.clone())]) + } else { + HashMap::new() + }, + }) } // has 1 dep, beware lol diff --git a/src/sources/maven.rs b/src/sources/maven.rs index 12af214..e81a8ce 100644 --- a/src/sources/maven.rs +++ b/src/sources/maven.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use anyhow::{anyhow, Result}; -use crate::{App, FileSource, util, CacheStrategy}; +use crate::{App, ResolvedFile, util, CacheStrategy}; pub struct MavenAPI<'a>(pub &'a App); @@ -94,7 +94,7 @@ impl<'a> MavenAPI<'a> { artifact_id: &str, version: &str, file: &str, - ) -> Result { + ) -> Result { let version = self.fetch_version(url, group_id, artifact_id, version).await?; let file = file @@ -120,23 +120,14 @@ impl<'a> MavenAPI<'a> { group_id.replace(".", "/"), ); - if self.0.has_in_cache("maven", &cached_file_path) { - Ok(FileSource::Cached { - path: self.0.get_cache("maven").unwrap().0.join(cached_file_path), - filename: file, - }) - } else { - Ok(FileSource::Download { - url: download_url, - filename: file, - cache: if let Some(cache) = self.0.get_cache("maven") { - CacheStrategy::File { path: cache.0.join(cached_file_path) } - } else { - CacheStrategy::None - }, - size: None, - hashes: HashMap::new(), - }) - } + Ok(ResolvedFile { + url: download_url, + filename: file, + cache: CacheStrategy::File { + namespace: String::from("maven"), + path: cached_file_path }, + size: None, + hashes: HashMap::new(), + }) } } diff --git a/src/sources/modrinth.rs b/src/sources/modrinth.rs index a7549e2..51e8d9e 100644 --- a/src/sources/modrinth.rs +++ b/src/sources/modrinth.rs @@ -3,7 +3,7 @@ use std::collections::HashMap; use anyhow::{Result, anyhow}; use serde::{Deserialize, Serialize, de::DeserializeOwned}; -use crate::{App, FileSource, CacheStrategy}; +use crate::{App, ResolvedFile, CacheStrategy}; #[derive(Debug, Deserialize, Serialize, Clone)] pub struct ModrinthProject { @@ -157,30 +157,20 @@ impl<'a> ModrinthAPI<'a> { .await?) } - pub async fn resolve_source(&self, id: &str, version: &str) -> Result { + pub async fn resolve_source(&self, id: &str, version: &str) -> Result { let (file, version) = self.fetch_file(id, version).await?; let cached_file_path = format!("{id}/{}/{}", version.id, file.filename); - if self.0.has_in_cache("modrinth", &cached_file_path) { - Ok(FileSource::Cached { - path: self.0.get_cache("modrinth").unwrap().0.join(cached_file_path), - filename: file.filename, - }) - } else { - Ok(FileSource::Download { - url: file.url, - filename: file.filename, - cache: if let Some(cache) = self.0.get_cache("modrinth") { - CacheStrategy::File { - path: cache.0.join(cached_file_path), - } - } else { - CacheStrategy::None - }, - size: Some(file.size), - hashes: file.hashes, - }) - } + Ok(ResolvedFile { + url: file.url, + filename: file.filename, + cache: CacheStrategy::File { + namespace: String::from("modrinth"), + path: cached_file_path, + }, + size: Some(file.size), + hashes: file.hashes, + }) } } diff --git a/src/sources/neoforge.rs b/src/sources/neoforge.rs index 0744fca..0607ae5 100644 --- a/src/sources/neoforge.rs +++ b/src/sources/neoforge.rs @@ -1,6 +1,6 @@ use anyhow::{anyhow, Context, Result}; -use crate::{util, App, FileSource}; +use crate::{util, App, ResolvedFile}; pub static NEOFORGE_MAVEN: &str = "https://maven.neoforged.net/releases"; pub static NEOFORGE_GROUP: &str = "net.neoforged"; @@ -43,7 +43,7 @@ impl<'a> NeoforgeAPI<'a> { }) } - pub async fn resolve_source(&self, loader: &str) -> Result { + pub async fn resolve_source(&self, loader: &str) -> Result { self.0.maven().resolve_source(NEOFORGE_MAVEN, NEOFORGE_GROUP, NEOFORGE_ARTIFACT, &format!( "{}-{}", self.0.mc_version(), diff --git a/src/sources/papermc.rs b/src/sources/papermc.rs index a72ada7..6966b82 100644 --- a/src/sources/papermc.rs +++ b/src/sources/papermc.rs @@ -3,9 +3,9 @@ use std::collections::HashMap; use anyhow::{anyhow, Result}; use serde::{de::DeserializeOwned, Serialize}; -use crate::{App, FileSource, CacheStrategy}; +use crate::{App, ResolvedFile, CacheStrategy}; -pub struct PaperMCAPI<'a>(&'a App); +pub struct PaperMCAPI<'a>(pub &'a App); const PAPERMC_URL: &str = "https://api.papermc.io/v2"; const CACHE_DIR: &str = "papermc"; @@ -55,35 +55,22 @@ impl<'a> PaperMCAPI<'a> { }) } - pub async fn resolve_source(&self, project: &str, version: &str, build: &str) -> Result { + pub async fn resolve_source(&self, project: &str, version: &str, build: &str) -> Result { let resolved_build = self.fetch_build(project, version, build).await?; let download = resolved_build.downloads.get("application") .ok_or(anyhow!("downloads['application'] missing for papermc project {project} {version}, build {build} ({})", resolved_build.build))?; let cached_file_path = format!("{project}/{}", download.name); - let has_in_cache = self.0.has_in_cache(CACHE_DIR, &cached_file_path); - - if has_in_cache { - Ok(FileSource::Cached { - path: self.0.get_cache(CACHE_DIR).unwrap().0.join(cached_file_path), - filename: download.name.clone(), - }) - } else { - Ok(FileSource::Download { - url: format!( - "{PAPERMC_URL}/projects/{project}/versions/{version}/builds/{}/downloads/{}", - resolved_build.build, download.name - ), - filename: download.name.clone(), - cache: if let Some(cache) = self.0.get_cache(CACHE_DIR) { - CacheStrategy::File { path: cache.0.join(cached_file_path) } - } else { - CacheStrategy::None - }, - size: None, - hashes: HashMap::new(), - }) - } + Ok(ResolvedFile { + url: format!( + "{PAPERMC_URL}/projects/{project}/versions/{version}/builds/{}/downloads/{}", + resolved_build.build, download.name + ), + filename: download.name.clone(), + cache: CacheStrategy::File { namespace: CACHE_DIR.to_owned(), path: cached_file_path }, + size: None, + hashes: HashMap::new(), + }) } } diff --git a/src/sources/purpur.rs b/src/sources/purpur.rs index ee1d14f..eee32c0 100644 --- a/src/sources/purpur.rs +++ b/src/sources/purpur.rs @@ -3,9 +3,9 @@ use std::collections::HashMap; use anyhow::{Result, anyhow}; use serde::{Deserialize, Serialize, de::DeserializeOwned}; -use crate::{App, FileSource, CacheStrategy}; +use crate::{App, ResolvedFile, CacheStrategy}; -pub struct PurpurAPI<'a>(&'a App); +pub struct PurpurAPI<'a>(pub &'a App); pub const API_URL: &str = "https://api.purpurmc.org/v2/purpur"; pub const CACHE_DIR: &str = "purpur"; @@ -40,36 +40,23 @@ impl<'a> PurpurAPI<'a> { }) } - pub async fn resolve_source(&self, version: &str, build: &str) -> Result { + pub async fn resolve_source(&self, version: &str, build: &str) -> Result { let resolved_build = self.fetch_build(version, build).await?; let cached_file_path = format!("purpur-{version}-{}.jar", resolved_build.build); - let has_in_cache = self.0.has_in_cache(CACHE_DIR, &cached_file_path); - - if has_in_cache { - Ok(FileSource::Cached { - path: self.0.get_cache(CACHE_DIR).unwrap().0.join(&cached_file_path), - filename: cached_file_path.clone(), - }) - } else { - Ok(FileSource::Download { - url: format!( - "{API_URL}/{version}/{}/download", - resolved_build.build - ), - filename: cached_file_path.clone(), - cache: if let Some(cache) = self.0.get_cache(CACHE_DIR) { - CacheStrategy::File { path: cache.0.join(cached_file_path) } - } else { - CacheStrategy::None - }, - size: None, - hashes: HashMap::from([ - ("md5".to_owned(), resolved_build.md5) - ]), - }) - } + Ok(ResolvedFile { + url: format!( + "{API_URL}/{version}/{}/download", + resolved_build.build + ), + filename: cached_file_path.clone(), + cache: CacheStrategy::File { namespace: CACHE_DIR.to_owned(), path: cached_file_path }, + size: None, + hashes: HashMap::from([ + ("md5".to_owned(), resolved_build.md5) + ]), + }) } } diff --git a/src/sources/quilt.rs b/src/sources/quilt.rs index 110fa5b..b2750ce 100644 --- a/src/sources/quilt.rs +++ b/src/sources/quilt.rs @@ -4,9 +4,9 @@ use anyhow::{anyhow, Result}; use mcapi::quilt::{self, InstallerVariant}; -use crate::{App, FileSource}; +use crate::{App, ResolvedFile}; -pub struct QuiltAPI<'a>(&'a App); +pub struct QuiltAPI<'a>(pub &'a App); pub const QUILT_MAVEN_URL: &str = "https://maven.quiltmc.org/repository/release"; pub const QUILT_MAVEN_GROUP: &str = "org.quiltmc"; @@ -14,7 +14,7 @@ pub const QUILT_MAVEN_ARTIFACT: &str = "quilt-installer"; pub const QUILT_MAVEN_FILE: &str = "${artifact}-${version}.jar"; impl<'a> QuiltAPI<'a> { - pub async fn resolve_installer(&self, version: &str) -> Result { + pub async fn resolve_installer(&self, version: &str) -> Result { self.0.maven().resolve_source( QUILT_MAVEN_URL, QUILT_MAVEN_GROUP, diff --git a/src/sources/spigot.rs b/src/sources/spigot.rs index 5a71bcc..495a601 100644 --- a/src/sources/spigot.rs +++ b/src/sources/spigot.rs @@ -3,7 +3,7 @@ use std::collections::HashMap; use anyhow::Result; use serde::{Deserialize, Serialize, de::DeserializeOwned}; -use crate::{App, FileSource, CacheStrategy}; +use crate::{App, ResolvedFile, CacheStrategy}; #[derive(Debug, Deserialize, Serialize)] struct SpigotResourceVersion { @@ -13,7 +13,7 @@ struct SpigotResourceVersion { -pub struct SpigotAPI<'a>(&'a App); +pub struct SpigotAPI<'a>(pub &'a App); pub const API_URL: &str = "https://api.spiget.org/v2"; pub const CACHE_DIR: &str = "spiget"; @@ -58,35 +58,22 @@ impl<'a> SpigotAPI<'a> { self.fetch_api(&format!("{API_URL}/resources/{}/versions/{version}", Self::get_resource_id(id))).await } - pub async fn resolve_source(&self, id: &str, version: &str) -> Result { + pub async fn resolve_source(&self, id: &str, version: &str) -> Result { let resolved_version = self.fetch_version(id, version).await?; let filename = format!("spigot-{id}-{}.jar", resolved_version.name); let cached_file_path = format!("{id}/{}.jar", resolved_version.id); - let has_in_cache = self.0.has_in_cache(CACHE_DIR, &cached_file_path); - - if has_in_cache { - Ok(FileSource::Cached { - path: self.0.get_cache(CACHE_DIR).unwrap().0.join(cached_file_path), - filename: filename.clone(), - }) - } else { - Ok(FileSource::Download { - url: format!( - "{API_URL}/resources/{}/versions/{version}/download", - Self::get_resource_id(id) - ), - filename, - cache: if let Some(cache) = self.0.get_cache(CACHE_DIR) { - CacheStrategy::File { path: cache.0.join(cached_file_path) } - } else { - CacheStrategy::None - }, - size: None, - hashes: HashMap::new(), - }) - } + Ok(ResolvedFile { + url: format!( + "{API_URL}/resources/{}/versions/{version}/download", + Self::get_resource_id(id) + ), + filename, + cache: CacheStrategy::File { namespace: CACHE_DIR.to_owned(), path: cached_file_path }, + size: None, + hashes: HashMap::new(), + }) } } diff --git a/src/sources/vanilla.rs b/src/sources/vanilla.rs index 8c32367..ee91c30 100644 --- a/src/sources/vanilla.rs +++ b/src/sources/vanilla.rs @@ -2,9 +2,9 @@ use std::collections::HashMap; use anyhow::{anyhow, Result}; -use crate::{App, FileSource, CacheStrategy}; +use crate::{App, ResolvedFile, CacheStrategy}; -pub struct VanillaAPI<'a>(&'a App); +pub struct VanillaAPI<'a>(pub &'a App); pub const CACHE_DIR: &str = "vanilla"; @@ -13,7 +13,7 @@ impl<'a> VanillaAPI<'a> { Ok(mcapi::vanilla::fetch_version_manifest(&self.0.http_client).await?.latest.release) } - pub async fn resolve_source(&self, version: &str) -> Result { + pub async fn resolve_source(&self, version: &str) -> Result { let version_manifest = mcapi::vanilla::fetch_version_manifest(&self.0.http_client).await?; let version = match version { @@ -30,27 +30,14 @@ impl<'a> VanillaAPI<'a> { let cached_file_path = format!("server-{}.jar", version.id); - let has_in_cache = self.0.has_in_cache(CACHE_DIR, &cached_file_path); - - if has_in_cache { - Ok(FileSource::Cached { - path: self.0.get_cache(CACHE_DIR).unwrap().0.join(&cached_file_path), - filename: cached_file_path.clone(), - }) - } else { - Ok(FileSource::Download { - url: file.url.clone(), - filename: cached_file_path.clone(), - cache: if let Some(cache) = self.0.get_cache(CACHE_DIR) { - CacheStrategy::File { path: cache.0.join(cached_file_path) } - } else { - CacheStrategy::None - }, - size: Some(file.size as i32), - hashes: HashMap::from([ - ("sha1".to_owned(), file.sha1.clone()) - ]), - }) - } + Ok(ResolvedFile { + url: file.url.clone(), + filename: cached_file_path.clone(), + cache: CacheStrategy::File { namespace: CACHE_DIR.to_owned(), path: cached_file_path }, + size: Some(file.size as i32), + hashes: HashMap::from([ + ("sha1".to_owned(), file.sha1.clone()) + ]), + }) } }